#!/bin/bash
#=============================================================================#
# Make fakeroot directory with installed packages			      #
#=============================================================================#
# Requires:
#	hasher by Dmitry V. Levin (ldv)
#=============================================================================#
# (C) Denis Smirnov <mithraen@freesource.info>			  12.06.2006  #
#=============================================================================#
set +e

WORKDIR=$1; shift
PKGLIST=$1; shift
LANGS=en:ru:ru_RU:en_US:ru_RU.KOI8-R

mkdir -p $WORKDIR

WORKDIR=`realpath "$WORKDIR"`
PKGLIST=`realpath "$PKGLIST"`

T=`mktemp`

showhelp()
{
	echo "Using:"
	echo "    pkglst2aptbox <path to APTBOX> <path to package list> [hasher options]"
	echo "If package list is directory, used all files from this directory"
}

if [ "x$WORKDIR" == "x" ]; then
	showhelp
	exit -1
fi

if [ "x$WORKDIR" == "x" ]; then
	showhelp
	exit -1
fi

hsh "$WORKDIR" --cleanup
hsh "$WORKDIR" \
	    --initroot-only \
	    --save-fakeroot \
	    --install-langs="$LANGS"  \
	    --pkg-init-list='setup filesystem info-install coreutils rpm fakeroot>=0:0.7.3 findutils' \
	    --pkg-build-list=',' \
		"$@"

	hsh-run "$WORKDIR" --rooter -- /bin/mkdir -p /etc/sysconfig
	hsh-run "$WORKDIR" --rooter -- /bin/sh -c "echo LANG=en_US > /etc/sysconfig/i18n"

	# Install packages
	if [ -d "$PKGLIST" ]; then
		for s in "$PKGLIST"/*; do
				echo $s
				if [ -f "$s" ]; then
					cat "$s" \
						| grep -h '^[^#]' \
						>> $T
				fi
		done
	else
		grep -h '^[^#]' "$PKGLIST" \
			>> $T
	fi
	cat $T | sort -u | xargs -r hsh-install "$WORKDIR"

#	    ${ARCH:+--target="$ARCH"} \
#	    ${REPO:+--repo="$REPO"} \
#	    ${LANGS:+--install-langs="$LANGS"} \
#	    ${apt_config:+--apt-config=$apt_config} \
#	    ${apt_prefix:+--apt-prefix=$apt_prefix} \
