#!/bin/bash
###############################################################################
## postinstall                                                                #
###############################################################################
## Copyright 2002. Spinnaker Networks Inc.                                    #
## All rights reserved.                                                       #
##                                                                            #
###############################################################################
## This script is used to perform tasks that have to be done after            #
## installing the new version of software                                     #
###############################################################################

if [ ${CUR_MAJOR} -eq 1 ]; then

    DISK=`ls /proc/fcd/2* | head -n 1`
    if [ ! -z ${DISK} ]; then
	if [ -f ${SCRIPT_DIR}/lpconvert ]; then
	    DBLADE_ID=`cat /tmp/dbladeid | sed "s/\(.*{1\}\)\([0-9]\+\).*/\2/"`
	    rm -rf /tmp/dbladeid
	    if [ ! -z ${DBLADE_ID} ]; then
		echo "Performing lpconvert to convert LPs to Version 2"
		echo "${SCRIPT_DIR}/lpconvert -a -v2 -d${DBLADE_ID} /proc/fcd/2*"
		${SCRIPT_DIR}/lpconvert -a -v2 -d${DBLADE_ID} /proc/fcd/2* 
	    else
		echo "DBLADE_ID not set" >&2
		echo "lpconvert skipped" >&2
	    fi
	else
	    echo "Script ${SCRIPT_DIR}/lpconvert does not exist" >&2
	    echo "lpconvert skipped" >&2
	fi
    else
	echo "No disk; lpconvert skipped"
    fi
    
    #########
    ## Figure out which partition we have booted on
    #########
    BOOTPART=`mount -l -t ext2 | awk '/\[\/image1\]/ {print $3}'`
    if [ $BOOTPART == "/" ]; then
	ROOTEXT=1
	ALTEXT=2
    else
	ROOTEXT=2
	ALTEXT=1
    fi

    #########
    ## Set the default boot image
    #########
    echo "Setting image${ALTEXT} as the default boot image"
    
    if [ -f /tmp/lilo.conf ]; then
	rm -f /tmp/lilo.conf
    fi
    
    echo "default=image${ALTEXT}" > /tmp/lilo.conf
    cat /etc/lilo.conf | sed "s/default *=.*//" >> /tmp/lilo.conf
    
    echo "lilo -C /tmp/lilo.conf"
    lilo -C /tmp/lilo.conf
    
    rm -f /tmp/lilo.conf
fi

MoveDeviceFile ()
{
    OLD_DEVICE=$1
    NEW_DEVICE=$2

    if [ -f /altroot/etc/sysconfig/network-scripts/ifcfg-${OLD_DEVICE} ]; then
	mv /altroot/etc/sysconfig/network-scripts/ifcfg-${OLD_DEVICE} /tmp/ifcfg-${OLD_DEVICE}.old
	cat /tmp/ifcfg-${OLD_DEVICE}.old | sed "s/${OLD_DEVICE}/${NEW_DEVICE}/" >  /altroot/etc/sysconfig/network-scripts/ifcfg-${NEW_DEVICE}
	rm -f /tmp/ifcfg-${OLD_DEVICE}.old
    fi 
}

if [ ${CUR_MAJOR} -eq 2 ]; then

    # Create the /core directory
    if [ ! -d /altroot/core ]; then
	rm -f /altroot/core
	mkdir /altroot/core
    fi

    # Generate default SSL key & certificate
    if [ ! -f /altroot/var/spinmgmt/keys/spinmgmt.pem ]; then
	chroot /altroot /sbin/makekey -s
    fi
	
    # Remove the kernel 2.2.17 directory
    if [ -a /altroot/lib/modules/2.2.17-spinos ]; then
	rm -rf /altroot/lib/modules/2.2.17-spinos
    fi

    # Added spinvldb startup script to init
    if [ -f /altroot/etc/init.d/spinvldb ]; then
	chroot /altroot /sbin/chkconfig --add spinvldb
    fi

    #########
    ## If SpinServer 4100 and if eth0 is e1000, then need to move the device files
    ## 
    #########
    echo SPINSERVER_MODEL=${SPINSERVER_MODEL}
    if [ ${SPINSERVER_MODEL} == "S4100" ]; then
	CURRENT_ETH0_ALIAS=`cat /altroot/etc/modules.conf |  sed 's/#.*//' | grep "alias eth0" | sed 's/alias eth0 //'`
	echo CURRENT_ETH0_ALIAS=${CURRENT_ETH0_ALIAS}
	if [ ${CURRENT_ETH0_ALIAS} == "e1000" ]; then
	    mv -f /altroot/etc/modules.conf /altroot/etc/modules.conf.save
	    cat /altroot/etc/modules.conf.save | sed 's/alias eth0 .*/alias eth0 bcm5700/' > /altroot/etc/modules.conf

	    MoveDeviceFile eth2 eth1
	    MoveDeviceFile eth3 eth2
	    MoveDeviceFile eth4 eth3
	    MoveDeviceFile eth5 eth4

	    rm -f /tmp/rc.update

	    if [ -f /altroot/etc/rc.update ]; then
		cat /altroot/etc/rc.update | sed 's/.*modprobe bcm5700.*//' > /tmp/rc.update
	    fi

	    echo "" >> /tmp/rc.update
	    echo "modprobe bcm5700" >> /tmp/rc.update
	    echo "" >> /tmp/rc.update

	    cat /tmp/rc.update > /altroot/etc/rc.update

	    sync;sync;sync;
	fi
    fi
fi

sync; sync; sync;

exit 0
