#!/bin/sh
###############################################################################
## preinstall.sh                                                              #
###############################################################################
## Copyright 2002. Spinnaker Networks Inc.                                    #
## All rights reserved.                                                       #
##                                                                            #
###############################################################################
## This script is used to perform tasks that have to be done prior to         #
## Installing the new version of software                                     #
###############################################################################


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

######
## Obtain the Primary Dblade ID of the Spinserver
######
    if [ ! -z ${DBLADE_ID} ]; then
	echo "Using the DbladeID ${DBLADE_ID} specified. This id will be used to peform lpconvert"
	rm -rf /tmp/dbladeid
	echo ${DBLADE_ID} > /tmp/dbladeid
    else
	
	BASEDIR="/usr/spinmgmt/bin";
	export BASEDIR
	. ${BASEDIR}/../lib/functions;

	PID=`getParentPid "${HOMEDIR}/bin/Tomcat"`;
	RETVAL=$?

	if [ $RETVAL -eq 0 -a -n "$PID" ]; then

	    rm -rf /tmp/getdbladeid /tmp/gotdbladeid
	    echo "debug d-function show" > /tmp/getdbladeid
	    spinsh < /tmp/getdbladeid > /tmp/gotdbladeid
	    DBLADE_ID=`grep "${HOSTNAME}[ ]\+" /tmp/gotdbladeid | sed "s/\(.*${HOSTNAME}[ ]\+\{1\}\)\([0-9]\+\).*/\2/"`
	    rm -rf /tmp/getdbladeid /tmp/gotdbladeid

	    if [ ! -z ${DBLADE_ID} ]; then
		echo "DbladeID of host ${HOSTNAME} is ${DBLADE_ID}. This id will be used to peform lpconvert"
		rm -rf /tmp/dbladeid
		echo ${DBLADE_ID} > /tmp/dbladeid
	    else
		echo "Could not find DbladeID"; >&2
		exit 1
	    fi

	else
	    echo "spinmgmt is not running. Cannot find DbladeID when spinmgmt is not runing"; >&2
	    exit 1
	fi
    fi

######
## Stop all spin processes
######
    if [ -f /etc/init.d/spinmgmt ]; then
	echo "Stopping spinmgmt"
	echo "/etc/init.d/spinmgmt stop"
	/etc/init.d/spinmgmt stop
    fi

    if [ -f /etc/init.d/spinndmpd ]; then
	echo "Stopping spinndmpd"
	echo "/etc/init.d/spinndmpd stop"
	/etc/init.d/spinndmpd stop
    fi

    if [ -f /etc/init.d/spinsnmp ]; then
	echo "Stopping spinsnmp"
	echo "/etc/init.d/spinsnmp stop"
	/etc/init.d/spinsnmp stop
    fi

    if [ -f /etc/init.d/spinsyslog ]; then
	echo "Stopping spinsyslog"
	echo "/etc/init.d/spinsyslog stop"
	/etc/init.d/spinsyslog stop
    fi

    if [ -f /etc/init.d/spintapelockd ]; then
	echo "Stopping spintapelockd"
	echo "/etc/init.d/spintapelockd stop"
	/etc/init.d/spintapelockd stop
    fi

    if [ -f /etc/init.d/spinvldb ]; then
	echo "Stopping spinvldb"
	echo "/etc/init.d/spinvldb stop"
	/etc/init.d/spinvldb stop
    fi

    if [ -f /etc/init.d/spinfs ]; then
	echo "Stopping spinfs"
	echo "/etc/init.d/spinfs stop"
	/etc/init.d/spinfs stop
    fi

    if [ -f /etc/init.d/spinfskrb ]; then
	echo "Stopping spinfskrb"
	echo "/etc/init.d/spinfskrb stop"
	/etc/init.d/spinfskrb stop
    fi

    #
    # kill all the RDB user processes; make sure they're gone
    #
    echo "killall -9 Tomcat vldb hamgr"
    killall -9 Tomcat vldb hamgr

    if [ -f ${SCRIPT_DIR}/cpaltroot ]; then
	echo "Synchronizing Altroot"
	echo "${SCRIPT_DIR}/cpaltroot"
	${SCRIPT_DIR}/cpaltroot
    else
	echo "${SCRIPT_DIR}/cpaltroot does not exist" >&2
	exit 1
    fi
fi

#########
## Save the existing /altroot rdb configuration 
#########
echo "Saving the existing /altroot rdb configuration to /var/tmp/var_spinmgmt.tar"
(
  cd /altroot/var/spinmgmt;
  tar -cpSf /var/tmp/var_spinmgmt.tar --one-file-system --exclude=lost+found .
)

#########
## Copy the / tree to /altroot
#########
echo "Copying the / tree to /altroot using /sbin/cpaltroot"
/sbin/cpaltroot

#########
## restore the saved /altroot rdb configuration 
#########
echo "restoring the  /altroot rdb configuration from /var/tmp/var_spinmgmt.tar"
rm -rf /altroot/var/spinmgmt/*
tar -xf /var/tmp/var_spinmgmt.tar -C /altroot/var/spinmgmt
rm -rf /var/tmp/var_spinmgmt.tar 

#########
## Remove the contents of /altroot/var/tmp
#########
echo "Removing the contents of /altroot/var/tmp"
rm -rf /altroot/var/tmp/*

sync; sync; sync;

rpm --root /altroot -q ucd-snmp-utils >/dev/null
if [ $? -eq 0 ]; then
    echo "Removing the ucd-snmp-utils rpm"
    echo "rpm --root /altroot -e ucd-snmp-utils"
    rpm --root /altroot -e ucd-snmp-utils
fi

rpm --root /altroot -q ucd-snmp >/dev/null
if [ $? -eq 0 ]; then
    echo "Removing the ucd-snmp rpm"
    echo "rpm --root /altroot -e ucd-snmp"
    rpm --root /altroot -e ucd-snmp
fi


exit 0
