#!/bin/sh -e
#
# $Id: mkchroot,v 1.17 2004/08/08 09:44:10 ldv Exp $
# Copyright (C) 2003, 2004  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.
#

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

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

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;
  -V, --version             print program version and exit;
  -h, --help                show this text.
EOF
	[ -n "$1" ] && exit "$1" || exit
}

TEMP=`getopt -n $PROG -o q,v,V,h -l number:,hasher-priv-dir:,quiet,verbose,help,version -- "$@"` || Usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		--number) shift; number="${1##-}"
			[ -z "$(printf %s "$number" |tr -d [0-9])" ] ||
				Fatal "--number: $number: invalid number."
			number="-$number"
			;;
		--hasher-priv-dir) shift; hasher_priv_dir="$1"
			;;
		-q|--quiet) quiet=-q
			;;
		-v|--verbose) verbose=-v
			;;
		-V|--version) print_version mkchroot
			;;
		-h|--help) Usage 0
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
	shift
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="$(printf %s "$ugid1" |cut -d: -f2)"
ugid2="$("$getugid2" $number)" ||
	Fatal "getugid2 failed."
gid2="$(printf %s "$ugid2" |cut -d: -f2)"

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

mkdir -m711 $verbose .host
mkdir -m755 $verbose .in dev dev/pts
chgrp "$gid1" $verbose . dev dev/pts
mkdir -m1770 $verbose .out
chgrp "$gid2" $verbose .out
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/'"
