#!/bin/sh -e
#
# $Id: install,v 1.22 2004/08/08 09:44:10 ldv Exp $
# Copyright (C) 2003, 2004  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

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

Usage()
{
	[ "$1" = 0 ] || exec >&2
	cat <<EOF
install - installs binary packages.

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

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

Valid options are:
  --number=NUMBER           subconfig identifier;
  --hasher-priv-dir=DIR     hasher-priv directory;
  -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.
EOF
	[ -n "$1" ] && exit "$1" || exit
}

TEMP=`getopt -n $PROG -o q,v,V,h -l number:,hasher-priv-dir:,quiet,verbose,help,version -- "$@"` || Usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		--number) shift; number="${1##-}"
			[ -z "$(printf %s "$number" |tr -d [0-9])" ] ||
				Fatal "--number: $number: invalid number."
			number="-$number"
			;;
		--hasher-priv-dir) shift; hasher_priv_dir="$1"
			;;
		-q|--quiet) quiet=-q
			;;
		-v|--verbose) verbose=-v
			;;
		-V|--version) print_version install
			;;
		-h|--help) Usage 0
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
	shift
done

# At least two arguments, please.
[ "$#" -ge 2 ] || Usage
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."

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

cat >"$entry" <<__EOF__
$entry_fakeroot_header

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__
chmod 755 "$entry"

"$chrootuid1" $number "$chroot" /.host/entry &&
	Verbose "packages installation complete." ||
	Fatal "packages installation failed."

purge_chroot_in
update_RPM_database $filelist

