#!/bin/sh
#
# Copyright (c) 1998 - This script is hereby placed in the public domain
#                      by Marcus Brinkmann.
#
# dpkg-hurd - poor man's dpkg.
#
# Usage dpkg-hurd -i|--install package_*.deb [options]
#    or dpkg-hurd --unpack package_*.deb [options]
#    or dpkg-hurd -r|--remove package [options]
#    or dpkg-hurd --purge package [options]
#    or dpkg-hurd -s|-L package
#    or dpkg-hurd -l|-S  [pattern]
#
#    [options] are passed through to dpkp. A good example is
#              "--force-depends".
#
# Warning: This script is for bootstrapping a Debian/Gnu Hurd system
#          onto an empty ext2 partition.  The {pre,post}{inst,rem}
#          scripts are not run.
#
# Note:    The action must be the first argument, the package the second.
#          Multiple packages can't be processed at one command line.
#
# Change history:
#    19 Dec 1998 - v0.0 <Marcus.Brinkmann@ruhr-uni-bochum.de>
#        Original created by Marcus Brinkmann.
#    22 Dec 1998 - v0.1 Jeff Sheinberg <jeffsh@erols.com>
#        Error checking, automatic update of status file.
#    24 Dec 1998 - v0.2 Jeff Sheinberg <jeffsh@erols.com>
#        Remove by package name, fix bugs in name.list file creation.
#    TODO - add lockfile and trap exit & int, merge with deb_remove.
#    28 Dec 1998 - v0.3 Marcus Brinkmann <brinkmd@debian.org>
#        Extract package name from deb file, not from file name.
#        Rebuild package w/o install scripts and use dpkg directly.
#        Added -r and -p as well as -s,-S,-l,-L options.
#     1 Jan 1999 - v0.3.1 Marcus Brinkmann <brinkmd@debian.org>
#                  Added the possibility to add options.
#                - v0.3.2 Fixed typo in grep (Package: instead Package)
#    15 Apr 1999 - v0.3.3 Make it a bit more verbose (Pavel Roskin)
#                  Specify admindir explicitely to make it work on
#                  non-Debian systems. (patch by Pavel Roskin)
#    TODO - fix all those race conditions and use proper tmp dir!
#                - v0.3.3 Minor fix by Matthew Vernon <mcv21@cam.ac.uk>
#                  No longer expects the /usr -> / symlink
#    27 May 1999 - v1.0.1 More patches by Pavel Roskin for compatibility.
#                  Make use of case instead -o to test.
#                  Add --force-not-root.
#     1 Jun 1999 - v1.0.2 Patch by Kalle Olavi Niemitalo to make it work
#                  with packages which Contain the string "Package" in
#                  the description.
#     7 Sep 1999 - v1.0.3 Add --force-bad-path. Suggested by Espy.

set -u -e

PS4='+$LINENO: '

err ()
{
    echo "$0: $1" 1>&2
    exit 1
}

vld="var/lib/dpkg"
vldi="${vld}/info"
[ -d ./${vld} ] || \
  err "cd is \`$PWD', must be run from a dpkg root directory"
[ -d ./lost+found ] || \
  err "cd is \`$PWD', must be run from a partition root filesystem directory where lost+found resides"
#[ -L ./usr -a ./ -ef usr/ ] \
#  || err "cd is \`$PWD', must be run from a hurd root filesystem directory"

# [ "$#" == 2 ] || err "exactly two arguments, an action and a *.deb file or package name is required"

case ${1} in
"-i"|"--install"|"--unpack")

	action="${1}"
	file="${2}"
	name=`dpkg-deb --field ${file} Package`

	[ -f ${file} ] || err "file \`${file}' does not exist"

	# Construct directory hierarchie
	ctrl=temp_build/${name}/DEBIAN
	mkdir -m 0755 -p ${ctrl}

	# Extract the files from the .deb archive.
	dpkg-deb --extract ${file} ${ctrl}/..

	# Extract control info and create control files.
	dpkg-deb --control ${file} ${ctrl}

	# Move the package scripts out of the build dir.
	for f in preinst postinst prerm postrm
	do
	  if [ -e ${ctrl}/${f} ] ; then
	     mv ${ctrl}/${f} temp_build/
	  fi 
	done

	# Now repack the file.
	dpkg-deb --build temp_build/${name}

	# Move old prerm and postrm scripts out of the way.
	for f in prerm postrm
	do
	  if [ -e ${vldi}/${name}.${f} ] ; then
	     mv ${vldi}/${name}.${f} ${vldi}/${name}.${f}.backup
	  fi
	done

	shift
	shift
	dpkg --root=. --admindir=${vld} --force-architecture --force-not-root --force-bad-path ${action} ${@-} temp_build/${name}.deb

	# Move new package scripts into dpkg's repository
	for f in preinst postinst prerm postrm
	do
	  if [ -e temp_build/${f} ] ; then
	     mv temp_build/${f} ${vldi}/${name}.${f}
	  fi
	  if [ -e ${vldi}/${name}.${f}.backup ] ; then
	     rm ${vldi}/${name}.${f}.backup
	  fi
	done

	# Clean temp directory.
	rm -fR temp_build
	exit 0
	;;
esac

case ${1} in
"-r"|"--remove"|"--purge")

	action="${1}"
	name="${2}"

	# Move old prerm and postrm scripts out of the way.
	for f in prerm postrm
	do
	  if [ -e ${vldi}/${name}.${f} ] ; then
	     mv ${vldi}/${name}.${f} ${vldi}/${name}.${f}.backup
	  fi
	done

	shift
	shift

	dpkg --root=. --admindir=${vld} --force-architecture --force-not-root --force-bad-path ${action} ${@-} ${name}

	for f in prerm postrm
	do
	  if [ -e ${vldi}/${name}.${f}.backup ] ; then
	     rm ${vldi}/${name}.${f}.backup
	  fi
	done
	exit 0
	;;
esac
	
case ${1} in
"-s"|"-S"|"-l"|"-L")
	dpkg --root=. --admindir=${vld} "${@}"
	exit 0
	;;
esac


# dpkg-hurd - end of file.
