#!/bin/sh -e
#
# $Id: rmchroot,v 1.19 2003/10/17 13:10:56 ldv Exp $
# Copyright (C) 2003  Dmitry V. Levin <ldv@altlinux.org>
# 
# The rmchroot 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
rmchroot - removes chroot created by mkchroot.

This program is free software, covered by the GNU General Public License.
rmchroot 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

check_helpers

cd chroot
Verbose "changed working directory to \`$chroot'"

install -pD -m755 $verbose /bin/ash.static .host/sh
install -pD -m755 $verbose /usr/bin/find.static .host/find
Verbose "installed .host programs."

if [ ! -c "dev/null" ]; then
	ugid1="$("$getugid1" $number)" ||
		Fatal "getugid1 failed."
	gid1="$(echo "$ugid1" |cut -d: -f2)"

	mkdir -p -m755 $verbose dev
	chgrp "$gid1" $verbose dev
	"$makedev" $number "$chroot" ||
		Fatal "failed to make devices."
	Verbose "created devices in \`$chroot/dev/'"
fi

cat >.host/entry <<\__EOF__
#!/.host/sh
chmod -Rf u+rwX -- /* 2>/dev/null
/.host/find / -delete >/dev/null 2>&1
exit 0
__EOF__
chmod 755 .host/entry

"$chrootuid2" $number "$chroot" /.host/entry &&
	Verbose "removed files owned by user2" ||
	Fatal "failed to remove files owned by user2."

"$chrootuid1" $number "$chroot" /.host/entry &&
	Verbose "removed files owned by user1" ||
	Fatal "failed to remove files owned by user1."

rm -rf $verbose -- "$chroot" ||
	Fatal "failed to remove some files."
