#!/usr/bin/ksh
#
#
# Copyright 2002, 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident    "@(#)upgrade    1.4    03/12/12 SMI
#
# This script updates the firmware on the Netgear RP114 Firewall/Router
#
# 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 upgrade
#
        if ping $fw_hostname 1 > /dev/null 2>&1 ; then
#
# Successful firmware upgrade
#
            exit 0;
        else
            print "Firmware Upgrade Failed!  Could not contact firewall, $fw_hostname"
            print "Follow documented manual recovery procedures"
            exit 11
        fi
    else
        print "Firmware Upgrade 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
