#!/bin/sh -e
#
# $Id: rmchroot,v 1.43 2005/08/01 12:33:56 ldv Exp $
# Copyright (C) 2003-2005  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.
#

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

show_help()
{
	cat <<EOF
rmchroot - remove hasher chroot created by mkchroot.

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

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

Valid options are:
  --hasher-priv-dir=DIR     hasher-priv directory;
  --number=NUMBER           subconfig identifier;
  -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 h,q,v,V -l hasher-priv-dir:,number:,help,quiet,verbose,version -- "$@"` ||
	show_usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		--hasher-priv-dir)
			hasher_priv_dir="$(opt_check_dir "$1" "$2")"
			shift
			;;
		--number)
			number="$(opt_check_number "$1" "$2")"
			shift
			;;
		--) shift; break
			;;
		*) parse_common_options rmchroot "$1"
			;;
	esac
	shift
done

# Exactly one argument, please.
[ "$#" -ge 1 ] || show_usage 'Insufficient arguments.'
[ "$#" -le 1 ] || show_usage 'Too many arguments.'

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:+-$number})" ||
		Fatal 'getugid1 failed.'
	gid1="$(printf %s "$ugid1" |cut -d: -f2)"

	mkdir -p -m755 $verbose dev
	chgrp "$gid1" $verbose . dev
	"$makedev" ${number:+-$number} "$chroot" ||
		Fatal 'Failed to make devices.'
	Verbose "Created devices in \`$chroot/dev/'."
fi

cat >.host/entry <<__EOF__
#!/.host/sh${verbose:+ -x}
chmod -Rf u+rwX -- /* /.out 2>/dev/null
/.host/find / -delete >/dev/null 2>&1
exit 0
__EOF__
chmod 755 .host/entry

wlimit_time_elapsed=$wlimit_time_long wlimit_time_idle=$wlimit_time_long \
    chrootuid2 &&
	Verbose 'Removed files owned by user2.' ||
	Fatal 'Failed to remove files owned by user2.'

wlimit_time_elapsed=$wlimit_time_long wlimit_time_idle=$wlimit_time_long \
    chrootuid1 &&
	Verbose 'Removed files owned by user1.' ||
	Fatal 'Failed to remove files owned by user1.'

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