#!/bin/sh -e
#
# $Id: hsh-shell,v 1.15 2005/08/01 12:33:55 ldv Exp $
# Copyright (C) 2003-2005  Dmitry V. Levin <ldv@altlinux.org>
# 
# The hasher shell.
#
# 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.
#

mountpoints=
x11_forwarding=
x11_timeout=60

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

show_help()
{
	cat <<EOF
hsh-shell - the hasher shell.

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

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

Valid options are:
  --hasher-priv-dir=DIR     hasher-priv directory;
  --mountpoints=LIST        comma-separated list of mount points;
  --number=NUMBER           subconfig identifier;
  --x11-timeout=SECONDS     how long X11 authorization will be valid;
  -x                        disable X11 forwarding;
  -X                        enable X11 forwarding;
  -Y                        enable trusted X11 forwarding;
  -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 x,X,Y,h,q,v,V -l hasher-priv-dir:,mountpoints:,number:,x11-timeout:,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
			;;
		--mountpoints) shift; mountpoints="$1"
			;;
		--number)
			number="$(opt_check_number "$1" "$2")"
			shift
			;;
		--x11-timeout) shift; x11_timeout="$1"
			;;
		-x) x11_forwarding=
			;;
		-X) x11_forwarding=untrusted
			;;
		-Y) x11_forwarding=trusted
			;;
		--) shift; break
			;;
		*) parse_common_options hsh-shell "$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

lock_workdir

prepare_x11_forwarding || x11_forwarding=

export mountpoints
use_pty=yes chrootuid2 /bin/runas -bash bash &&
	Verbose 'hasher shell finished.' ||
	Fatal 'hasher shell failed.'
