#!/bin/sh -e
#
# $Id: install,v 1.14 2003/12/04 08:35:51 ldv Exp $
# Copyright (C) 2003  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.
#

. /usr/share/hasher/functions

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

This program is free software, covered by the GNU General Public License.
install comes with ABSOLUTELY NO WARRANTY, see license for details.

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;
-h, --help              show this text.
EOF
	[ -n "$1" ] && exit "$1" || exit
}

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

while :; do
	case "$1" in
		--number)
			shift
			number="${1##-}"
			[ -z "$(echo -E "$number" |tr -d [0-9])" ] ||
				Fatal "--number: $number: invalid number."
			number="-$number"
			shift
			;;
		--hasher-priv-dir) shift; hasher_priv_dir="$1"; shift
			;;
		-q|--quiet) quiet=-q; shift
			;;
		-v|--verbose) verbose=-v; shift
			;;
		-h|--help) Usage 0
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
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 "$@")"
if [ -z "$filelist" ]; then
	Verbose "nothing to install."
	exit 0
fi

purge_chroot_in
purge_chroot_out
for f in $filelist; do
	install -p -m644 $verbose -- "$f" chroot/.in/
done

check_tty

cat >"$entry" <<__EOF__
$entry_fakeroot_header

rpmi -i $rpm_verbose $hash $exclude_docs $(for f in $filelist; do echo -n "\"$(quote_arg "${f##*/}")\" "; done)
ldconfig
adjust_kernel_headers --first
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

