#! /bin/bash
#
# Copyright 1993 Thomas Heiling, Wuerzburg, Germany
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is 
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# As always, bug reports, suggestions, etc:
# phar006@rz.wrzv1.uni-wuerzburg.d400.de or
# heiling@rz.vax.uni-wuerzburg.d400.de
PATH=:/sbin:/bin:/etc:/usr/bin:/etc/lilo

#
#USER Konfiguration
#

MOUNTPOINT=/mnt
ROOTDEVICE=/dev/fd0
BLOCKS=1200
#Default for 5 1/4 Floppy
LIBRARY=libc.so.4.4.4
#Default to newest library
SH=ksh
#Default to the public domain Korn Shell
#If you have another small shell,change it.
#BEWARE: You must configure the .profile and etc/profile file

function copy()
{
	unalias $2
	Afile=`type -path $2`
	if [ "$Afile" = "" ]; then
		echo "I haven't found $2, which says `type $2`"
		echo -n "Please type the complete path for $2"
		echo " or Control-C to quit"
		read Afile
	fi
	if [ "$Afile" = "" ]; then
		echo "Skipping $2"
	else
		echo "Copying $2"
		cp $Afile $1
	fi
}

echo
echo "Welcome to MAKEROOT, a root disk creation utility."
echo
if [ "$1" = "" ]; then
	cat << EOF
	I need a formatted 5 1/4 disk in your boot drive $ROOTDEVICE !
	After making a minix Filesystem, i will mount the disk on $MOUNTPOINT.
	This is configured for a 5 1/4 Boot disk!
	
	If this is not OK hit control-C to end this script,
	and restart the script with your device,mountpoint 
	and Number of Block.
	You must reconfigure this script, if you are not using
	the simpleinit,agetty and normal passwd site.
	I assume that you have a copy of ksh ( Korn - Shell).

	Example: makeroot /dev/fd0h1200 /user
	 	 For a 3 1/2 disk use
	         makeroot /dev/fd0h1440 /user 1440
EOF
else
	ROOTDEVICE=$1
	MOUNTPOINT=$2
	if [ $# = 3 ]; then BLOCKS=$3;fi
fi
umask 022
echo
echo "Please insert a formatted floppy disk into your boot drive."
echo "This will be your new Linux root disk."
echo
echo -n "Press [enter] when ready."  
read REPLY;
echo
echo "Making a new minix filesystem on your disk $ROOTDEVICE"
mkfs.minix -n30 $ROOTDEVICE $BLOCKS
sync
echo "Mounting disk to $MOUNTPOINT"
mount -t minix $ROOTDEVICE $MOUNTPOINT
echo "Making required Directories"
cd $MOUNTPOINT
mkdir auto bin dev etc etc/lilo lib mnt proc tmp user usr
echo "Searching and copying required files for bin"
echo "I assume sh as ksh"
for i in cp gzip login mkdir mv passwd $SH rm
do
	copy $MOUNTPOINT/bin $i
done
cd bin
echo "Linking $SH to sh"
ln -sf $SH sh
echo "Copying your MAKEDEV Script to $MOUNTPOINT/dev"
cd $MOUNTPOINT/dev/
Afile=/dev/MAKEDEV
if [ ! -f $Afile ]; then
	echo "I haven't found /dev/MAKEDEV"
	echo -n "Please type the complete path "
	echo " or Control-C to quit"
	read Afile
fi
if [ "$Afile" = "" ]; then
	echo "Skipping MAKEDEV"
	echo "Sorry, but i need a script to generate the devices!"
	exit 1
else
	echo "Copying MAKEDEV"
	cp $Afile .
fi
echo "Creating devices in dev"
echo "If you have unusual devices you *must* generate it with mknod !!"
echo "Hit enter to continue"
read REPLY;
chmod +x MAKEDEV
MAKEDEV generic 
sync
cd $MOUNTPOINT
echo "Searching and copying required files for etc"
for i in fsck.minix agetty mkfs.minix simpleinit mount \
rdev shutdown swapon sync umount update hostname
do
	copy $MOUNTPOINT/etc $i
done
cd etc
echo "Making links for agetty, simpleinit and shutdown"
ln -sf agetty getty
ln -sf simpleinit init
ln -sf swapon swapoff
ln -sf shutdown reboot
ln -sf shutdown halt
while [ 0 ]; do
	echo "Are you using lilo ( Answer y or n )"
	read REPLY;
	if [ "$REPLY" = "n" -o "$REPLY" = "y" ]; then
		break;
	fi
done
if [ "$REPLY" = "y" ]; then
	echo "Copying lilo to your rootdisk"
	cd /etc/lilo
	for i in any_d.b mkck boot.b chain.b disktab lilo os2_d.b config map 
	do
		cp $i $MOUNTPOINT/etc/lilo
	done
fi
while [ 0 ]; do
	echo "Are you using $LIBRARY ( Answer y or n )"
	read REPLY;
	if [ "$REPLY" = "n" -o "$REPLY" = "y" ]; then
		break;
	fi
done
if [ "$REPLY" = "n" ];then
	echo "Enter your library ( e.g. libc.4.4.1 )"
	read LIBRARY;
fi
echo "Copying necessary Libraries"
cd $MOUNTPOINT/lib
cp /lib/$LIBRARY .
ln -sf $LIBRARY libc.so.4
if [ -f /lib/ld.so ]; then
	cp /lib/ld.so .
fi
cd $MOUNTPOINT/auto
echo "Copying compressed tools" 
for i in cat chown chmod df doshell killall loadkeys ls ln more rmdir vi \
fdisk mkfs.ext2 mkfs.xiafs fsck.ext2 fsck.xiafs tune2fs
do
	unalias $i
	Afile=`type -path $i`
	if [ "$Afile" = "" ]; then
		echo "I haven't found $i, which says `type $i`"
		echo "Please type the complete path for $i,"
		echo "Enter to Skip,or Control-C to quit"
		read Afile
	fi
	if [ "$Afile" = "" ]; then
		echo "Skipping $i"
	else
		echo "Copying and compressing $i"
		gzip -cv9 $Afile > `basename $Afile`.gz
	fi
done

cd $MOUNTPOINT/etc
echo "Enter the complete path for your Keytables or Enter to skip"
read REPLY
if [ -f $REPLY ]; then
	gzip -c9v $REPLY > keys.map.gz 
else
	echo "You are using no Keytables"
fi

echo "Copying your termcap"
cp /etc/termcap .
chmod 644 termcap

echo "Creating Startup Files"

echo "Creating etc/brc"
cat > brc << EOF
killall -KILL
echo "Unmounting all Filesystems"
umount -a
EOF
chmod 744 brc

echo "Creating etc/issue"
echo "Rescue Disk by Thomas Heiling!" > issue
chmod 644 issue

echo "Creating etc/motd"
echo "Welcome to Linux !" > motd
chmod 644 motd

echo "Creating etc/fstab"
cat > fstab << EOF
/dev/fd0	/		minix	defaults
/proc		/proc		proc	defaults
/dev/ram	/usr		minix	defaults
EOF
chmod 644 fstab

echo "Creating etc/shells"
echo "/bin/sh" > shells
chmod 644 shells

echo "Creating etc/group"
cat > group << EOF
system::0:
daemon::1:
bin::2:
mem::3:
kmem::3:
tty::4:
disk::5:
cron::15:
other::50:
EOF
chmod 644 group

echo "Creating etc/securetty"

cat > securetty << EOF
tty1
tty2
tty3
EOF
chmod 755 securetty

echo "Creating etc/passwd"
echo "root::0:0:system admin:/:/bin/sh" > passwd
echo "guest::100:0:Guest:/:/bin/sh" >> passwd
chmod 644 passwd

echo "I assume you are using the simpleinit program"
echo "If not you *must* edit the inittab and rc file!!!!"
echo "Press Enter to continue"
read REPLY;

echo "Creating etc/inittab" 
cat > inittab << EOF
tty1:con80x60:/etc/getty 9600 tty1
tty2:con80x60:/etc/getty 9600 tty2
tty3:con80x60:/etc/getty 9600 tty3
tty4:con80x60:/etc/getty 9600 tty4
EOF
chmod 755 inittab

echo "Creating etc/profile"
cat > profile << EOF
PATH=/bin:/etc:/usr/bin:/auto:.
TERM=console
PS1='# '
PS2='> '
ignoreeof=10
export PATH TERM PS1 PS2 ignoreeof
EOF
chmod 755 profile

echo "Creating etc/rc"
cat > rc << EOF
#!/bin/sh
# /etc/rc - generic system configuration script
PATH=/bin:/etc:/usr/bin:/sbin

# If we we shut down with "shutdown -f" don't check file systems.
if [ -f /fastboot ]
then
	echo "Fast boot, Skipping fsck" > /dev/console
	rm -f /fastboot
else
	echo "Checking file systems..." > /dev/console
	fsck.minix -a $ROOTDEVICE
    if [ $? -gt 1 ]
    then
        echo "Warning: fsck FAILED"
        echo "         Starting single user shell"
        echo "         Fix filesystem and REBOOT"
        /bin/sh -si < /dev/console > /dev/console 2>&1
    fi
fi

# Remount the root filesystem in read-write mode
echo "Remounting the ROOT File System"
mount -n -o remount $ROOTDEVICE /

# update syncs the filesystems every 30 seconds
# comment it out. if you use sysvinit.
update &

# remove /etc/mtab* so mount creates the /etc/mtab file
rm -f /etc/mtab* /etc/nologin

mkfs.minix -n30 /dev/ram $BLOCKS

# mount all partitions specified in /etc/fstab
mount -av -t nonfs
# mount swap partition specified in /etc/fstab
swapon -a

# tmp and usr may be mounted, so we do these rm's after mount
rm -f /usr/spool/uucp/LCK*
rm -rf /tmp/*

# remove /etc/utmp and touch it
rm -f /etc/utmp
echo -n > /etc/utmp

# to save some floppy disk space, we delete /etc/wtmp.
rm -f /etc/wtmp

if [ ! -f /etc/wtmp ]; then
  echo -n > /etc/wtmp
fi

/bin/sh /etc/rc.local
exit 0
EOF
chmod 755 rc

echo "Creating etc/rc.local"
cat > rc.local << EOF
mkdir /usr/tmp
mkdir /usr/bin
cp /auto/*.gz /usr/bin
gzip -dv /usr/bin/*.gz
hostname rescue 

if [ -x /etc/clock ]; then
	/etc/clock -u -s
fi

if [ -x /etc/syslogd ]; then
	syslogd  # turn on logging of su's, logins etc.
fi

if [ -x /etc/syslogk ]; then
	syslogk on > /usr/log/kernel &  # log all kernel messages.
fi

if [ -x /etc/rc.serial ]; then
	/etc/rc.serial
fi

if [ -f /etc/keys.map.gz ]; then
	gzip -cd /etc/keys.map.gz | loadkeys
fi
EOF
chmod 755 rc.local

echo "Creating .profile"
echo "BEWARE: This is for the Korn-Shell!"
cat > $MOUNTPOINT/.profile << EOF
alias -x m=more
alias -x h='fc -l'
alias -x al=alias
alias -x bye=exit
alias -x rmv=/bin/rm
alias -x rm='/bin/rm -i'
alias -x lc='ls -C'
alias -x ll='ls -l'
alias ls='ls -F'
alias which='type -path'

#stty erase \^h susp \^z kill \^u intr \^?
set -o trackall -o ignoreeof -o monitor

set -o emacs >/dev/null 2>/dev/null
bind '^I'=complete >/dev/null 2>/dev/null
bind '^A'=beginning-of-line >/dev/null 2>/dev/null
bind '^[['=prefix-2  >/dev/null 2>/dev/null
bind '^XA'=up-history >/dev/null 2>/dev/null
bind '^XB'=down-history >/dev/null 2>/dev/null
bind '^XC'=forward-char >/dev/null 2>/dev/null
bind '^XD'=backward-char >/dev/null 2>/dev/null
bind '^P'=up-history > /dev/null 2>/dev/null
bind '^N'=down-history >/dev/null 2>/dev/null
EOF

cd $MOUNTPOINT
echo "Setting up Owner and Permissions"
chown -R root.system auto bin etc usr
chmod -R 755 auto bin usr
chmod 777 tmp
echo "Installation Complete"
sync
#Wait for sync
sleep 5
sync
df
cd 
umount $ROOTDEVICE
echo "If you have enough space you can copy and compress other files to"
echo "the auto directory. ( e.g. tar or cpio )."
echo "Example : mount -t minix $ROOTDEVICE $MOUNTPOINT"
echo "          gzip -9cv /usr/bin/tar > $MOUNTPOINT/auto/tar.gz"
echo "Have fun. Thomas"
exit 0
