#!/bin/sh
# 2005-2006 (c) Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain
#
# Get shell in hasher with apt and mc
# -t - use test hasher
# -i - initialize hasher
# -M? - set target distro
# Public domain
# TODO: -r

# load common functions, compatible with local and installed script
. `dirname $0`/../share/eterbuild/functions/common
load_mod hasher rpm

SAVEOPT=$@

#############################

Usage="Usage: $name [-t -i -h -r -c] [-$CURRENTBRANCHNAME] bin.rpm..."
function mygetopts()
{
name=${0##*/}
Descr="$name - install package in hasher / take shell"

phelp()
{
	echog "$Descr"
	echog "$Usage"
	echog "Options:"
	echog " -i - initialize hasher"
	echog " -c - cleanup hasher"
	echog " -o - log in as root user"
	echog " -v - verbose"
	echog " -t - use test hasher dir (different to build hasher)"
	echog " -M?? - branch name"
	echog "Extra options:"
	echog " -a - auto test mode for post myhsh build checking"
	echog " -r - remote login to hasher"
}

while getopts :chitoav opt; do
    case $opt in
	h) phelp; exit 0;;
	t) TESTMODE=1 ;;
	i) INITIALIZE=1 ;;
	c) CLEANUP=1 ;;
	a) AUTOMODE=1 ;;
	r) REMOTE=1 ;;
	v) VERBOSE="-v" ;;
	o) ROOTER="--rooter" ;;
    +?) echog "$name: options should not be preceded by a '+'." 1>&2; exit 2;;
#    ?)  echog "$name: $OPTARG: bad option.  Use -h for help." 1>&2 ; exit 2;;
	?) OPTIND=$((OPTIND-1)); break;
    esac
done

# remove args that were options
if [ $# -gt 0 ]; then
	shift $((OPTIND - 1))
fi

LISTRPMARGS=$@
}

parse_cmd_pre "$@" --

mygetopts $LISTARGS

# export OURAPTCONF with temp. file contains correct path to sources.list
prepare_aptconfig

HASHERARG="$HASHERARG --apt-config=$OURAPTCONF --target $DEFAULTARCH --with-stuff"

set_hasherdir
if [ -n "$TESTMODE" ] || [ -n "$AUTOMODE" ] ; then
	HASHERDIR=$HASHERDIR-test
fi


if [ -n "$REMOTE" ] ; then
	check_ssh_key
	test -n "$BUILDSERVER" || fatal "Please set BUILDSERVER in config file"
	ssh -t $BUILDSERVER loginhsh ${SAVEOPT/-r/}
	exit
fi

if [ "$CLEANUP" = "1" ] ; then
	echog "Cleanup hasher in $HASHERDIR..."
	$HSH --cleanup-only $HASHERDIR $HASHERARG || fatal "cleanup"
	exit
fi

if [ ! -d $HASHERDIR ] || [ "$INITIALIZE" = "1" ] || [ -n "$AUTOMODE" ]; then
	echog "Initialize $HASHERDIR ..."
	mkdir -p $HASHERDIR || fatal "mkdir"
	$HSH $VERBOSE $HASHERARG --initroot-only $HASHERDIR || fatal "Cannot initialize hasher"
fi

SHELLHASHERARG="--mountpoints=/proc"

check_locking

echog "Prepare hasher in $HASHERDIR ..."
# there is package names in LISTRPMARGS
UTILPKG="mc"

# GNOME program needs dbus-tools-gui
if rpm --requires -p $LISTNAMES 2>/dev/null | grep -q dbus ; then
	echo "dbus using detecteed, add dbus-tools-gui"
	UTILPKG="$UTILPKG dbus-tools-gui"
fi

if [ -n "$TESTMODE$AUTOMODE" ] ; then
	UTILPKG="$UTILPKG XFree86-xauth fonts-ttf-liberation"
else
	UTILPKG="$UTILPKG etersoft-build-utils"
fi
$HSH-install $HASHERDIR --wait-lock $UTILPKG $LISTRPMARGS $LISTNAMES || fatal "Error with install"

echog "You are in hasher shell"
$HSH-shell $HASHERDIR $SHELLHASHERARG $ROOTER -Y --shell

if [ -n "$AUTOMODE" ] ; then
	# FIXME: do not remove on x86_64
	echo "Remove package for test preun scripts"
	$HSH-run --rooter $HASHERDIR -- rpm -e $(drop_pkg_extensions $LISTNAMES)
	echo "Cleanup hasher in $HASHERDIR..."
	$HSH --cleanup-only $HASHERDIR
fi
