#!/bin/sh
#
# This script installs Mint KGMD
#
# This script is automatically executed by mint.cnf when mint1.11
# boots (mint.tos on the boot disk).
#
# The RAM-Disk just finished before the uncompress process, the Links
# are ready and corrects. A workable environent is already possible
#
# You already have the following binary files.
#
# sh, tar, gzip, cat, [, cp.
#
#
# trap "" 1 2 15
# trap error 0
#

# First we activate the cursor
echo -n e
# then we activate the auto-wrap
echo -n v

umask 022

PATH="/bin:/usr/etc" ; export PATH

# Fundamental process error
# One way or the other, it doesn't work.... Fixme!

error()
{
case $? in
	0)
		echo Perfect, Everything is good!
		;;
	*)
		echo Sometimes it doesn\'t work.
		echo The installation has not been completed.
		echo -n 'Please press RETURN...' ; read j
		exit 1
esac
}

cat << EOF

             Welcome in the installation of the
              English distribution of Knarf / MiNT

First, you need a Minix partition where the distribution will decompress.
This partition should be at least 32 Mo, this is the minimum to be able to
work.

Rem: The Backspace (^H) doesn't work as well as the line delete (^U).
Don't be scared, the mistakes will always be fixable.


EOF

#
# First the choice of the partition
#

echo What partition should be converted ?
echo Enter the disk letter of the partition without the colon.
echo The available letters start from disk C to P.
echo
echo

while [ -z "$MINIXFS" ] ; do
	echo -n 'Please enter a valid letter: ' ; read j

	case $j in
		[C-Pc-p])
			MINIXFS=$j
			;;
		*)
			echo incorrect choice!
			MINIXFS=""
	esac

	if [ ! -z "$MINIXFS" ] ; then # Nur wenn Eingabe sinnvoll.

		echo on the partition $MINIXFS: the following files are still present...
		ls -la /$MINIXFS

		echo
		echo Are you sure you want to convert the drive $MINIXFS: into a Minix
		echo partition? All the data of this partition will be gone forever!!
		echo
		echo The possible answers are Y\)es N\)o C\)ancel and !\)Shell-Escape.
		echo
		echo -n 'Your choice [y/N/c/!]: ' ; read j

		case $j in
			[Yy])
				echo Very good! We can start!
				;;
			[Cc])
				echo The installation won\'t be finnished.
				exit 1
				;;
			!)
				echo With the choice \'exit\' you can go backward in the installation process
				echo Then you have to give a new letter.
				/bin/sh
				MINIXFS=""
				;;
			*)
				echo No? Then choose an other one!
				MINIXFS=""
		esac
	fi # Sinnvolle Eingabe
done # $MINIXFS

#
# choice of the type for the filesystem.
#

echo
echo Do you want to install on the partition $MINIXFS a V1 filesystem or V2?
echo
echo In general, you\'ll choose the V2 filesystem, especially if
echo you want to convert a 64 Mb partition or bigger.
echo

while [ -z "$V" ] ; do
	echo -n 'Please, what filesystem do you choose [1 or 2]:' ; read j

	case $j in
		[12])
			V=$j
			;;
		*)
			echo Incorrect answer! Try again please!
	esac

done # Filesystemtyp

#
# Even if correct, we have to treat the command.
#

case $V in
	1)
		V=""
		;;
	2)
		V="-V"
		;;
	*)
		echo Unexpected error!
		exit 1
esac

#
# Possible number of characters for the filenames
#

echo
echo The Minix partition allows the use for long filenames.
echo You can even fix the maximal lenght for the filename.
echo The longer the filename will be the slower the disk access
echo And the bigger the space each file will take.
echo
echo
echo The maximum length of the filenames are calculated from this
echo formula 'max = (n*16)-2', where n is a power of 2.
echo
echo Thus we obtain the possible combinations:
echo  1:  14 characters
echo  2:  30 characters
echo  4:  62 characters
echo  8: 126 characters [this case wasn\'t anticipated]
echo
echo You must not choose 14 characters. In Fact you can only choose
echo 30 or 62 characters. To use 126 characters, you have to recompile
echo minix.xfs.
echo

while [ -z "$MAXLEN" ] ; do
	echo -n 'SVP choose the multiple n [1, 2 our 4]: ' ; read j

	case $j in
		[124])
			MAXLEN=$j
			;;
		*)
			echo Incorrect choice! Try again !
	esac
done # Lenght of the filename

#
# Process the parameters of the command line.
#

# MAXLEN="-n $MAXLEN"

#
# protection mechanism
#

echo
echo The installed partition can be protected of an access out of
echo Mint and MinixFS, \(for instance under SingleTOS\(w/o mint\)
echo or Mag!C\). For this we have to trick the GEMDOS in making him 
echo to believe that there are no fat input available.
echo Also, if you have a XHDI compatible harddisk driver
echo \(HDDRIVER or HuSHI\), ???It\'s unfortunate to not include
echo the parameter -P???.

echo -n 'Do you want to use the protection mechanism? [Y/n]: ' ; read j

case $j in
	[Nn])
		PROTECT=""
		;;
	*)
		PROTECT="-P"
esac

COMMANDLINE="-n $MAXLEN $V $PROTECT ${MINIXFS}:"

echo
echo Very good! Minit will be launch with the following parameters
echo minit $COMMANDLINE
echo
echo answer the next question only by pressing the key
echo \`y\' \(w/o RETURN\). The pressed key won\'t be displayed.
echo The initialisation of the partition will take a moment...
echo

minit $COMMANDLINE

error

echo
echo Where is the kit of the distribution\(Knarf german kit\)?
echo
echo Please indicate the directory like this /\<drive\>/\<directory\>.
echo
echo If the distribution kit is on the drive g: in the main
echo directory,  then indicate  "/g".
echo If the distribution kit is on the drive d: in the directory
echo download, then indicate "/d/download".
# echo
# echo A copy of the distribution kit on disks is not implemented yet
# echo
# echo If before the installation you don\'t have enough space
# echo on your Harddrive partition, then indicate "/a" ou "/b", the
# echo distribution kit on disk will be then copied on the Minix
# echo partition freshly created. [This option is not implemented, yet !]
echo

while [ -z "$INSTALL" ] ; do

echo -n 'SVP indicate the directory: ' ; read j

case $j in
	"/[ab]")
		echo The copy from a or b is too slow!.
		echo Please try again..
		;;
	*)
		if [ -f /${j}/basic.aa -o -f /${j}/basic.tgz ] ; then
			echo Very good, We can start!
			# Ein Slash zu viel schadet nicht.
			INSTALL="/${j}"
		else
			echo I don\'t find the Basic package in the directory $j!
			echo Please try again..
		fi
esac

done # $INSTALL

echo
echo Now the system base filestems will be installed on the chosen partition...
echo

tar -C /${MINIXFS} -xzpf /tmp/filesys.tgz

error

echo
echo Then the Basic distribution will be uncompressed without running anything...
echo

if [ -f ${INSTALL}/basic.aa -o -f ${INSTALL}/basic.tgz ] ; then
  echo It might take a moment...
  if [ -f ${INSTALL}/basic.aa ] ; then
    cat ${INSTALL}/basic.??  | point | gzip -cd | tar -C /${MINIXFS} -xvpf - > /${MINIXFS}/var/db/kgmd/basic.log 2> /${MINIXFS}/var/db/kgmd/basic.errlog
  else
    cat ${INSTALL}/basic.tgz | point | gzip -cd | tar -C /${MINIXFS} -xvpf - > /${MINIXFS}/var/db/kgmd/basic.log 2> /${MINIXFS}/var/db/kgmd/basic.errlog
  fi
  echo
  if [ -s /${MINIXFS}/var/db/kgmd/basic.errlog ] ; then
    echo
    echo Here are the errors while uncompressing:
    echo
    more /${MINIXFS}/var/db/kgmd/basic.errlog
    echo
    echo This information is in the file /${MINIXFS}/var/db/kgmd/basic.errlog
    echo Please, check.
    echo
  else
    echo Perfect, Everything\'s fine!
  fi
fi

# minit sets by default: 777

/${MINIXFS}/usr/bin/chmod 755 /${MINIXFS}

# Creation of the links and the new PATH.

RM="/${MINIXFS}/usr/bin/rm"
LN="/${MINIXFS}/usr/bin/ln -fs"
MV="/${MINIXFS}/usr/bin/mv"

$RM /bin /etc /home /usr /tmp

$LN /${MINIXFS}/usr/bin /bin
$LN /${MINIXFS}/etc     /etc
$LN /${MINIXFS}/home    /home
$LN /${MINIXFS}/usr     /usr
$LN /${MINIXFS}/tmp     /tmp
$LN /${MINIXFS}/var     /var

PATH='/bin:/usr/ucb:/usr/etc' ; export PATH

# It's possible that the archives links could be misplaced.
# This will be corrected here.

cd /${MINIXFS}/usr/bin

# $RM -f sh
$LN ash sh

# $RM -f ps
$LN ps.mint ps

# $RM -f nice
$LN nice.gnu nice

# $RM -f stty
# $LN stty.gnu stty

# $RM -f su
$LN su.bsd su

# New links placed

cd /${MINIXFS}/usr/ucb

# $RM -f finger
$LN finger.bsd finger

# $RM -f talk
$LN talk.bsd talk

echo
echo The installation of the part Basic was successful. Very good!

while [ -z "$OK" ] ; do

echo
echo Before the next step in the installation, we need to create the
echo configuration files. Please answer these following questions:
echo
echo
echo First, a \`normal\' user will be created, it won\'t have the
echo rights of the root, but with \`su\' it will. What will be the
echo Username for this account?
echo
echo The Username must be in lower case and must not exceed 8
echo caracters. It can not also start by \`-\'.
echo
echo -n 'Username: ' ; read j
echo

ACCOUNT=$j

echo What is the complete name of the user? Generally it will be
echo the first name then the last name.
echo
echo -n 'First name  : ' ; read i
echo -n 'Last name   : ' ; read j
echo

USERNAME="$i $j"

echo Now you have to choose a Hostname for this computer.
echo The Hostname must not exceed 7 characters.
echo The Hostname can, under Mint, be easily modified.
echo
echo -n 'Hostname: ' ; read j
echo

HNAME=$j

echo What is your boot drive\? The path is only needed for
echo configuration files, the installation won\'t touch this partition.
echo Please Give the letter of the boot drive in lower case without
echo the colon.
echo Generaly the drive \`c\' will be used.
echo
echo -n 'Boot partition: ' ; read j
echo

BOOT=$j

echo
echo Your data will be saved in the configuration files:
echo
echo
echo Accountname : ${ACCOUNT}
echo Realname    : ${USERNAME}
echo Hostname    : ${HNAME}
echo Boot Drive  : ${BOOT}

echo -n 'Is it correct [Y/n]?' ; read j

case $j in
	[Nn])
		OK=""
		;;
	*)
		OK="OK"
esac

done # OK=OK

MODEM1="# "
MODEM2="# "
SERIAL1="# "
SERIAL2="# "
MIDI="# "

# Sparen wir uns hier mal den tr...

if [ -c /dev/modem1 ] ; then
	echo interface /dev/modem1 found...
	MODEM1=""
fi

if [ -c /dev/modem2 ] ; then
	echo interface /dev/modem2 found...
	MODEM2=""
fi

if [ -c /dev/serial1 ] ; then
	echo interface /dev/serial1 found...
	SERIAL1=""
fi

if [ -c /dev/serial2 ] ; then
	echo interface /dev/serial2 found...
	SERIAL2=""
fi

if [ -c /dev/midi ] ; then
	echo interface /dev/midi found...
	MIDI=""
fi

# echo
# echo If you want to use the software CoNnect under KGMD, respect the
# echo notes in the file /${MINIXFS}/boot/multitos/mintkgmd.cnf.

for i in \
boot/multitos/mintkgmd.cnf.in \
etc/passwd.in \
etc/group.in \
etc/hostname.in \
etc/hosts.in \
etc/fstab.in
do

##ACCOUNT##  : Login name for the user
##USERNAME## : Real name for the user
##HNAME##	 : Local name for the host
##MINIXFS##  : Drive letter for the Minix filesystem
##MODEM1##	: Computer to modem1 "", otherwise "# "
##MODEM2##	: Computer to modem2 "", otherwise "# "
##SERIAL1##  : Computer to serial1 "", otherwise "# "
##SERIAL2##  : Computer to serial2 "", otherwise "# "

cat /$MINIXFS/$i | sed \
	-e "s/##ACCOUNT##/$ACCOUNT/g"\
	-e "s/##USERNAME##/$USERNAME/g"\
	-e "s/##HNAME##/$HNAME/g"\
	-e "s/##MINIXFS##/$MINIXFS/g"\
	-e "s/##MODEM1##/$MODEM1/g"\
	-e "s/##MODEM2##/$MODEM2/g"\
	-e "s/##SERIAL1##/$SERIAL1/g"\
	-e "s/##SERIAL2##/$SERIAL2/g"\
	-e "s/##MIDI##/$MIDI/g"\
	> /${MINIXFS}/`dirname $i`/`basename $i .in`

case $? in
	0)
		rm -f /${MINIXFS}/$i
		;;
	*)
		echo Sometimes it doesn\'t work!
		echo The installation will not be completed.
		exit 1
esac

done

#
# Links correction
#
if [ -d /${BOOT}/multitos ] ; then
	rm -f /${MINIXFS}/usr/multitos
	ln -s /${BOOT}/multitos /${MINIXFS}/usr/multitos
else
	echo Attention: /${BOOT}/multitos doesn\'t exist yet.
	echo Please place by hand the multitos directory link
	echo of your boot drive to /usr/multitos.
fi
#
# Change the name of Home Directory
#

mv /home/user /home/$ACCOUNT
chown $ACCOUNT /home/$ACCOUNT

echo
echo Now, the COMPILER part will be uncompressed...
echo

if [ -f ${INSTALL}/compiler.aa -o -f ${INSTALL}/compiler.tgz ] ; then
  echo This will take a little while...
  if [ -f ${INSTALL}/compiler.aa ] ; then
    cat ${INSTALL}/compiler.?? | point | gzip -cd | tar -C /${MINIXFS} -xvpf - > /${MINIXFS}/var/db/kgmd/compiler.log 2> /${MINIXFS}/var/db/kgmd/compiler.errlog.1
  else
    cat ${INSTALL}/compiler.tgz | point | gzip -cd | tar -C /${MINIXFS} -xvpf - > /${MINIXFS}/var/db/kgmd/compiler.log 2> /${MINIXFS}/var/db/kgmd/compiler.errlog.1
  fi
  echo
  #
  # Here is a list of some stupid errors "permission denied" ;(
  #
  grep -v permission /${MINIXFS}/var/db/kgmd/compiler.errlog.1 > /${MINIXFS}/var/db/kgmd/compiler.errlog.2
  if [ -s /${MINIXFS}/var/db/kgmd/compiler.errlog.2 ] ; then
    echo
    echo The following errors are in the file overwriten by the uncompressing process.:
    echo
    more /${MINIXFS}/var/db/kgmd/compiler.errlog.2
    echo
    echo All this is in the file /${MINIXFS}/var/db/kgmd/compiler.errlog.2.
    echo please check.
    echo
  else
	echo Perfect, Everything\'s fine!
  fi
else
  echo Hoppla! There\'s nothing to install here!
  echo Well, no Compiler. It\'s like you want....
fi

echo
echo Now the NET package will be uncompressed...
echo

# The following links will probably get deleted

$RM -f /${MINIXFS}/usr/ucb/finger
$RM -f /${MINIXFS}/usr/ucb/talk

if [ -f ${INSTALL}/net.aa -o -f ${INSTALL}/net.tgz ] ; then
  echo It might take a little while...
  if [ -f ${INSTALL}/net.aa ] ; then
    cat ${INSTALL}/net.??  | point | gzip -cd | tar -C /${MINIXFS} -xvpf - > /${MINIXFS}/var/db/kgmd/net.log 2> /${MINIXFS}/var/db/kgmd/net.errlog
  else
    cat ${INSTALL}/net.tgz | point | gzip -cd | tar -C /${MINIXFS} -xvpf - > /${MINIXFS}/var/db/kgmd/net.log 2> /${MINIXFS}/var/db/kgmd/net.errlog
  fi
  echo
  if [ -s /${MINIXFS}/var/db/kgmd/net.errlog ] ; then
    echo
    echo The following errors are in the overwritten files because of
    echo the uncompress process:
    more /${MINIXFS}/var/db/kgmd/net.errlog
    echo
    echo The description is in the file /${MINIXFS}/var/db/kgmd/net.errlog.
    echo Please check.
    echo
  else
    echo Perfect, Everything was fine!
  fi

  # Corrected Links ( again)

  ( cd /${MINIXFS}/usr/ucb ; $LN finger.net finger ; $LN talk.net talk )

  # For ncftp - This is probably not necessary...
  # As well in /etc/host FQDN should be there...
  # And /etc/domainname is also a thing to fill...

  cp /etc/hostname /etc/host

  # And here again, weil es im Artikel nicht steht.
  touch /etc/ppp/options

else
  echo Hoppla! There is nothing to install!
  echo Well ok, no Net. As you want...
fi

echo
echo Now, uncompress the Manual package...
echo

if [ -f ${INSTALL}/manual.aa -o -f ${INSTALL}/manual.tgz ] ; then
  echo This will take a little while...
  if [ -f ${INSTALL}/manual.aa ] ; then
    cat ${INSTALL}/manual.??  | point | gzip -cd | tar -C /${MINIXFS} -xvpf - > /${MINIXFS}/var/db/kgmd/manual.log 2> /${MINIXFS}/var/db/kgmd/manual.errlog
  else
    cat ${INSTALL}/manual.tgz | point | gzip -cd | tar -C /${MINIXFS} -xvpf - > /${MINIXFS}/var/db/kgmd/manual.log 2> /${MINIXFS}/var/db/kgmd/manual.errlog
  fi
  echo
  if [ -s /${MINIXFS}/var/db/kgmd/manual.errlog ] ; then
    echo
    echo The following errors are in the overwritten files because of
    echo the uncompress process:
    more /${MINIXFS}/var/db/kgmd/manual.errlog
    echo
    echo These errors are in /${MINIXFS}/var/db/kgmd/manual.errlog.
    echo Please, check it.
    echo
  else
    echo Perfect, Everything was fine!
  fi
else
   echo Hoppla! There\'s nothing to install!
   echo Well, no Manual. As you want...
fi

echo
echo Now, uncompressing the X11R5 package...
echo

if [ -f ${INSTALL}/x11r5.aa -o -f ${INSTALL}/x11r5.tgz ] ; then
  echo this will take a little while...
  if [ -f ${INSTALL}/x11r5.aa ] ; then
    cat ${INSTALL}/x11r5.??  | point | gzip -cd | tar -C /${MINIXFS} -xvpf - > /${MINIXFS}/var/db/kgmd/x11r5.log 2> /${MINIXFS}/var/db/kgmd/x11r5.errlog
  else
    cat ${INSTALL}/x11r5.tgz | point | gzip -cd | tar -C /${MINIXFS} -xvpf - > /${MINIXFS}/var/db/kgmd/x11r5.log 2> /${MINIXFS}/var/db/kgmd/x11r5.errlog
  fi
  echo
  if [ -s /${MINIXFS}/var/db/kgmd/x11r5.errlog ] ; then
    echo
    echo The following errors are in the overwritten files because of
    echo the uncompress process:
    more /${MINIXFS}/var/db/kgmd/x11r5.errlog
    echo
    echo This errors are in /${MINIXFS}/var/db/kgmd/x11r5.errlog.
    echo Please check.
    echo
  else
    echo Perfect, Everything\'s fine!
  fi
  cd /usr/lib
  # Why Evil comes here "file exists"??
  ln -fs ./X11/*.a . >> /${MINIXFS}/var/db/kgmd/x11r5.log 2>> /${MINIXFS}/var/db/kgmd/x11r5.errlog
else
  echo Hoppla! There\'s nothing to install!
  echo Well, no X11R5. As you want...
fi

cd /

#
# npasswd needs syslogd
#

syslogd ; sleep 2

echo
echo Now please, a password for root \(5 characters minimum which one in upper case\):
echo
passwd root
echo
echo And now for the user ${ACCOUNT} \(5 characters minimum which one in upper case\):
echo
passwd ${ACCOUNT}
echo
echo Now, please, go in the directory /${MINIXFS}/boot with cd
echo and there follow the instructions of \(KGMD.txt\) file
echo section 4 about your bootdrive ${BOOT}: copy.
echo
echo If needed. Delete the directory /${BOOT}/mint and remove all the
echo \*.xdd and \*.xfs of /${BOOT}.
echo
echo After this, you can reboot the computer \(reboot\).
echo

HOME="/home/root" ; export HOME

exec /bin/tcsh
