#!/bin/sh -e
#
# $Id: mkaptbox,v 1.21 2003/10/17 13:10:56 ldv Exp $
# Copyright (C) 2003  Dmitry V. Levin <ldv@altlinux.org>
# 
# The mkaptbox utility for the hasher project
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

. /usr/share/hasher/functions

Usage()
{
	[ "$1" = 0 ] || exec >&2
	cat <<EOF
mkaptbox - creates initial aptbox.

This program is free software, covered by the GNU General Public License.
mkaptbox comes with ABSOLUTELY NO WARRANTY, see license for details.

Usage: $PROG [options] <path-to-workdir>

<path-to-workdir> must be valid writable directory.

Valid options are:
--no-stuff              do not install built packages;
--target=ARCH           target architecture;
--apt-config=FILE       path to custom apt.conf file;
-f, --force             force aptbox creation;
-u, --update            run "apt-get update" after creation;
-q, --quiet             try to be more quiet;
-v, --verbose           print a message for each action;
-h, --help              show this text.
EOF
	[ -n "$1" ] && exit "$1" || exit
}

TEMP=`getopt -n $PROG -o f,u,q,v,h -l no-stuff,target:,apt-config:,force,update,quiet,verbose,help -- "$@"` || Usage
eval set -- "$TEMP"

force=
update=
apt_config=
while :; do
	case "$1" in
		--no-stuff) no_stuff=1; shift
			;;
		--target) shift; target="$1"; shift
			;;
		--apt-config) shift; apt_config="$1"; shift
			[ -r "$apt_config" ] || Fatal "$apt_config: cannot access."
			;;
		-f|--force) force=1; shift
			;;
		-u|--update) update=1; shift
			;;
		-q|--quiet) quiet=-q; shift
			;;
		-v|--verbose) verbose=-v; shift
			;;
		-h|--help) Usage 0
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
done

# Exactly one argument, please.
[ "$#" -eq 1 ] || Usage
set_workdir "$1"
shift

make_repo
update_repo

# exists already?
if [ -e aptbox -o -L aptbox ]; then
	[ -n "$force" ] || Fatal "remove $aptbox first."
	[ -d aptbox ] || Fatal "$aptbox: invalid pathname."

	# empty dir?
	if rmdir -- aptbox 2>/dev/null; then
		mkdir -m700 $verbose -- aptbox
		cd aptbox
	else
		cd aptbox
		[ -d ./etc/apt -a -d ./var/lib/rpm ] ||
			Fatal "$aptbox: doesn't look valid."
	fi
else
	mkdir -m700 $verbose -- aptbox
	cd aptbox
fi

Verbose "changed working directory to \`$aptbox'"

# apt/rpm skeleton directories.
mkdir -p $verbose -- \
	./etc/apt/{apt.conf,{sources,vendors}.list}.d \
	./var/cache/apt/archives/partial \
	./var/lib/apt/lists/partial \
	./var/lib/rpm

[ -z "$apt_config" ] || APT_CONFIG="$apt_config"

get_apt_config()
{
	local temp name value
	name="$1"
	shift || return 1
	value="$1"
	shift || return 1

	temp="$(apt-config shell "$value" "$name")" || return 1
	eval $temp
}
	
sl_name="Dir::Etc::SourceList"
get_apt_config "$sl_name" sl_value
[ -n "$sl_value" ] || Fatal "apt-config: undefined: $sl_name"

if [ -n "${sl_value##/*}" ]; then
	get_apt_config "Dir::Etc" dir_etc
	[ -n "$dir_etc" ] || Fatal "apt-config: undefined: Dir::Etc"

	if [ -n "${dir_etc##/*}" ]; then
		get_apt_config "Dir" dir
		[ -n "$dir" ] || Fatal "apt-config: undefined: Dir"

		dir_etc="$dir$dir_etc"
		unset dir
	fi
	sl_value="$dir_etc$sl_value"
	unset dir_etc
fi

[ -z "${sl_value##/*}" ] &&
	[ -s "$sl_value" -a -r "$sl_value" ] ||
	Fatal "apt-config: broken $sl_name: $sl_value"

# reset APT_CONFIG and fill it with new data.
export APT_CONFIG="$aptbox/etc/apt/apt.conf"
cat >"$APT_CONFIG" <<__EOF__
Dir::State "$aptbox/var/lib/apt/";
Dir::Cache "$aptbox/var/cache/apt/";
$sl_name "$aptbox/etc/apt/sources.list";
RPM::RootDir "$aptbox";
APT::Install::Virtual "true";
APT::Install::VirtualVersion "true";
__EOF__
Verbose "created APT configuration file \`$APT_CONFIG'"

target_config="./etc/apt/sources.list"
cat >"$target_config" <<__EOF__
${no_stuff:+#}rpm file:$workdir/repo $target hasher
$(grep '^[^#]' "$sl_value")
__EOF__
Verbose "created APT source list file \`$target_config'"

# create a new rpm database.
rpmdb --initdb --dbpath "$aptbox/var/lib/rpm"
Verbose "created RPM database in \`./var/lib/rpm/'"

[ -z "$update" ] || apt-get update $([ -n "$verbose" ] || echo -qq)
