#!/bin/sh

# Copyright 2004 Sun Microsystems, Inc.	 All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)postbackout 1.3	04/08/03 SMI"

#
# Do special stuff like restarting the daemon processes after backing out the patch
#

#
# This portion of the postpatch script is the upgrade script from System1
# Note that upgrade / downgrade actions are the same.
# This script does the following:
#
#  1.  Pings the NTC using the service Processor hostname as a prefix to the
#      hardware component.  (e.g. ping sp0-ntc)
#  2.  Upload the firmware.
#      In the case of the NTC this means archiving any existing firmware and
#      copying the new firmware to /tftpboot
#  3.  Reboot the NTC
#  4.  Ping the NTC to make sure it is alive again.
#
#  Exit status is:
#     0   Successful completion
#    11   Could not contact NTC Initially
#    12   Firmware file for NTC not found
#    13   Copy to /tftpboot failed
#    14   Reboot failed
#    15   Could not PING after Reboot
#
# Editorial NOTE:  It still seems to me like this functionality should be
#                  provided in the patch postinstall script.  The upgraded
#                  firmware is provided by the patch and the patch postinstall
#                  does the appropriate config stuff.
#                  The patch postbackout script takes care of doing the reverse.
#                  The patch add and patch remove then can do everything with
#                  the cool side effect of having an "unlimited" number of
#                  firmware levels available.
#
PKGBASE=/opt
PKGNAME=SUNWsentcu

NTC=`hostname | cut -f1 -d_`
HN=`hostname`
IP=`grep "$HN " /etc/hosts | cut -f1 -d" "`
NTCTEMPIP=`grep $NTC-ntc /etc/hosts | cut -f1 -d" "`
NTCIP=${NTCTEMPIP:=`grep ${NTC}_ntc /etc/hosts | cut -f1 -d" "`}

 
#
#  Step 1 Ping the NTC
#
echo Pinging $NTCIP
ping $NTCIP
if [ $? -eq "0" ]; then
    echo $NTCIP found
else
    exit 11
fi

#
#  Step 2 Archive firmware & copy the new firmware
#         The stuff to archive is /tftpboot/scs400.sys
#         The new firmware has been delivered via patch and is  $PKGBASE/$PKGNAME/scs400.sys
#
#echo Archive old firmware
#if [ -f $PKGBASE/$PKGNAME/scs400.sys.0 ]; then
#    if [ -f $PKGBASE/$PKGNAME/scs400.sys.1 ]; then
#        if [ -f $PKGBASE/$PKGNAME/scs400.sys.2 ]; then
#            cp -p $PKGBASE/$PKGNAME/scs400.sys.1 $PKGBASE/$PKGNAME/scs400.sys.2
#        fi
#    else
#        cp -p $PKGBASE/$PKGNAME/scs400.sys.0 $PKGBASE/$PKGNAME/scs400.sys.1
#    fi 
#    cp -p /tftpboot/scs400.sys $PKGBASE/$PKGNAME/scs400.sys.0
#    if [ $? -eq "0" ]; then
#        echo ARCHIVE Done
#    else
#        echo ARCHIVE of old Firmware failed - Attempting to continue
#    fi
#else
#    if [ -f /tftpboot/scs400.sys ]; then
#        cp -p /tftpboot/scs400.sys $PKGBASE/$PKGNAME/scs400.sys.0
#        echo ARCHIVE - Done
#    else
#        cp -p $PKGBASE/$PKGNAME/scs400.sys $PKGBASE/$PKGNAME/scs400.sys.0
#        echo "Saving current Package config.sys"
#    fi
#fi

echo Copying old firmware
cp -p /opt/${PKGNAME}/scs400.sys /tftpboot
if [ $? -eq "0" ]; then
    echo scs400.sys copied
else
    echo scs400.sys FAILED
    exit 13
fi

echo Copying old config file
cp -p /opt/${PKGNAME}/config.sys /tftpboot
if [ $? -eq "0" ]; then
    echo config.sys copied
else
    echo config.sys FAILED
    exit 13
fi

#
#  Step 3  Reboot the NTC
#     Note the config.sys file in tftpboot is upgraded with the parameters below
#
echo Reboot the NTC
/usr/local/bin/ntc_config -d --new_ip $NTCIP --curr_ip $NTCIP --sp_ip $IP
if [ $? -eq "0" ]; then
    echo NTC rebooting - This takes about 3 minutes
else
    exit 14
fi
sleep 200


#
#  Step 4 Ping the NTC
#
echo Pinging $NTCIP
ping $NTCIP
if [ $? -eq "0" ]; then
    echo $NTCIP found
else
    exit 15
fi

exit 0

