ss#!/bin/sh


# Grab the init.d library functions.
. ./init.d/LIB

function get_return()
{
	echo
	echo $use_escapes "Press return to continue: (type 'shell' for shell) \c"
	read i
	if [ "$i" = "shell" ]; then
		echo
		echo "Type 'exit' or control-D to resume installation"
		/bin/sh < /dev/tty > /dev/tty 2>&1
		echo
		echo "(continuing...)"
		echo
	fi
	echo
}

cat << !!MSG1!!


This script installs a complete system boot management package comprising
a System V like version of init, a number of scripts and a set of run
level definitions. These give you complete control over the configuration
of your system. e.g. what ports are enabled, whether or not TCP/IP is
running, whether or not NFS is running, whether or not you go straight
into xdm on boot up etc.

*BEFORE* you continue you should have carefully checked the inittab
in this directory to make sure it does what you want. In particular
you should make sure the getty entries have the correct arguments
for your system. Some gettys use different arguments. You can find
out what yours wants by looking at your existing /etc/inittab file.
Edit the inittab here if necessary.

This script has worked in the past but has been modified since. It is
not convenient to test a script like this much. It may not work. It may
render your system unbootable. You should have a backup, know what you
are doing and be prepared to *think*.

!!MSG1!!

echo $use_escapes "Are you sure you want to do this? (y/n or shell) \c"
read i
while [ "$i" = "shell" ]
do
	echo
	echo "Type 'exit' or control-D to resume installation"
	/bin/sh < /dev/tty > /dev/tty 2>&1
	echo
	echo "(continuing...)"
	echo
	echo $use_escapes "Are you sure you want to do this? (y/n or shell) \c"
	read i
done
if [ "$i" != "y" -a "$i" != "Y" ]; then
	exit
fi

echo
echo $use_escapes "Have you got some way of booting from floppy? (y/n or shell) \c"
read i
while [ "$i" = "shell" ]
do
	echo
	echo "Type 'exit' or control-D to resume installation"
	/bin/sh < /dev/tty > /dev/tty 2>&1
	echo
	echo "(continuing...)"
	echo
	echo $use_escapes "Have you got some way of booting from floppy? (y/n or shell) \c"
	read i
done
if [ "$i" != "y" -a "$i" != "Y" ]; then
	echo
	echo "Get a bootable disk. If there are any problems with this installation"
	echo "you are going to need it!"
	exit
fi

cat << !!MSG2!!

Ok, I'm trusting you...

First I'll install a few scripts and configure the basics...

!!MSG2!!

echo
echo "Setting up boot scripts /etc/bcheckrc, /etc/brc and /etc/rc"
echo "N.B. Some init/script setups use /etc/brc as a shutdown script. SYSV"
echo "uses /etc/brc and its sub-scripts to configure the system for"
echo "multi user use. Before changing anything read the documentation,"
echo "read the scripts and make sure you understand what is happening."
if [ -f /etc/bcheckrc ]; then
	echo "	Backing up /etc/bcheckrc to /etc/bcheckrc.old"
	cp /etc/bcheckrc /etc/bcheckrc.old
fi
if [ -f /etc/brc ]; then
	echo "	Backing up /etc/brc to /etc/brc.old"
	cp /etc/brc /etc/brc.old
fi
if [ -f /etc/rc -o -d /etc/rc ]; then
	echo "	Backing up /etc/rc to /etc/rc.old"
	cp /etc/rc /etc/rc.old
fi
cp etc/bcheckrc etc/brc etc/rc /etc/.
chmod 744 /etc/bcheckrc /etc/brc /etc/rc

get_return

echo
echo "Installing the init scripts in /etc/init.d"
if [ ! -d /etc/init.d ]; then
	echo "	Huh? You seem to have an /etc/init.d which isn't a directory?"
	echo "	I'll back it up as /etc/init.d.old"
	mv /etc/init.d /etc/init.d.old
fi
mkdir /etc/init.d > /dev/null 2>&1
chmod 755 /etc/init.d
for i in LIB clock console cron ctrlaltdel filesys iface modules syslog nfs \
	nfs-fs parallel print serial swap tcp usr
do
	if [ -f /etc/init.d/$i ]; then
		echo "	Backing up /etc/init.d/$i to /etc/init.d/$i.old"
		mv /etc/init.d/$i /etc/init.d/$i.old
	fi
	cp init.d/$i /etc/init.d/$i
	chmod 755 /etc/init.d/$i
done


get_return

echo
echo "Setting up scripts for system checks (/etc/bcheckrc.d/*) and"
echo "multi-user configuration (/etc/brc.d/*). If these directories"
echo "already exist they will by wiped out first. If you have anything"
echo "in them which is not linked elsewhere you had better move it first!"
echo "N.B. Some init/script setups use /etc/brc as a shutdown script. SYSV"
echo "uses /etc/brc and its sub-scripts to configure the system for"
echo "multi user use. Before changing anything read the documentation,"
echo "read the scripts and make sure you understand what is happening."

get_return

rm -fr /etc/bcheckrc.d /etc/brc.d
mkdir /etc/bcheckrc.d /etc/brc.d > /dev/null 2>&1
ln -sf init.d/modules /etc/bcheckrc.d/00modules
ln -sf init.d/ctrlaltdel /etc/bcheckrc.d/01ctrlaltdel
ln -sf init.d/parallel /etc/bcheckrc.d/02parallel
ln -sf init.d/serial /etc/bcheckrc.d/02serial
ln -sf init.d/iface /etc/bcheckrc.d/03iface
ln -sf init.d/usr /etc/bcheckrc.d/04usr
ln -sf init.d/clock /etc/bcheckrc.d/05clock
ln -sf init.d/console /etc/bcheckrc.d/05console

get_return

echo
echo "Setting scripts for each run level."
echo "I'll do this by wiping out anything in the /etc/rc?.d directories and"
echo "then installing things as I see fit. If you already had a SYSV style"
echo "init running and had created local scripts which you linked in to the"
echo "rc?.d directories you will need to remake the links later. If you"
echo "put scripts in an rc?.d directory without linking them to init.d they"
echo "will be *lost*! In this case you had better shell out now and do"
echo "something about them."

get_return

mkdir /etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rc4.d /etc/rc5.d > /dev/null 2>&1
chmod 755 /etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rc4.d /etc/rc5.d

rm -f /etc/rc0.d/* /etc/rc1.d/* /etc/rc2.d/* /etc/rc3.d/* /etc/rc4.d/* /etc/rc5.d/*

ln -fs /etc/init.d/cron /etc/rc0.d/K30cron
ln -fs /etc/init.d/nfs /etc/rc0.d/K50nfs
ln -fs /etc/init.d/rpc /etc/rc0.d/K55rpc
ln -fs /etc/init.d/nfs-fs /etc/rc0.d/K58nfs-fs
ln -fs /etc/init.d/print /etc/rc0.d/K59print
ln -fs /etc/init.d/tcp /etc/rc0.d/K60tcp
ln -fs /etc/init.d/swap /etc/rc0.d/K69swap
ln -fs /etc/init.d/filesys /etc/rc0.d/K70filesys
ln -sf /etc/init.d/syslog /etc/rc0.d/K31syslog

ln -fs /etc/init.d/cron /etc/rc1.d/K30cron
ln -fs /etc/init.d/nfs /etc/rc1.d/K50nfs
ln -fs /etc/init.d/rpc /etc/rc1.d/K55rpc
ln -fs /etc/init.d/nfs-fs /etc/rc1.d/K58nfs-fs
ln -fs /etc/init.d/tcp /etc/rc1.d/K60tcp
ln -fs /etc/init.d/swap /etc/rc1.d/K69swap
ln -fs /etc/init.d/filesys /etc/rc1.d/K70filesys
ln -sf /etc/init.d/syslog /etc/rc1.d/K31syslog

ln -fs /etc/init.d/nfs /etc/rc2.d/K50nfs
ln -fs /etc/init.d/rpc /etc/rc2.d/K55rpc
ln -fs /etc/init.d/nfs-fs /etc/rc2.d/K58nfs-fs
ln -fs /etc/init.d/tcp /etc/rc2.d/K60tcp
ln -fs /etc/init.d/filesys /etc/rc2.d/S20filesys
ln -fs /etc/init.d/swap /etc/rc2.d/S21swap
ln -sf /etc/init.d/syslog /etc/rc2.d/S22syslog
ln -fs /etc/init.d/cron /etc/rc2.d/S50cron

ln -fs /etc/init.d/nfs /etc/rc3.d/K50nfs
ln -fs /etc/init.d/rpc /etc/rc3.d/K55rpc
ln -fs /etc/init.d/filesys /etc/rc3.d/S20filesys
ln -fs /etc/init.d/swap /etc/rc3.d/S21swap
ln -sf /etc/init.d/syslog /etc/rc3.d/S22syslog
ln -fs /etc/init.d/tcp /etc/rc3.d/S30tcp
ln -fs /etc/init.d/nfs-fs /etc/rc3.d/S35nfs-fs
ln -fs /etc/init.d/cron /etc/rc3.d/S50cron

ln -fs /etc/init.d/filesys /etc/rc4.d/S20filesys
ln -fs /etc/init.d/swap /etc/rc4.d/S21swap
ln -sf /etc/init.d/syslog /etc/rc4.d/S22syslog
ln -fs /etc/init.d/tcp /etc/rc4.d/S30tcp
ln -fs /etc/init.d/nfs-fs /etc/rc4.d/S35nfs-fs
ln -fs /etc/init.d/rpc /etc/rc4.d/S35rpc
ln -fs /etc/init.d/nfs /etc/rc4.d/S40nfs
ln -fs /etc/init.d/cron /etc/rc4.d/S50cron

ln -fs /etc/init.d/filesys /etc/rc5.d/S20filesys
ln -fs /etc/init.d/swap /etc/rc5.d/S21swap
ln -sf /etc/init.d/syslog /etc/rc5.d/S22syslog
ln -fs /etc/init.d/tcp /etc/rc5.d/S30tcp
ln -fs /etc/init.d/nfs-fs /etc/rc5.d/S35nfs-fs
ln -fs /etc/init.d/rpc /etc/rc5.d/S35rpc
ln -fs /etc/init.d/nfs /etc/rc5.d/S40nfs
ln -fs /etc/init.d/cron /etc/rc5.d/S50cron

get_return

echo
echo "Creating system default files. Some of these will be setup by config"
echo "scripts which I'll run in a second. Others *will* require editing"
echo "*before* you reboot - things like /etc/default/swap spring to mind."
mkdir /etc/default > /dev/null 2>&1
chmod 755 /etc/default

for i in  clock iface modules nfs parallel serial swap tcp
do
	if [ -f /etc/default/$i ]; then
		echo "	Backing up /etc/default/$i to /etc/default/$i.old"
		mv /etc/default/$i /etc/default/$i.old
	fi
	cp default/$i /etc/default/$i
	chmod 644 /etc/default/$i
done

get_return

echo
echo "Next we'll install and run a few config scripts. These are installed"
echo "in /etc/config if you ever want to run them again."
if [ ! -d /etc/config ]; then
	echo "	Backing up /etc/config to /etc/config.old"
	mv /etc/config /etc/config.old
fi
mkdir /etc/config > /dev/null 2&1
chmod 755 /etc/config
for i in  console gateways interfaces name network time
do
	if [ -f /etc/config/$i ]; then
		echo "	Backing up /etc/config/$i to /etc/config/$i.old"
		mv /etc/config/$i /etc/config/$i.old
	fi
	cp config/$i /etc/config/$i
	chmod 755 /etc/config/$i
done

sh /etc/config/name
get_return
sh /etc/config/console
get_return
sh /etc/config/time
get_return
sh /etc/config/network
get_return

echo
echo "Have you checked inittab and edited the getty entries as appropriate"
echo $use_escapes "to your system? (y/n or shell) \c"
read i
while [ "$i" = "shell" ]
do
	echo
	echo "Type 'exit' or control-D to resume installation"
	/bin/sh < /dev/tty > /dev/tty 2>&1
	echo
	echo "(continuing...)"
	echo
	echo "Have you checked inittab and edited the getty entries as appropriate"
	echo $use_escapes "to your system? (y/n or shell) \c"
	read i
done
if [ "$i" != "y" -a "$i" != "Y" ]; then
	echo
	echo "DO IT NOW!!!!"
	echo
	echo "When you have done type exit (or control-D) to return to this script"
	/bin/sh
fi

echo
echo $use_escapes "Are you sure you could boot from floppy if necessary? (y/n or shell) \c"
read i
while [ "$i" = "shell" ]
do
	echo
	echo "Type 'exit' or control-D to resume installation"
	/bin/sh < /dev/tty > /dev/tty 2>&1
	echo
	echo "(continuing...)"
	echo
	echo $use_escapes "Are you sure you could boot from floppy if necessary? (y/n or shell) \c"
	read i
done
if [ "$i" != "y" -a "$i" != "Y" ]; then
	echo
	echo "If anything goes wrong you could be well stuffed (technical term)..."
fi

echo
echo $use_escapes "Are you absolutely sure you want to do this? (y/n or shell) \c"
read i
while [ "$i" = "shell" ]
do
	echo
	echo "Type 'exit' or control-D to resume installation"
	/bin/sh < /dev/tty > /dev/tty 2>&1
	echo
	echo "(continuing...)"
	echo
	echo $use_escapes "Are you absolutely sure you want to do this? (y/n or shell) \c"
	read i
done
if [ "$i" != "y" -a "$i" != "Y" ]; then
	echo
	echo "Installation aborted. Coward :-). Your system is in an intermediate state"
	echo "You will probably have to do quite a bit of work to restore it."
	exit
fi

echo
echo "Installing new version of init"
if [ -f /bin/init ]; then
	echo "	Backing up /bin/init to /bin/init.old"
	rm -f /bin/init.old
	mv /bin/init /bin/init.old
fi
if [ -f /etc/init ]; then
	echo "	Backing up /etc/init to /etc/init.old"
	rm -f /etc/init.old
	mv /etc/init /etc/init.old
fi
if [ -f /sbin/init ]; then
	echo "	Backing up /sbin/init to /sbin/init.old"
	rm -f /sbin/init.old
	mv /sbin/init /sbin/init.old
fi
cp bin/init /sbin/init
chmod 700 /sbin/init

get_return

if [ -d /sbin ]; then
	echo
	echo "Ah! You have an /sbin! I'll assume you keep your super user only"
	echo "programs there (or intend to) and put mine there."
	spath=/sbin
else
	echo
	echo "Hmmm... You don't have an /sbin directory for super user only programs."
	echo "I'll put mine in /bin..."
	spath=/bin
fi

get_return

if [ -x /etc/telinit ]; then
	rm -f /etc/telinit
fi
if [ -x /bin/telinit ]; then
	rm -f /bin/telinit
fi
if [ -x /sbin/telinit ]; then
	rm -f /sbin/telinit
fi
if [ "$spath" = "/sbin" ]; then
	echo "I'll put telinit in your /sbin directory..."
	ln -s /etc/init /sbin/telinit
else
	echo "I'll put telinit in your /etc directory..."
	ln -s /etc/init /etc/telinit
fi

get_return

ctrlaltdel=`pathof ctrlaltdel`
if [ -z "$ctrlaltdel" ]; then
	echo
	echo "If you want init to catch ctrl-alt-del and do a graceful shutdown"
	echo "and reboot you need the ctrlaltdel program from poe's admutils so"
	echo "the kernel can be told what we want to do when the system is booted."
	echo "I have one here :-). I'll install it for you in $spath."
	cp bin/ctrlaltdel $spath/ctrlaltdel
	chmod 700 $spath/ctrlaltdel
else
	echo "You already have ctrlaltdel in $ctrlaltdel - ok."
fi

get_return

adjtimex=`pathof adjtimex`
if [ -z "$adjtimex" ]; then
	echo
	echo "Linux assumes ticks are at exactly 100Hz bu default. This is not"
	echo "necessarily the case. The frequency is adjustable however. Simply"
	echo "enter the number of seconds gained per day (at the default of"
	echo "100Hz) in /etc/default/clock and the frequency will be adjusted"
	echo "automatically when you boot using adjtimex. Since you don't"
	echo "appear to have adjtimex I'll install one in $spath."
	cp bin/adjtimex $spath/adjtimex
	chmod 700 $spath/adjtimex
else
	echo "You already have adjtimex in $adjtimex - ok."
fi

get_return

echo
echo "There is a small helper program called netaddr which I use to figure"
echo "out the address of a network given a host address on the network"
echo "and, optionally, a netmask to use instead of the default. I will"
echo "install this in /bin"
if [ -f /bin/netaddr ]; then
	echo "	Backing up /bin/netaddr to /bin/netaddr.old"
	mv /bin/netaddr /bin/netaddr.old
fi
cp bin/netaddr /bin/netaddr

get_return

echo
echo "Installing the System V-a-like inittab from this directory"
echo "- which you have modified to match your system!"
echo "(If you haven't, shell out now and edit /etc/inittab!)"
if [ -f /etc/inittab ]; then
	cp /etc/inittab /etc/inittab.old
	cp inittab /etc/inittab
	chmod 644 /etc/inittab
fi

get_return

cat << !!MSG3!!


Run level 2 is multiuser but without TCP/IP running.
Run level 3 is multiuser with TCP/IP running.
Run level 4 is multiuser with TCP/IP and NFS running.
Run level 5 is multiuser with TCP/IP and NFS running and xdm on the console.

Little further documentation exists for this. If you are reasonably familiar
with the System V approach to init and run levels then you shouldn't
have any trouble. If you aren't at all familiar then you have three
choices - ask someone local to you who is, email someone who is (or may
be), or post to one of the many newsgroups. These are listed in order
of preference!

Note that the filesystem checks may be bypassed by using shutdown -f,
fastboot or fasthalt. These create a file called /fastboot before
shutting the system down. If this exists on booting the system comes
up as quickly as possible. These scripts understand this convention
but this is *not* a good idea generally!

Note also that the default behaviour is to clean /usr/tmp and /tmp and
to delete existing lock files when the system first goes multiuser.
Don't put things in there and expect them to survive reboots!

!!MSG3!!

get_return

cat < < !!MSG4!!

SHUTDOWN AND REBOOT YOUR SYSTEM NOW!

It should come up at run level 3 which is a full multiuser system
with all filesystems checked and mounted and TCP/IP up and running.

!!MSG4!!