#!/bin/sh -e
#
# $Id: mkchroot,v 1.11 2003/12/04 08:34:08 ldv Exp $
# Copyright (C) 2003  Dmitry V. Levin <ldv@altlinux.org>
# 
# The 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

. /usr/share/hasher/functions

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

This program is free software, covered by the GNU General Public License.
mkchroot 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:
--number=NUMBER         subconfig identifier;
--hasher-priv-dir=DIR   hasher-priv directory;
-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 q,v,h -l number:,hasher-priv-dir:,quiet,verbose,help -- "$@"` || Usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		--number)
			shift
			number="${1##-}"
			[ -z "$(echo -E "$number" |tr -d [0-9])" ] ||
				Fatal "--number: $number: invalid number."
			number="-$number"
			shift
			;;
		--hasher-priv-dir) shift; hasher_priv_dir="$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

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

check_helpers

ugid1="$("$getugid1" $number)" ||
	Fatal "getugid1 failed."
gid1="$(echo "$ugid1" |cut -d: -f2)"

mkdir -m1775 $verbose -- chroot
cd chroot
Verbose "changed working directory to \`$chroot'"

mkdir -m711 $verbose .host
mkdir -m755 $verbose .in dev
mkdir -m1777 $verbose .out
chgrp "$gid1" $verbose . dev
Verbose "created directory tree."

install -p -m755 $verbose /bin/ash.static .host/sh
install -p -m755 $verbose /usr/bin/find.static .host/find
install -p -m755 $verbose /usr/bin/cpio.static .host/cpio
postin="$HOME/.hasher/install/post"
if [ -r "$postin" -a -x "$postin" ]; then
	install -p -m755 $verbose "$postin" .host/postin
fi
Verbose "installed .host programs."

"$makedev" $number "$chroot" ||
	Fatal "failed to make devices."

Verbose "created devices in \`$chroot/dev/'"
