#!/bin/sh -ef
#
# $Id: hsh-install,v 1.12 2005/10/09 15:30:46 ldv Exp $
# Copyright (C) 2003-2005  Dmitry V. Levin <ldv@altlinux.org>
# 
# The install 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

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

show_help()
{
	cat <<EOF
hsh-install - install binary packages into hasher chroot.

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

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

Options:
  --excludedocs             do not install documentation files;
  --hasher-priv-dir=DIR     hasher-priv directory;
  --number=NUMBER           subconfig identifier;
  --save-fakeroot           save fakeroot state;
  -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 excludedocs,hasher-priv-dir:,number:,save-fakeroot,help,quiet,verbose,version -- "$@"` ||
	show_usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		--excludedocs) exclude_docs=--excludedocs
			;;
		--hasher-priv-dir)
			hasher_priv_dir="$(opt_check_dir "$1" "$2")"
			shift
			;;
		--number)
			number="$(opt_check_number "$1" "$2")"
			shift
			;;
		--save-fakeroot) save_fakeroot=1
			;;
		--) shift; break
			;;
		*) parse_common_options hsh-install "$1"
			;;
	esac
	shift
done

# At least two arguments, please.
[ "$#" -ge 2 ] || show_usage 'Insufficient arguments.'

set_workdir "$1"
shift

check_helpers

[ -d "$chroot" ] || Fatal "$chroot: cannot find chroot."
[ -d "$aptbox" ] || Fatal "$aptbox: cannot find aptbox."
[ -r "$APT_CONFIG" ] || Fatal "$APT_CONFIG: cannot access APT configuration."

if [ "$PROG" = hsh-install ]; then
	lock_workdir
fi

filelist="$(print_uris "$@")" ||
	Fatal 'Failed to generate package file list.'
if [ -z "$filelist" ]; then
	Verbose 'Nothing to install.'
	exit 0
fi

copy_chroot_incoming $filelist

check_tty

create_entry_fakeroot_header
cat >>"$entry" <<__EOF__
${exclude_docs:+export RPM_EXCLUDEDOCS=1}
rpmi -i $rpm_verbose $hash $exclude_docs $(for f in $filelist; do printf %s "\"$(quote_arg "${f##*/}")\" "; done)
ldconfig
if [ -x /usr/sbin/adjust_kernel_headers ]; then
	/usr/sbin/adjust_kernel_headers --first
fi
if [ -x /.host/postin ]; then
	/.host/postin
fi
__EOF__

chrootuid1 &&
	Verbose 'Packages installation complete.' ||
	Fatal 'Packages installation failed.'

purge_chroot_in
update_RPM_database $filelist

