#!/bin/sh -e

PROG="${0##*/}"

if [ "$0" = ./$PROG ]; then
    . ../data/functions
    SCRIPTDIR=.
else
    . /usr/share/separator/functions
fi

print_usage()
{
	[ "$1" = 0 ] || exec >&2
	cat <<EOF
Usage: $PROG [options] <workdir>

$PROG initialize aptbox and other directories we need to create livecd

Valid options are:
  -h, --help	display help screen
  -v, --version	display version information
  -a, --aptconf name of alternative apt.conf
  -u, --update update apt-box
  -p, --profile <profile> use profile instead of default
		     				     
Report bugs to <rider@altlinux.org>
EOF
	[ -n "$1" ] && exit "$1" || exit
}



#parse command line options
TEMP=`getopt -n $PROG -o a:,v,h,u,p: -l aptconf:,version,help,update,profile: -- "$@"` || exit 1
eval set -- "$TEMP"

workdir=
deps_file=
langs=
update=
profile="default"
while :; do
	case "$1" in
		-h|--help) print_usage 0
			;;
		-w|--workdir)
			shift
			workdir="$1"
		    ;;
		-a|--aptconf)
			shift
			aptconf="$1"
		    ;;
		-v|--version) print_version;exit 0
		    ;;
		-p|--profile)
			shift
			profile="$1"
		    ;;
		-u|--update)
			update=1
		    ;;
		--) shift; break
		   ;;
		*) "unrecognized option: $1"
		    exit 1
			;;
	esac
	shift
done

WORKDIR=$@

[ -z "$WORKDIR" ] && print_usage 0

[ -d "$WORKDIR" ] || mkdir -p "$WORKDIR"

[ -d "$WORKDIR/aptbox" ] && rm -Rf "$WORKDIR/aptbox"
/usr/bin/mkaptbox $WORKDIR

[ -z "$update" ] || exit 0

[ -d "$WORKDIR/profile" ] && exit 0

mkdir -p $WORKDIR/{profile,root,tmp}

[ -d $tordir/profile-$profile/ ] || Fatal "Profile $profile not found"

cp -a $tordir/profile-$profile/* $WORKDIR/profile/
