#!/bin/bash
###############################################################################
## cpaltroot                                                                  #
###############################################################################
## This script is used by spinmgmt to synchronize the alteranete              #
## RootFilesystem withe current RootFileSystem                                #
##                                                                            #
###############################################################################

#########
## Function: LogInfo & LogCritical
## Send a Info/Critical message to system logger
#########
LogInfo ()
{
    echo $*
    logger -p daemon.info -t cpaltroot -- $*
}

LogCritical ()
{
    echo $* >&2
    logger -p daemon.crit -t cpaltroot -- $*
}


#########
## Function: QuitOnError
## Displays given error message and exits the script
#########
QuitOnError ()
{
    LogCritical $*
    LogCritical "cpaltroot --- Stopped due to error ---"
    exit 1
}


#########
## Function: Interrupted
## Called when the script is interrupted
#########
trap 'Interrupted' INT QUIT TERM

Interrupted ()
{
    QuitOnError "cpaltroot --- Interrupted ---"
    exit 1
}

LogInfo "cpaltroot --- BEGIN ---"

#########
## Error check
#########
if [ ! -d /altroot ]; then
	QuitOnError "Error locating / and /altroot partitions"
fi


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

LogInfo "rootext=image${rootext}"
LogInfo "altext=image${altext}"


#########
## Clean /altroot
#########
LogInfo "Cleaning /altroot"
cd /altroot
files=`echo * | sed 's/ proc //'`
rm -rf $files  2>/dev/null


#########
## Copy the / tree to /altroot
#########
LogInfo "Copying the / tree to /altroot"
cd /
tar -cpS --one-file-system --exclude=lost+found . | 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

#########
## Remove the install log file
#########
LogInfo "Removing the install log file /altroot/var/spinmgmt/log/install.log"
rm -f /altroot/var/spinmgmt/log/install.log

#########
## Copy the appropriate boot & mount config files
#########
LogInfo "Copying the boot & mount config files"
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

#########
## Run LILO to update the boot configuration
#########
LogInfo "Running LILO to update the boot configuration"
lilo

#########
## Update the sync time information on the alternate RootFileSystem 
#########
LogInfo "Updating the sync time information on the alternate RootFileSystem"
ALT_SW_FILE="/altroot/etc/sysconfig/spinsoftware"
TMP_FILE="/tmp/spinsoftware.tmp"
FS_SYNCTIME=`date +"%a %b %d %T %Y"`
cat ${ALT_SW_FILE} | sed '/.*LAST_\(CFG_\)\?SYNCTIME=.*/d' > ${TMP_FILE}
echo "LAST_SYNCTIME=${FS_SYNCTIME}" >> ${TMP_FILE}
echo "LAST_CFG_SYNCTIME=${FS_SYNCTIME}" >> ${TMP_FILE}
mv ${TMP_FILE} ${ALT_SW_FILE} 


# Make sure that rc.update does not run again
LogInfo "Making sure that rc.update does not run again: rm -f /altroot/etc/rc.update"
rm -f /altroot/etc/rc.update

######
## cpaltroot is complete!!!
######
LogInfo "cpaltroot --- END ---"
exit 0
