#!/bin/sh

echo `date`- Procedure to copy base mover system to a new mover node

if [ -n  "${1-}" ]; then 
  FROM=$1
else
  FROM=/fnal/enstore/movers
fi
SPECIAL=`dirname $FROM/special`

TO=/master
DEV=/dev/hda

echo `date`- DDing the boot sector from $FROM/partition_table.dd to $DEV
dd if=$FROM/partition_table.dd of=/$DEV bs=512

echo `date`- Running fdisk to update/sync the partition tables with kernel memory
fdisk /dev/hda << EOF
w
q
EOF

echo `date`- Running fdisk to determine min and max cylinder sizes for /dev/hda4
rm -f /tmp/FDISK
fdisk /dev/hda << EOF >>/tmp/FDISK
p
q
EOF

maxCyl=`grep cylinders /tmp/FDISK |grep heads|sed -e 's/.*, \([0-9]*\) cylinders*/\1/'`
x=`grep /dev/hda4  /tmp/FDISK`
minCyl=`echo $x|cut -f2 -d\ `

echo `date`- Running fdisk to use all of /dev/hda4 from $minCyl to $maxCyl
fdisk /dev/hda <<EOF
d
4
n
p
4
$minCyl
$maxCyl
p
q
EOF

echo `date`- Running mke2fs to make filesystem on ${DEV}4
mke2fs -m0 ${DEV}4

echo `date`- Running mkswap to create swap on ${DEV}{1,2,3}
mkswap ${DEV}1
mkswap ${DEV}2
mkswap ${DEV}3

echo `date`- Mounting ${DEV}4 onto $TO
mount ${DEV}4 $TO
mkdir $TO/proc

cd $TO

echo `date`- Extracting files from gzip tar file to `pwd`
gzip -d < $FROM/files.tgz | tar -xf -

echo `date`- Removing HOSTNAME
rm $TO/etc/HOSTNAME

echo `date`- Running static version of lilo to make sure boot sector is correct
$TO/usr/local/bin/lilo.static -r $TO

echo `date`- Creating ifcfg-eth1 file
cp $TO/etc/sysconfig/network-scripts/ifcfg-eth0 $TO/etc/sysconfig/network-scripts/ifcfg-eth1

echo `date`- Edit the $TO/etc/sysconfig/network-scripts/ifcfg-eth0 file
echo `date`- Edit the $TO/etc/sysconfig/network-scripts/ifcfg-eth1 file

echo `date`- OR    use tar and restore the special files form $SPECIAL
echo `date`-      " such as gzip -d < $SPECIAL/<nodename>.special.tgz | tar -xf -"
echo

dir $SPECIAL


echo `date`- THEN REBOOT
