#!/bin/sh
# 2005-2006, 2012, 2016 (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

# 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 -b -i] [package name] [package file bin.rpm] ..."
function mygetopts()
{
name=${0##*/}
Descr="$name - install package in hasher / take shell"

phelp()
{
	echog "$Descr"
	echog "$Usage"
	echo
	echog "Options:"
	echog "   -i           initialize hasher"
	echog "   -b REPONAME  binary repository name (4.1, p5, t6 and so on)"
	echog "   -c           cleanup hasher"
	echog "   -q           quiet mode (don't run anything)"
	echog "   -r           run command"
	echog "   -o           log in as root user"
	echog "   -v           verbose"
	echog "   -X|-Y        allow run X program (set DISPLAY and install xauth)"
	echog "   -t           use test hasher dir (different to usual build dir)"
	echog "   -p NAME      use hasher dir with NAME postfix"
	echo
	echog "Extra options:"
	echog "   -a           auto test mode for post myhsh build checking"
	echog "   -d           print hasher dir only"
	echog "   -M??         binary repository name (obsoleted)"
	echog "   -x           allow run X program (obsoleted)"
}

while getopts :chitoadvqr:b:p:xXY opt; do
    case $opt in
	h) phelp; exit 0;;
	t) TESTMODE=1 ;;
	i) INITIALIZE=1 ;;
	c) CLEANUP=1 ;;
	a) AUTOMODE=1 ;;
	d) PRINTHASHERDIR=1 ;;
	q) QUIETMODE=1 ;;
	b) BINARYREPONAME=$OPTARG ;;
	r) RUNCOMMAND=$OPTARG ;;
	p) HASHERPOSTFIX=$OPTARG ;;
	X) ALLOWX=-X ;;
	Y) ALLOWX=-Y ;;
	x) ALLOWX=-Y ;;
	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

# see functions/alt:set_binaryrepo() for BINARYREPONAME
set_binaryrepo $MENV

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

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

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

if [ "$PRINTHASHERDIR" = "1" ] ; then
	echo $HASHERDIR
	exit
fi


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

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

SHELLHASHERARG="--mountpoints=/proc"

check_locking

UTILPKG=
if [ -n "$ALLOWX" ] ; then
	UTILPKG="$UTILPKG xauth fonts-ttf-liberation"
fi

if [ -n "$QUIETMODE" ] ; then
	docmd $HSH-install $HASHERDIR --wait-lock $UTILPKG $LISTRPMARGS $LISTNAMES || fatal "Error with install"
	echog "Exiting (quiet mode)..."
	exit
fi

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

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

if [ -n "$AUTOMODE" ] || [ -n "$ALLOWX" ] ; then
	UTILPKG="$UTILPKG xauth fonts-ttf-liberation"
	SHELLHASHERARG="$SHELLHASHERARG $ALLOWX"
else
	UTILPKG="$UTILPKG etersoft-build-utils"
fi

docmd $HSH-install $HASHERDIR --wait-lock $UTILPKG $LISTRPMARGS $LISTNAMES || fatal "Error with install"

if [ -n "$RUNCOMMAND" ] ; then
	docmd $HSH-run $HASHERDIR $SHELLHASHERARG $ROOTER --wait-lock -- $RUNCOMMAND
	exit
fi

echog "You are in hasher shell"
docmd $HSH-shell $HASHERDIR $SHELLHASHERARG $ROOTER --wait-lock --shell

if [ -n "$AUTOMODE" ] ; then
	# FIXME: do not remove on x86_64
	echo "Removing package for test preun scripts..."
	# use when eepm >= 1.3.0 will everywhere
	#docmd $HSH-run --rooter $HASHERDIR -- epm remove $LISTNAMES
	docmd $HSH-run --rooter $HASHERDIR -- rpm -e $(drop_pkg_extensions $LISTNAMES)
	echo "Cleanup hasher in $HASHERDIR..."
	docmd $HSH --cleanup-only $HASHERDIR
fi
