#!/bin/bash

START=`date`

echo ################################################
echo # JIM'S MAGIC INSTALL SCRIPT!                  #
echo ################################################

# start up the portmapper
/sbin/portmap -i 127.0.0.1 -v

IP=`hostname`
HOSTNAME=`host $IP | awk '{print $5}' | sed 's/\..*//g'`

mount /proc

LOG=/mnt/logs/$IP/log

FILESERVER=10.0.0.1
LOGDIR=/opt/netinstall-logs
VICTIM_FS=/dev/sda2

## check to see if we have an "unknown" IP

THIRD_OCTET=`echo $IP | awk -F'.' '{print $3}'`

if [ $THIRD_OCTET -gt 189 ] ; then
	# register this host
	MAC=`ifconfig eth0 | awk '/HWaddr/ {print $5}'`	
	ssh 10.0.0.1 "/opt/netinstall/bin/addnode.sh $MAC; /etc/init.d/dhcpd restart"

	# reboot
	shutdown -r now
else
	echo "not doing fresh install"
fi


mount $FILESERVER:$LOGDIR /mnt/logs
mkdir /mnt/logs/$IP
mv /mnt/logs/$IP/log /mnt/logs/$IP/log.old
echo "boot started at $START for $HOSTNAME IP: $IP" | tee -a $LOG

echo "making tmp" 2>&1 | tee -a $LOG
mkfs -t ext2 /dev/ram1 2>&1 | tee -a $LOG
echo "mounting tmp" 2>&1 | tee -a $LOG
mount /dev/ram1 /tmp/ 2>&1 | tee -a $LOG
echo "done making and mounting tmp" 2>&1 | tee -a $LOG

ssh 10.0.0.1 "logger $HOSTNAME installation start"

#### set up disks
echo "Setting up partitions" | tee -a $LOG

echo "My Image:" | tee -a $LOG
ls -l /install_scripts/images/$HOSTNAME/ | tee -a $LOG


for i in `ls /install_scripts/images/$HOSTNAME/*sfdisk*` ; do 
	DISK=`basename $i |  sed 's/\..*//g'`

	echo "disk: $DISK" | tee -a $LOG

	# nuke any old DOS stuff
	echo "clearing disk of DOS garbage" | tee -a $LOG
	dd if=/dev/zero of=/dev/$DISK bs=512 count=1 | tee -a $LOG

	echo "contents:" | tee -a $LOG
	cat $i | tee -a $LOG
	sfdisk /dev/$DISK < $i 2>&1 | tee -a $LOG


	#### create filesystems
	echo "creating filesystems" | tee -a $LOG

	## XXX XXX XXX XXX
	for filesystem in sda1 sda2 ; do

		## build the right kind of filesystem
		if [ grep filesystem $i | grep Id=82 ] ; then 
			echo "making swap on $filesystem" | tee -a $LOG
			mkswap -c $filesystem | tee -a $LOG
		elif [ grep filesystem $i | grep Id=83 ] ; then 
			echo "making ext2 on $filesystem" | tee -a $LOG
			mkfs -t ext2 $filesystemi 2>&1 | tee -a $LOG
			if [ $? -ne 0 ] ; then
			    echo "Failed to make filesystem: $filesystem"
			fi
			sleep 10
			export VICTIM_FS=$filesystem
			echo "set victim fs to $VICTIM_FS" | tee -a $LOG
		fi
	done
        # Temp hack
	# mkswap -c /dev/sda1 | tee -a $LOG
        mkfs -t ext3 /dev/sda2 | tee -a $LOG
        export VICTIM_FS=/dev/sda2
	echo "done creating filesystems" | tee -a $LOG
done

## mount 'victim' filesystem on /mnt
echo "mounting victim filesystem: $VICTIM_FS on /mnt/target" | tee -a $LOG
# mount $VICTIM_FS /mnt/target | tee -a $LOG
mount $VICTIM_FS -o rw /mnt/target | tee -a $LOG
mount
df -h /mnt/target
sleep 5
ls /mnt/target
sfdisk -l /dev/sda
sleep 10
cd /mnt/target | tee -a $LOG

cd /

echo unpack tarballs | tee -a $LOG
for tarball in `ls  /install_scripts/images/$HOSTNAME/*.tar.gz` ; do 
	echo "unpacking $tarball" | tee -a $LOG
	cat $tarball | dd bs=512 | tar -C /mnt/target --numeric-owner --preserve --checkpoint -zxf - 2>&1 | tee -a $LOG
done

echo done unpacking tarballs | tee -a $LOG
sleep 10

cd /

echo setup ssh | tee -a $LOG
cp /install_scripts/ssh/${HOSTNAME}.rr.alliance.unm.edu/* /mnt/target/etc/ssh/ 

# Set up grub
echo "setting up grub" | tee -a $LOG
chroot /mnt/target grub-install hd0 2>&1 | tee -a $LOG
echo "done setting up grub" | tee -a $LOG

mkdir -p /mnt/target/nfs/scratch | tee -a $LOG
mkdir -p /mnt/target/media | tee -a $LOG

# set up pbs special directories
mkdir -p /mnt/target/var/spool/pbs/spool | tee -a $LOG
chmod 1777 /mnt/target/var/spool/pbs/spool | tee -a $LOG
mkdir -p /mnt/target/var/spool/pbs/undelivered | tee -a $LOG
chmod 1777 /mnt/target/var/spool/pbs/undelivered | tee -a $LOG

echo $HOSTNAME >/mnt/target/etc/HOSTNAME

echo setting up networking | tee -a $LOG
# set up networking

rm -f /mnt/target/etc/sysconfig/network/ifcfg-eth-id-*
rm -f /mnt/target/etc/sysconfig/network/ifcfg-myri-*
cat /dev/null > /mnt/target/etc/udev/rules.d/30-net_persistent_names.rules

MAC=`ifconfig eth0 | awk '/HWaddr/ {print $5}'  | tr '[:upper:]' '[:lower:]'`
echo got MAC for eth0 of: $MAC | tee -a $LOG
cat /install_scripts/images/$HOSTNAME/templates/ifcfg-eth.template | sed "s/IP_ADDRESS/$IP/g" > /mnt/target/etc/sysconfig/network/ifcfg-eth-id-$MAC | tee -a $LOG

MYRI_IP=`echo $IP | sed 's/10.0/10.1/g'`
echo got IP: $MYRI_IP for myrinet interface based on $MYRI_HOSTNAME | tee -a $LOG

cat /install_scripts/images/$HOSTNAME/templates/ifcfg-myri-bus-pci-0000\:0a\:01.0.template | sed "s/IP_ADDRESS/$MYRI_IP/g" > /mnt/target/etc/sysconfig/network/ifcfg-myri0

ALLIANCE_IPADDR=`host $HOSTNAME.alliance.unm.edu | awk '/has address/ {print $4}'`

SCAT_ROOT=/opt/scat/

echo /bin/expandtree /install_scripts/images/$HOSTNAME/templates/ / ALLIANCE_IPADDR=$ALLIANCE_IPADDR | tee -a $LOG
/bin/expandtree /install_scripts/images/$HOSTNAME/templates/ / ALLIANCE_IPADDR=$ALLIANCE_IPADDR | tee -a $LOG

echo "Done with installation???!!!" | tee -a $LOG
ssh 10.0.0.1 "/opt/scat/bin/nodeset $HOSTNAME boot" | tee -a $LOG

cd / | tee -a $LOG
sync ; sync ; sync | tee -a $LOG
umount /mnt/target | tee -a $LOG
umount /mnt/target | tee -a $LOG
sync ; sync ; sync | tee -a $LOG

shutdown -r -n now | tee -a $LOG


