#!/bin/sh -ef
#
# Copyright (C) 2003-2008  Dmitry V. Levin <ldv@altlinux.org>
# Copyright (C) 2007  Kirill A. Shutemov <kas@altlinux.org>
# 
# The hsh-mkchroot 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

. hsh-sh-functions

show_help()
{
	cat <<EOF
hsh-mkchroot - create initial chroot.

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

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

Options:
  --hasher-priv-dir=DIR     hasher-priv directory;
  --number=NUMBER           subconfig identifier;
  --wait-lock               wait for workdir and hasher-priv locks;
  --no-wait-lock            do not wait for workdir and hasher-priv locks;
  --with-qemu=ARCH          copy qemu executable into the chroot .host directory;
  -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 $getopt_common_opts -l hasher-priv-dir:,no-lock,number:,wait-lock,no-wait-lock,with-qemu:,$getopt_common_longopts -- "$@"` ||
	show_usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		--hasher-priv-dir)
			hasher_priv_dir="$(opt_check_dir "$1" "$2")"
			shift
			;;
		--no-lock) no_lock=1
			;;
		--number)
			if [ -z "${2##/*}" ]; then
				[ -d "$2" ] ||
					fatal "$1: $2: Directory not available."
				number="$2"
			else
				number="$(opt_check_number_ge_0 "$1" "$2")"
			fi
			shift
			;;
		--wait-lock) lock_nowait=
			;;
		--no-wait-lock) lock_nowait=1
			;;
		--with-qemu)
			qemu_arch="$2"
			shift
			;;
		--) shift; break
			;;
		*) parse_common_option "$1"
			;;
	esac
	shift
done

if [ -z "$workdir" ]; then
	# Exactly one argument.
	[ "$#" -ge 1 ] || show_usage 'Insufficient arguments.'
	[ "$#" -le 1 ] || show_usage 'Too many arguments.'
else
	# At most one argument.
	[ "$#" -le 1 ] || show_usage 'Too many arguments.'
fi

if [ "$#" -ge 1 ]; then
	set_workdir "$1"
	shift
else
	set_workdir
fi

lock_workdir

# exists already?
[ ! -e chroot -a ! -L chroot ] ||
	fatal "Remove $chroot first."

if [ -n "$number" -a -z "${number##/*}" ]; then
	deduce_lock_hasher_priv "$number"
else
	allocate_lock_hasher_priv
fi

ugid1="$("$getugid1" ${number:+-$number})" ||
	fatal "getugid1 failed."
gid1="$(printf %s "$ugid1" |cut -d: -f2)"
ugid2="$("$getugid2" ${number:+-$number})" ||
	fatal "getugid2 failed."
gid2="$(printf %s "$ugid2" |cut -d: -f2)"

mkdir -m1775 $verbose -- chroot >&2
cd chroot
verbose "Changed working directory to \`$chroot'."

mkdir -m711 $verbose .host >&2
mkdir -m755 $verbose .in dev dev/pts >&2
chgrp "$gid1" $verbose . dev dev/pts >&2
mkdir -m1777 $verbose dev/shm >&2
mkdir -m1770 $verbose .out >&2
chgrp "$gid2" $verbose .out >&2
verbose 'Created directory tree.'

ln -s $verbose ../proc/self/fd dev/fd >&2
ln -s $verbose ../proc/self/fd/0 dev/stdin >&2
ln -s $verbose ../proc/self/fd/1 dev/stdout >&2
ln -s $verbose ../proc/self/fd/2 dev/stderr >&2
verbose 'Created /dev symlinks.'

install_static_helper ash sh
install_static_helper find
install_static_helper cpio
[ -z "$qemu_arch" ] ||
	install_static_helper "qemu-$qemu_arch" "qemu-${qemu_arch/armh/arm}"
postin="$HOME/.hasher/install/post"
if [ -r "$postin" -a -x "$postin" ]; then
	install -p -m755 $verbose "$postin" .host/postin >&2
fi
verbose 'Installed .host programs.'

"$makedev" ${number:+-$number} "$chroot" &&
	verbose "Created devices in \`$chroot/dev/'." ||
	fatal 'failed to make devices.'
