#!/usr/bin/ksh
#
#
# Copyright 2002, 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# SCCS Info:  @(#)downgrade	1.1     03/12/12 10:32:57 SMI
#
# This script installs a downgrade firmware file on the Netgear RP114 
# Firewall/Router.
#
PKGBASE=$INSTALL_BASE/opt
PKGNAME=SUNWsespfd
#
# Get the previous firmware file.  The firmware files are saved with the
# format:
#           ras OR ras.<backup number>
#
# Where ras is the current firmware file that is installed
#
# Examples:
#           ras ras.0 ras.1 ras.2
# 
# So from the example files above the behavior of this downgrade script
# would copy ras.2 to ras and then call the FW_config script.
#
prev_fwfile=`ls -1 $PKGBASE/$PKGNAME/ras.* | sort -k 1.5n | tail -1`
#
# Check to make sure the file is accessible
#
if [[ -r $prev_fwfile ]]; then
#
# Overwrite the current firmware file with the previous version.
# This file will be removed during the patchrm process and the
# previous version will be moved to the filename 'ras' which will
# then be the current firmware file.
#
# Rename because FW_config will do the downgrade and the options do not
# accept arguments.  The script expects the firmware file to live in
# /opt/SUNWsespfd and be named ras.
#
    if cp $prev_fwfile $PKGBASE/$PKGNAME/ras ; then
#
# Get hostname of the firewall.  Hostname will have a suffix of firewall
# and the prefix will be either the hostname of the service processor
# followed by a dash (-) OR if the service processor hostname is new_sp
# the prefix will be "new" followed by an underscore (_).  Examples:
#        Service Processor Hostname       Firewall Hostname
#        --------------------------       -----------------
#                new_sp                      new_firewall
#
#                sp1                         sp1-firewall
#
        prefix=`hostname | awk '{ if ( $1 ~ /new/ ) { print "new_" } else { print $1 "-" }}'`
        print "Firewall prefix: $prefix"
        fw_hostname=${prefix}firewall
        if ping $fw_hostname 1 > /dev/null 2>&1 ; then
            /usr/local/bin/FW_config -d --upgrade_firmware > /dev/null 2>&1
            if [[ $? -eq 0 ]]; then
#
# Make sure firewall is accessible after downgrade
#
                if ping $fw_hostname 1 > /dev/null 2>&1 ; then
#
# Successful firmware downgrade
#
                    exit 0;
                else
                    print "Firmware Downgrade Failed!  Could not contact firewall, $fw_hostname"
                    print "Follow documented manual recovery procedures"
                    exit 11
                fi
            else
                print "Firmware Downgrade Failed!"
                if [[ $? -eq 2 ]]; then
                    print "Firmware file for firewall does not exist"
                    exit 12
                else
                    print "Could not ftp the file to the firewall"
                fi
                print "Follow documented manual recovery procedures"
                exit 13
            fi
        else
            print "Could not contact firewall, $fw_hostname"
            exit 11
        fi
    else
        print "Error:  Firmware file cannot be transferred to staging area"
        exit 1
    fi
else
    print "Firmware file for firewall does not exist"
    exit 12
fi
