#!/bin/sh
#
# Module:       startPmFwUpgrade
# Author:       Henrique Oliveira <henrique.oliveira@cyclades.com>
# Copyright:    (c) 1999-2003 Cyclades Corp.
#
#      This program is free software; you can redistribute it and/or
#      modify it under the terxms of the GNU General Public License
#      as published by the Free Software Foundation; either version
#      2 of the License, or (at your option) any later version.
####################################################################
# $Log: startPmFwUpgrade,v $
# Revision 1.1  2003/08/27 22:51:52  henrique
# First version of the software
#
# Revision 1.2  2003/08/26 18:37:15  henrique
# Now, when users use -f options, they are not asked if they want to stop
# the services on the serial port, the services are stopped without notice.
#
# Revision 1.1  2003/08/26 00:18:44  henrique
# First version of the file.
#
####################################################################

showHelp () {
    echo "This script does the upgrade of all Cyclades AlterPath-PM intelligent"
    echo "Power Distribution Units (IPDU) equipments using the software pmFwUpgrade."
    echo "This script will try to disable all activities on the serial port it"
    echo "receives as parameter, do the upgrade, and bring the serial port back"
    echo "to its original state."
    echo
    echo "It has the following syntax:"
    echo "    #startPmFwUpgrade <options> <file> <serial port number>"
    echo "    <options>:"
    echo "        -h:   Show this help message and exit"
    echo "        -f:   Do the upgrade without asking any question"
    echo "        -p <password>: use <password> to log into the PM unit"
    echo "    <file>: is the new firmware you want to upload to your PM unit"
    echo "    <serial port number>: the serial port where the PM unit is connected"
    echo
    echo "Note that this script will only disable Cyclades software running on the"
    echo "serial port. If you have a custom application running on the serial port"
    echo "you must disable it yourself or your upgrade may fail."
}

PASSWORD=""
FIRMWARE_FILE=""
SERIAL_PORT_NUMBER=""
FORCE=""

###########################
## Receiving all parameters
###########################
for parameter in $@
do
    if [ -z "$parameter" ]
    then
	break
    fi

    if [ "$parameter" = "-h" ]
    then
	showHelp
	exit
    elif  [ "$parameter" = "-f" ]
    then
	echo "Forcing installation"
	FORCE="-f"
    elif [ "$parameter" = "-p" ]
    then
	echo "Getting password"
	PASSWORD="waiting password"
    else
	if [ "$PASSWORD" = "waiting password" ]
	then
	    PASSWORD="-p $parameter"
	elif [ -z "$FIRMWARE_FILE" ]
	then
	    FIRMWARE_FILE=$parameter
	elif [ -z "$SERIAL_PORT_NUMBER" ]
	then
	    SERIAL_PORT_NUMBER=$parameter
	fi
    fi
done

#########################
## Checking firmware file
#########################
if [ -z "$FIRMWARE_FILE" ]
then
    echo "Not enough arguments"
    showHelp
    exit
fi

if [ ! -f "$FIRMWARE_FILE" ]
then
    echo "Error. $FIRMWARE_FILE is not a valid filename"
    showHelp
    exit
fi

##############################
## Checking serial port number
##############################
if [ -z "$SERIAL_PORT_NUMBER" ]
then
    echo "Not enough arguments2"
    exit
fi
SERIAL_PORT="/dev/ttyS$SERIAL_PORT_NUMBER"

if [ ! -c $SERIAL_PORT ]
then
    echo "Error. $SERIAL_PORT_NUMBER is not a valid serial port number"
    exit
fi

#######################################
## Stopping services on the serial port
#######################################
if [ "$(teste -p $SERIAL_PORT_NUMBER |grep protocol |cut -d':' -f 2)" = "ipdu" ]
then
    ps |grep pmd |grep -v "grep pmd" > /dev/null 2>@1
    if [ "$?" -eq 0 ]
    then
	if [ -z "$FORCE" ]
	then
	    echo "It seems that the Cyclades Power Management daemon is currently"
	    echo "controlling the serial port $SERIAL_PORT_NUMBER. In order to proceed"
	    echo "this script needs to stop the Power Management daemon while the new"
	    echo "firmware is transfered to the AlterPath-PM unit."
	    while true
	    do
		echo -e "\nPlease type 'y' if the Power Management daemon can be stopped now or"
		echo -e "type 'n' to keep the daemon running and stop the upgrade process: \c"
		read OPTION
		case $OPTION in
		    y)
			break;
			;;
		    n)
			exit;
			;;
		    *)
			;;
		esac
	    done 
   	fi
	killall -int pmd
	sleep 4
	PMD=1
    fi
else
    if [ -z "$FORCE" ]
    then
	echo "It seems that Cyclades cy_ras is currently controlling the serial"
	echo "port $SERIAL_PORT_NUMBER. In order to proceed this script needs to stop all"
	echo "cy_ras services on this port while the new firmware is transfered to the"
	echo "AlterPath-PM unit."
	while true
	do
	    echo -e "\nPlease type 'y' if cy_ras services on this port can be stopped now or"
	    echo -e "type 'n' to stop the upgrade process: \c"
	    read OPTION
	    case $OPTION in
		y)
		    break;
		    ;;
		n)
		    exit;
		    ;;
		*)
		    ;;
	    esac
	done    
    fi
	
    PMD=0
    CONFIG_FILE="/etc/portslave/pslave.conf"
    BACKUP_FILE="/etc/portslave/pslave.conf.pmbak"
	
    cp $CONFIG_FILE $BACKUP_FILE
    echo "s$SERIAL_PORT_NUMBER.tty" >> $CONFIG_FILE
    echo "s$SERIAL_PORT_NUMBER.protocol socket_server" >> $CONFIG_FILE
    signal_ras hup
fi

#########################
## Upgrading the firmware
#########################
pmFwUpgrade $FORCE $PASSWORD $FIRMWARE_FILE $SERIAL_PORT_NUMBER

######################
## Restarting services
######################
if [ $PMD -eq 1 ]
then
    pmd &
else 
    mv $BACKUP_FILE $CONFIG_FILE
    signal_ras hup
fi
