#!/bin/sh
# Install partition table and Linux on web farm machines
disk="`/sbin/fdisk -l|egrep 'Disk /dev/.da'|sed -e 's,.*Disk /dev/,,' -e 's,:.*,,'`"
hda="/dev/${disk}"
umount /mnt
umount "$hda"1
umount "$hda"2
umount "$hda"3
umount "$hda"6
swapoff "$hda"5
mkswap "$hda"5
swapon "$hda"5
mkfs.ext2 "$hda"1
mkfs.ext2 "$hda"2
mkfs.ext2 "$hda"3
mount "$hda"2 /mnt || exit 1
if [ ! -d /mnt/lost+found ];then
  echo 'Cannot see /mnt/lost+found'
  exit 1
fi
echo "${hda}2 mounted."
rm -rf /mnt/boot /mnt/var
install -d -m 111 /mnt/boot /mnt/var
mount "$hda"1 /mnt/boot
if [ ! -d /mnt/boot/lost+found ];then
  echo 'Cannot see /mnt/boot/lost+found'
  exit 1
fi
echo "${hda}1 mounted."
mount "$hda"3 /mnt/var || exit 1
echo "${hda}3 mounted."
rm -f /mnt/boot/System.map-* /mnt/boot/config-* /mnt/boot/vmlinuz-* /mnt/boot/vmlinuz.old
for i in 01 02 03 04 05 06 07 08 09 10 11;do
 wget -q -O - http://debian.man.ac.uk/.web/farm$i;done|(cd /mnt;bunzip2|tar xifp -)

if [ ! -d /mnt/var/state ];then
  echo 'Untar of lwlx.bz2 failed.'
  exit 1
fi
mount -t proc proc /mnt/proc
cat /proc/mounts >/mnt/etc/mtab
/mnt/usr/sbin/rdev /mnt/boot/vmlinuz "$hda"2
cd /mnt
wget -O tmp/wfixup.tgz http://debian.man.ac.uk/.c/wfixup.tgz >/dev/null 2>&1
if [ -f tmp/wfixup.tgz ];then
  /usr/sbin/chroot /mnt /usr/sbin/mccinstall /tmp/wfixup.tgz
fi
/usr/sbin/chroot /mnt /usr/sbin/grub <<EOF1
if [ "$disk" = sda ];then
  /usr/sbin/chroot /mnt tar xivvzfpP /tmp/sda.tgz
fi
install (hd0,0)/grub/stage1 (hd0) (hd0,0)/grub/stage2 (hd0,0)/grub/menu.lst
quit
EOF1
sync
cp -p sbin/fsck.ext2 /sbin
cd lib
cp -p libext2fs.so.2 libcom_err.so.2 libuuid.so.1 /lib
rm -f /mnt/etc/mtab
cd /
umount /mnt/proc
umount /mnt/var || umount "$hda"3
umount /mnt/boot || umount "$hda"1
umount /mnt || umount "$hda"2
/sbin/tune2fs -c 0 -i 3m "$hda"2
/sbin/tune2fs -c 0 -i 3m "$hda"3
/sbin/tune2fs -j "$hda"2
/sbin/tune2fs -j "$hda"3
if [ -d /mnt/boot ];then
  echo "Unmount of ${hda}2 failed."
  exit 1
fi
fsck.ext2 -f "$hda"1
fsck.ext2 -f "$hda"2
fsck.ext2 -f "$hda"3
echo 'Installation completed successfully.'
echo 'Remember to check /etc/modules for the network device,'
echo ' and /etc/network/interfaces for sensible values!'
exit 0
