#!/bin/bash

# Find the / and /altroot partitions then change the labels
mount | grep -q /dev/md 2>/dev/null
if [ $? -eq 0 ]; then
	DEVS=/dev/md[0-9]*
	SINGLE=0
else
        DEVS=/dev/sda[0-9]*
	SINGLE=1
fi
for i in $DEVS; do
	partname=`e2label $i 2>> /dev/null`
	if [ $? == 0 ]; then
		if [ $partname == '/' ]; then
			root1=$i
			e2label $i /image1
		fi
                if [ $partname == '/image1' ]; then
                        root1=$i
                fi
                if [ $partname == '/altroot' ]; then
                        root2=$i
                        e2label $i /image2
                fi
                if [ $partname == '/image2' ]; then
                        root2=$i
                fi
                if [ $partname == '/space1' ]; then
                        boot=$i
                fi
	fi
done


# Error check
if [ -z $root1 ] || [ -z $root2 ] || [ -z $boot ]; then
	echo "Error locating / and /altroot partitions"
	exit 1
fi


# Figure out which partition we have booted on
bootpart=`mount -l | awk '/\[\/image1\]/ {print $3}'`
if [ $bootpart == "/" ]; then
	rootext=1
	altext=2
else
	rootext=2
	altext=1
fi


# Remove the /dumppart filesystem from the fstab and use it as the dump device.
umount /dumppart
rmdir /dumppart
awk  '{ {if ($2 != "/dumppart") {print}}}' /etc/fstab > /tmp/fstab
mv /tmp/fstab /etc/fstab
parts=`sfdisk -d /dev/sda | cut -f1 -d:`
for part in $parts; do
        label=`e2label $part 2> /dev/null`
        if [ $label ]; then
                if [ $label == "/dumppart" ]; then
			swapoff /dev/vmdump
			mkswap /dev/vmdump
			swapon /dev/vmdump
                        ln -fs $part /dev/vmdump
                fi
        fi
done
/sbin/vmdump config

# Remove the /unused filesystem from the fstab
umount /unused
rmdir /unused
awk  '{ {if ($2 != "/unused") {print}}}' /etc/fstab > /tmp/fstab
mv /tmp/fstab /etc/fstab

# Fix up fstab
if [ $SINGLE -eq 0 ]; then
	cp /etc/fstab /etc/fstab.$rootext
	awk -f /sbin/fstab.awk root1=$root1 root2=$root2 < /etc/fstab > /etc/fstab.$altext
else
	awk  '{ {if ($1 != "LABEL=/" && $1 != "LABEL=/altroot") {print}}}' /etc/fstab > /tmp/fstab
	echo "$root1               /                       ext2    defaults        1 1" >  /etc/fstab.$rootext
	echo "$root2               /altroot                ext2    defaults        1 2" >> /etc/fstab.$rootext
	cat /tmp/fstab >> /etc/fstab.$rootext
        echo "$root2               /                       ext2    defaults        1 1" >  /etc/fstab.$altext
        echo "$root1               /altroot                ext2    defaults        1 2" >> /etc/fstab.$altext
        cat /tmp/fstab >> /etc/fstab.$altext
	cp /etc/fstab.$rootext /etc/fstab
fi


# Fix up lilo
/sbin/buildlilo ${boot:0:8} $root1 $root2


# Fixup release file
[ -f /etc/redhat-release.rh ] || cp /etc/redhat-release /etc/redhat-release.rh
cp /etc/redhat-release.rh /etc/redhat-release.1
echo "Boot image 1" >> /etc/redhat-release.1
cp /etc/redhat-release.rh /etc/redhat-release.2
echo "Boot image 2" >> /etc/redhat-release.2


# Make sure that this does not run again
rm -f /etc/rc.update


# Copy the / tree to /altroot
cd /
tar -cpS --exclude=proc --exclude=lost+found --exclude=nvram --exclude=altroot --exclude=space1 --exclude=core --exclude=var/log * | tar -x -C /altroot
mkdir -p /altroot/proc >> /dev/null
mkdir -p /altroot/nvram >> /dev/null
mkdir -p /altroot/altroot >> /dev/null
mkdir -p /altroot/space1 >> /dev/null
mkdir -p /altroot/core >> /dev/null
mkdir -p /altroot/var/log >> /dev/null
cp /etc/fstab.$rootext /etc/fstab
cp /etc/lilo.conf.$rootext /etc/lilo.conf
cp /etc/redhat-release.$rootext /etc/redhat-release
cp /altroot/etc/fstab.$altext /altroot/etc/fstab
cp /altroot/etc/lilo.conf.$altext /altroot/etc/lilo.conf
cp /altroot/etc/redhat-release.$altext /altroot/etc/redhat-release


# Setup boot map in /space1/boot
mkdir -p /space1/boot >> /dev/null
cp -f /boot/boot.b /space1/boot
cp -f /boot/chain.b /space1/boot
lilo

