#!/bin/bash
# /etc/rc used when MCC interim installation boots for the second time.

yes () {
 while [ 0 ];do
	echo -n " (Y or N): "
	read answer
	case $answer in
	y|Y|yes) return 0;;
	n|N|no) return 1;;
	*) ;;
	esac
 done; }

install_pkg () {
 cd /
 [ -z "$this" ] && this="boot or scsi-cd"
 if [ -z "$netinst" ];then netinst="f";fsyst='msdos ext2 minix'
 elif [ "$netinst" = nfs ];then fsyst='nfs'
 else fsyst='ext2 msdos minix';fi

 while [ 0 ];do
  if [ -z "$DEV" ];then
   echo "In what mounted directory are the MCC packages to be found?"
   read mnt;if [ -z "$mnt" -o ! -d "$mnt" ];then
    echo "That is not an accessible directory";return;fi
   netinst=t
  fi

  if [ "$netinst" = "nfs" ];then
   echo "What is the fully qualified domain name of the NFS server?"
   echo 'Press <RETURN> to install from Manchester.'
   read nfshost;[ -z "$nfshost" ] && nfshost=hpd.mcc.ac.uk
   if [ "$nfshost" = hpd.mcc.ac.uk ];then answer='/afs'
    mnt='/mnt/mcc/ftp/pub/linux/mcc-interim/2.0+'
   else echo "What is the pathname of the partition to mount?"
    echo -n "It should begin with '/': "
    read answer
    [ -z "$answer" -o "${answer#/}" = "$answer" ] && return
    mnt=''
   fi
   DEV="-o,rsize=8192,intr $nfshost:$answer"
  fi

  if [ "$netinst" = "f" ];then
   if [ -n "$mnt" ];then
    umount "$mnt" 2>/dev/null
    echo -e "\nYou may now remove the $this disk from drive $DEV.  If you wish"
    echo "to install from another disk, then put it in drive $DEV and"
    while [ 0 ];do
	echo -n "type 'r' when ready.  Otherwise type 'q' to stop: "
	read answer
	if [ -z "$answer" ];then continue;fi
	if [ "$answer" = "q" -o "$answer" = "Q" ];then return;fi
	if [ "$answer" = "r" -o "$answer" = "R" ];then break;fi
    done
   fi
  fi

  if [ "$netinst" != t ];then
   umount /mnt 2>/dev/null
   for i in $fsyst;do mount -r -t $i $DEV /mnt 2>/dev/null && break;done
   if [ "$?" != "0" ];then echo "Mounting $DEV on /mnt has failed."
	echo -n "Do you want to try again"
	if yes;then continue;fi
	return;fi
   if [ -z "$mnt" ];then
    echo "$DEV is mounted on /mnt.  In which directory are the packages found?"
    read mnt
    if [ -z "$mnt" -o ! -d "$mnt" ];then
	echo "That is not an accessible directory."
	if [ "$netinst" != t ];then echo "$DEV is still mounted on /mnt.";fi
	return;fi
   fi
  fi

  this="floppy"
  if [ -f "$mnt/debian/debian.tgz" ];then
   export dmnt="$mnt/debian"
   [ -f `echo "$dmnt/dpkg*.deb"` ] || \
    echo "Warning: the Debian dpkg package is not in $dmnt."
   [ -f `echo "$dmnt/bash*.deb"` ] || \
    echo "Warning: the Debian bash package is not in $dmnt."
   echo -n "Do you want to make this a purely Debian system?"
   if yes;then export prompt=n;
    /bin/sh /usr/sbin/mccinstall $mnt/debian/debian.tgz
    if [ "$netinst" != t ];then umount /mnt;fi;break
   fi
  fi
  if [ -d "$mnt/base_packages" ];then
   tmpfile="/tmp/Zzbootinstall.$$"
   rm -rf $tmpfile
   for j in base_packages programming extra_packages non_free contributions;do
    if [ -d "$mnt/$j" ];then ls $mnt/$j/*.tgz &>/dev/null && {
     answer=x
     until [ "$answer" != x ];do
      echo "Install packages from $j? (A = all, S = some, N = none, L = list)"
      read answer
      case "$answer" in
	a | A | all | All)	echo "$j n" >>$tmpfile;;
	s | S | some | Some)	echo "$j y" >>$tmpfile;;
	n | N | none | None)	;;
	l | L | list | List)	answer=x;(cd $mnt/$j;ls -d *.tgz);echo;;
	*) answer=x;;
      esac
     done }
    fi
   done
   if [ -f $tmpfile ];then
    j="$mnt/XFree86-3.1.2"
    if [ -d "$j" ];then
     answer=y
     k='X312bin X312cfg X312doc X312fnt X312man xf86conf'
     for i in $k;do [ -f "$j/$i.tgz" ] || answer=n;done
     if [ "$answer" = y ];then
	echo "The following basic X packages are available:"
	echo "     $k"
	echo "Note that you must still install the appropriate X server"
	echo "yourself, but I can install these packages if you wish."
	echo -n "Should I do this"
	if yes;then
	 (cd /usr/X11R6;for i in $k;do echo $i
	  gunzip <$j/$i.tgz|cpio -imudV -H ustar;done)
	 echo "Compressing man pages."
	 gzip -9f /usr/X11R6/man/man?/*x
	 df
	fi
     fi
    fi
    j="$mnt"
    while read answer prompt;do export mnt="$j/$answer" prompt
     for i in `(cd $mnt;echo *.tgz)`;do echo
	/bin/sh /usr/sbin/mccinstall $i </dev/console
    done;done <$tmpfile
    rm -rf $tmpfile;mnt="$j"
   fi
  else
   if [ -z "$prompt" ];then
    echo "If you have a prompt, you can choose whether to install each package"
    echo -n "or not.  Do you wish to have a prompt before each package"
    if yes;then prompt="y";else prompt="n";fi
   fi
   for i in `(cd $mnt;echo *.tgz)`;do echo
	/bin/sh /usr/sbin/mccinstall $i;done
  fi
  if [ "$netinst" != t ];then umount /mnt;fi
  if [ "$netinst" != f ];then break;fi
 done

 sync
 cd /
 echo -e
 if [ -d /backupdirs ];then
  find backupdirs -type d|xargs rmdir -p &>/dev/null;fi
 if [ -d /backupdirs ];then
  echo "Some of your old files have been saved in various directories under"
  echo "/backupdirs.  You should examine and delete them at your leisure."
 fi
}

PATH=/bin:/usr/bin
this='root or scsi-cd'
action="$1"
if [ "$0" = "/etc/init" ];then
 echo "$0"
 cd /
 cdrom=''

 if [ "$action" = ram0 ];then
  if [ -f boot/initrd.gz ];then
   echo "Do you want to install drivers for non-IDE, non-SCSI, non-ATAPI"
   echo "CD-ROMs?  If so, place the scsi-cd driver disk in the A drive and"
   echo -n " answer 'yes'; otherwise answer 'no':"
   if yes;then
    mount -n -r /dev/fd0 /mnt
    cp -p /mnt/cdrom.tgz /tmp
    sash -c 'umount /dev/fd0'
    this='scsi-cd'
   else cdrom=no
   fi
  fi
  echo "You may now remove the $this disk from the floppy drive and replace"
  echo -n "it with the boot disk.  Press <RETURN> when ready: "
  read junk
 else
  /sbin/swapon -a
  if [ -f /etc/kbd.conf ];then
	loadkeys /etc/kbd.conf
  fi
  echo "/sbin/fsck -A -av"
  /sbin/fsck -A -av
  mount -n -o remount,rw /
  /sbin/update &
  rm -f /etc/mtab* /var/run/utmp
  mount -avt nonfs
  >/var/run/utmp
 fi

 mount -r /dev/fd0 /mnt
 cp -p etc/init usr/sbin/bootinstall
 rm -rf boot/*.{b,old}
 if [ -f boot/vmlinuz ];then mv -f boot/vmlinuz{,.old};fi
 cp -p /mnt/{boot.b,vmlinuz} boot

 export mnt="/mnt" prompt="n"
 /usr/sbin/mccinstall base1.tgz
 stty sane
 echo "It is a good idea at this point to set the passwords for root and"
 echo -n "for the sample user name 'user'.  Do you wish to do this?"
 if yes;then
  echo "Set root's password:";passwd root
  echo "Set user's password:";passwd user
 fi

 if [ -f /mnt/tcpip1.tgz ];then
  echo -en "\nDo you wish to install networking software"
  if yes;then
   /usr/sbin/mccinstall tcpip1.tgz
   echo "You may configure networking even if you have no Ethernet card"
   echo "by selecting the default IP address 127.0.0.1.  If you have a"
   echo "supported Ethernet card, you need to know your real IP address,"
   echo "and you should know the address of your gateway machine and of"
   echo "a domain name server before you configure it.  If there is no"
   echo "gateway or name server machine, you may use 127.0.0.1 for it."
   echo -en "\nConfigure networking now"
   if yes;then /usr/sbin/install.net
    if [ -x etc/init.d/network ];then echo -en "\nStart networking now"
     if yes;then etc/init.d/network;etc/init.d/netbase start;fi
    fi
   else echo "The 'install.net' command will configure networking for you."
   fi
 fi;fi

 umount /mnt
 chmod 111 /mnt
 if [ ! -f boot/initrd.gz ];then
  echo -n "Do you want to install SCSI driver modules?"
  if yes;then echo -n "Do you want to install SCSI CD-ROM drivers?"
   if yes;then cdrom=yes;fi
   echo "Place the scsi-cd driver disk in drive 0 and press <RETURN>."
   read junk
   mount -r /dev/fd0 /mnt
   if [ "$cdrom" = yes ];then cp -p /mnt/cdrom.tgz /tmp;fi
   gunzip </mnt/scsi.gz >/tmp/scsi
   umount /dev/fd0
   /sbin/modprobe loop
   /sbin/losetup /dev/loop0 /tmp/scsi
   mount /dev/loop0 /mnt
   cp -pM /mnt/* lib/modules/KVER/scsi
   umount /mnt
   /sbin/losetup -d /dev/loop0
   /sbin/rmmod loop
   rm -f tmp/scsi
   mv -f lib/modules/KVER/{scsi,block}/xd.o
   mv -f lib/modules/KVER/scsi/linuxrc usr/libexec
   /sbin/depmod -a
  fi
 fi
 if [ -f tmp/cdrom.tgz ];then
  mv -f tmp/cdrom.tgz mnt
  /usr/sbin/mccinstall cdrom.tgz
  rm -rf mnt/*
 fi
 echo -e "\nYou may now remove the disk from the floppy drive."

 lilo=1
 if [ -x /usr/sbin/install.lilo ];then
  echo -e "\nThe LILO utility allows you to boot Linux from a hard disk."
  echo "It can give you the choice of booting one or more versions of Linux,"
  echo "or of booting one or more other operating systems.  LILO installs a"
  echo "boot program in any partition (a complex option, since you must set"
  echo "active flags properly), or in the Master Boot Record (a simple option),"
  echo "or on a floppy disk (a safe option, since you can test for problems)."
  echo -n "Would you like LILO to install a boot program now?"
  if yes;then lilo=2;fi
 fi
 if [ $lilo = 2 ];then
  /usr/sbin/install.lilo
  if [ "$?" != 0 ];then lilo=1;fi
 fi
 if [ "$lilo" = 1 ];then
  echo -e "\nYou can now make a boot floppy which will boot your newly"
  echo "installed Linux system.  To do this, you must put a formatted"
  echo "high density diskette into your A drive and answer 'y'."
  echo "Be sure you do not mind overwriting whatever is on the floppy!"
  echo "If you wish to do this now, put the disk in the drive and"
  echo -n "answer 'y'."
  if yes;then
   echo "If there are any boot-time options you need to give to the"
   echo "kernel, give them here; for example 'hdb=noprobe'.  Otherwise"
   echo 'just press <RETURN>.'
   read opts
   /sbin/mkfs.ext2 -i 16384 -m 0 /dev/fd0
   mount /dev/fd0 /mnt
   cp -pM boot/vmlinuz boot/boot.b /mnt
   rootdev=`rdev /boot/vmlinuz|sed -e 's/.* //'`
   cat >/mnt/lilo.conf <<EOF
install = /mnt/boot.b
compact
vga = normal
backup = /dev/null
map = /mnt/map
boot = /dev/fd0
image = /mnt/vmlinuz
  label = linux
  root = $rootdev
  read-only
EOF
   if [ -n "$opts" ];then echo "  append = \"$opts\"" >>/mnt/lilo.conf;fi
   if [ -f boot/initrd.gz ];then
    cp -pM boot/initrd.gz /mnt
    echo "  initrd = /mnt/initrd.gz" >>/mnt/lilo.conf
   fi
   /sbin/lilo -C /mnt/lilo.conf
   umount /mnt
  fi
 fi

 if [ "$action" = ram0 ];then
  exec /usr/sbin/bootinstall ramdisk
 else
  exec /usr/sbin/bootinstall delete
 fi
fi

cd /
mnt="/mnt"
netinst=''
if [ "$action" = ramdisk -o "$action" = delete ];then
 rm -rf /etc/init
fi

echo -e "\nWhich drive will you use to install Linux packages:"
while [ 0 ];do
 echo
 echo -e "\n1  drive A:, /dev/fd0"
 echo "2  drive B:, /dev/fd1"
 echo "3  A directory on an unmounted partition on this machine"
 echo "4  A directory on a machine accessable via NFS"
 echo "   (for this you must have networking installed and running)"
 echo "5  A directory already mounted at this time"
 echo -en "6  No installation at this time\n\n? "
 read answer
 case $answer in
 1) DEV=/dev/fd0;;
 2) DEV=/dev/fd1;;
 3) echo "What is the block device on which this partition lives?"
	read DEV;
	if [ -z "$DEV" ];then continue;fi
	if [ -b $DEV ];then mnt="";netinst="x"
	else echo "$DEV is not a block device.";continue;fi;;
 4) DEV=nfs;netinst=nfs;;
 5) DEV="";;
 6) echo "When you wish to install further packages, type 'bootinstall'."
	break;;
 *) continue;;
 esac
 export netinst mnt prompt='';install_pkg
 break
done

if [ "$action" = delete ];then
 killall -9 update 2>/dev/null
 sync
 umount -a
 /sbin/swapoff -a 2>/dev/null
 echo "You may now login as root or as user."
 echo "Press <RETURN> when you are ready."
 read answer
 exec /sbin/init auto
fi
exit 0
