#!/bin/bash
#
# Copyright (c) 1998, 1999, 2000, 2002  by Marcus Brinkmann.
# Licensed under the GNU GPLv2 or later.
#
# Parts of this script was taken from the SETUP script in Hurd CVS. 

# native-install
#
#    1 Jan 1999 - v0.0 <Marcus.Brinkmann@ruhr-uni-bochum.de>
#        Original created by Marcus Brinkmann.
#   14 Jan 1999 - v0.1 Matthew Vernon <mcv21@cam.ac.uk>
#        Edited to deal with the removal of the /usr -> / symlink
#        This should work with or without the symlink
#   17 Feb 1999 - v1.0 Marcus Brinkmann <brinkmd@debian.org>
#        Create /etc/fstab.
#   27 Apr 1999 - v1.1 Marcus Brinkmann <brinkmd@debian.org>
#        Make it work with latest packages, libc needs awk configured.
#    5 Mar 1999 - v1.2 Marcus Brinkmann <brinkmd@debian.org>
#        We need /dev/fd early for calling #! scripts outside of our PATH.
#   13 May 1999 - v1.3
#        Add -v option to MAKEDEV, so we can watch progress.
#   17 May 1999 - v1.4
#        Shuffle around the order of initial commands. Esp, copy
#        the password file before invoking MAKEDEV fd, before
#        calling dpkg first time.
#        Also, insert special calls to dpkg for missing dependencies.
#   27 May 1999 - v1.5
#        Explain how to reboot.
#    1 Jun 1999 - v2.0
#        Next generation. We create three crash servers and link to the
#        default. Requires hurd-19990529 at least.
#        Make pfinet point to dummy interface, so localhost works. Does only
#        work with Marks patch to GNU Mach.
#   22 Jun 1999 - v2.1
#        Set passive translator for /servers/exec, it doesn't harm and will
#        be useful for people who boot a Hurd inside a file system.
#        Add force-depends to dpkg. The perl situation requires this for
#        dpkg and adduser.
#        Fix tmp permission. Why is it bogus?
#    7 Sep 1999 - v2.2
#        Set passive translator for /servers/password.
#        Don't configure mawk or passwd early.
#        Don't make use of dummy interface.
#        Remove libexec/runsystem before configuring hurd.
#   16 Oct 1999 - v2.3
#        Set up local network only.
#    1 Mar 2000 - v2.4
#	 Various updates.
#    2 May 2000 - v2.5
#        Make translator links hard.
#    1 Aug 2000 - v2.6
#        Going back to symbolic links for now; pfinet makes trouble.
#        Configure mawk, base-passwd, base-files, libdb2 before all others.
#        One run is enough now. Remove --force-depends.
#        Don't attempt to remove /var/mail.
#        Don't fix /tmp, it's okay.
#        [Kill sucking console process if it exists.] Commented out.
#    6 Mar 2001 -v 2.7
#        Add force depends to libc0.2 configuration because of "hurd".
#        Add force depends to dpkg configuration because of "sysvinit".
#   23 May 2002 -v 3.0
#        libc0.2 -> libc0.3
#        Patch to recognize new boot method fspart and fstype by
#        ams@kemisten.nu (Alfred M. Szmidt)
#   15 Aug 2002 -v 3.1
#        Install default pager.

# Make sure the filesystem is writeable
echo I am just checking if your filesystem is really writeable.
fsysopts / --writable

if [ ! -e /dev/console ] ; then

	# Set up standard passive translators
	echo
	echo Now I set up the translators. If you get Operation not supported
	echo errors, you have forgotten to set the file system owner to \"hurd\"!

	/bin/settrans -c /servers/socket/1 /hurd/pflocal
	/bin/settrans -c /servers/socket/2 /hurd/pfinet
	/bin/settrans -k /servers/exec /hurd/exec
	/bin/settrans -c /servers/crash-suspend /hurd/crash --suspend
	/bin/settrans -c /servers/crash-kill /hurd/crash --kill
	/bin/settrans -c /servers/password /hurd/password
	/bin/settrans -c /servers/default-pager /hurd/proxy-defpager

	# Not functional yet:
	/bin/settrans -c /servers/crash-dump-core /hurd/crash --dump-core
	if [ ! -e /servers/crash ] ; then
		/bin/ln -s crash-suspend /servers/crash
	fi
	if [ ! -e /servers/socket/local ] ; then
		/bin/ln -s 1 /servers/socket/local
	fi
	if [ ! -e /servers/socket/inet ] ; then
		/bin/ln -s 2 /servers/socket/inet
	fi

	# We need the password file to chown.

	if [ ! -e /etc/passwd ] ; then
		echo Copying passwd file.
		cp /usr/share/base-passwd/passwd.master /etc/passwd
	fi
	if [ ! -e /etc/group ] ; then
		echo Copying group file.
		cp /usr/share/base-passwd/group.master /etc/group
	fi

	# We need /dev/fd to call scripts.
	cd /dev
	/bin/sh ./MAKEDEV -v fd

	# Configure libc very soon. Ignore dependency on the Hurd.
	/bin/dpkg --force-depends --configure libc0.3

	# Configure packages with missing dependencies:
	if [ -e /var/lib/dpkg/info/bsdutils.list ] ; then
		/bin/dpkg --configure --force-depends bsdutils
	fi

	# Setup crucial devices
	echo
	echo The next job is to setup device files.
	cd /dev
	/bin/sh ./MAKEDEV -v std ptyp ptyq

	echo 'Now please reboot by pressing "reboot" and the enter key.'
	echo 'After reboot, run ./native-install a second time to complete'
	echo 'the installation.'
	exit
fi

# Make sure that the runsystem stuff will work
if [ -e /libexec/runsystem.gnu -a -e /libexec/runsystem ] ; then
  echo Removing /libexec/runsystem again, so the Hurd packages can
  echo take care about it.
  rm /libexec/runsystem
fi

# Debian set up.
echo
echo Configuring Debian packages.

# First configuring base-files and libdb2 helps a lot
dpkg --configure mawk
dpkg --configure base-passwd
dpkg --configure base-files
dpkg --configure libdb2
dpkg --force-depends --configure dpkg
dpkg --pending --configure

echo I just make sure that /libexec/runsystem is properly updated.
if [ ! -e /libexec/runsystem -a -e /libexec/runsystem.gnu ] ; then
  echo WHOA! We\'re in deep trouble. The Hurd package did not update
  echo the link in /libexec/runsystem. For now, I am making a link
  echo manually, but please report this.
  ln -s runsystem.gnu /libexec/runsystem
fi

echo The last step is to create /etc/fstab.

fstype=`fsysopts / | perl -ne 'm!^.*/hurd/(.*?)\.static.*?([^\s]+)\s*?$!; print $1;'`
rootpart=`fsysopts / | perl -ne 'm!^.*/hurd/(.*?)\.static.*?([^\s]+)\s*?$!; print $2;' | sed -e "s/^device\://"`

if [ ! -e /dev/${rootpart} ] ; then
	cd /dev
	/bin/sh ./MAKEDEV ${rootpart} || true
fi

if [ x${fstype} = xext2fs ] ; then
	fstype=ext2
fi

cat > /etc/fstab << __GNU__
# /etc/fstab: static file system information
#
# <file system>	<mount point>	<type>	<options>		<dump>	<pass>
__GNU__

if [ ! -e /dev/${rootpart} ] ; then
	echo Couldn\'t determine root partition, sorry.
	echo You have to edit /etc/fstab to match your system.
	echo However, for your convenience, I will install a dummy entry.
	echo "#/dev/hd0s1	/		ext2	rw			1	1" >> /etc/fstab
else
	echo Your root partition is /dev/${rootpart}.
	echo Your file system type is ${fstype}.
	echo If this information is not correct, you need to edit /etc/fstab.
	echo "/dev/${rootpart}	/		${fstype}	rw			1	1" >> /etc/fstab
fi

#proc=`ps ax | grep hurd/term | grep dev/console | sed -e 's/[a-z ]*\([0-9]*\) .*/\1/'`
#if [ ! -z $proc ] ; then
#	echo
#	echo Found dangling console server, which will suck up
#	echo every second character you type. That is a bug, and
#	echo I will kill this process for you.
#	kill $proc
#fi

echo
echo 'If you have a swap partition, please add it to /etc/fstab.'
echo 'You should reboot now, and enter multi-user mode. Then login as'
echo '"root" and set a password with "passwd".'
echo
echo 'Have fun exploring the Hurd, Marcus Brinkmann <brinkmd@debian.org>'
echo
