#!/bin/sh -e
#
# $Id: mkaptbox,v 1.39 2005/07/05 11:09:25 ldv Exp $
# Copyright (C) 2003-2005  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.
#

if [ "$0" = ./mkaptbox ]; then
	. ./functions
	hasher_dir="$(/bin/pwd)"
else
	. /usr/share/hasher/functions
fi

show_help()
{
	cat <<EOF
mkaptbox - create initial aptbox in specified working directory.

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

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

Valid options are:
  --apt-config=FILE         path to custom apt.conf file;
  --apt-prefix=DIR          path to apt directory prefix (e.g. /usr);
  -f, --force               force aptbox creation;
  --no-update               do not run "apt-get update" after creation;
  --repo=DIR                path to work repository directory;
  --target=ARCH             target architecture of the work repository;
  -u, --update              run "apt-get update" after creation;
  --without-stuff           do not configure sources.list for the work repository;
  --with-stuff              configure sources.list for the work repository;
  -q, --quiet               try to be more quiet;
  -v, --verbose             print a message for each action;
  -V, --version             print program version and exit;
  -h, --help                show this text and exit.

Report bugs to http://bugs.altlinux.ru/

EOF
	exit
}

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

apt_config=
force=
update=1
while :; do
	case "$1" in
		--apt-config)
			apt_config="$(opt_check_read "$1" "$2")"
			shift
			;;
		--apt-prefix)
			apt_prefix="$(opt_check_dir "$1" "$2")"
			shift
			;;
		-f|--force) force=1
			;;
		--no-update) update=
			;;
		--repo) shift; repo="$1"
			;;
		--target) shift; target="$1"
			[ -z "${target##[A-Za-z]*}" ] ||
				Fatal "--target: $target: invalid architecture."
			;;
		-u|--update) update=1
			;;
		--without-stuff|--no-stuff) no_stuff=1
			;;
		--with-stuff) no_stuff=
			;;
		--) shift; break
			;;
		*) parse_common_options mkaptbox "$1"
			;;
	esac
	shift
done

# Exactly one argument, please.
[ "$#" -ge 1 ] || show_usage 'Insufficient arguments.'
[ "$#" -le 1 ] || show_usage 'Too many arguments.'

set_workdir "$1"
shift

set_apt_vars
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"

read_apt_config Dir::Etc::SourceList sl_value

# reset APT_CONFIG and fill it with new data.
export APT_CONFIG="$aptbox/etc/apt/apt.conf"
cat >"$APT_CONFIG" <<__EOF__
${apt_config:+$(cat "$apt_config")}
Dir::State "$aptbox/var/lib/apt/";
Dir::Cache "$aptbox/var/cache/apt/";
Dir::Etc::SourceList "$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:-$def_repo} ${target:-$def_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/'."

# create apt wrappers.
for n in apt-cache apt-config apt-get genbasedir; do
	cat >"$n" <<__EOF__
#!/bin/sh -e

if [ -n "$LD_LIBRARY_PATH" ]; then
	LD_LIBRARY_PATH="$LD_LIBRARY_PATH"
	export LD_LIBRARY_PATH
fi
APT_CONFIG="$APT_CONFIG" \
exec $(eval printf %s "$(printf '$prog_%s' "$n" |tr - _)") "\$@"
__EOF__
Verbose "created $n wrapper"
chmod $verbose a+x "$n"
done

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