#!/bin/sh
##################################################################
# Name: /IBM/bin/lcmaStart
# Purpose: Starts one lcma session and logs lcma cout's to
#           /tmp/lcma/lcma.log
#          Then sends success or error trap to server with task oid
#          TCM gets log as needed using backup task
##################################################################

PATH=$PATH:/IBM/bin

# If doing a software update (exported by cf_InstallFlashImage.sh) ...
if [ "$DELTA_UPDATE" = "y" ]; then
	# Stop snmpd
        /etc/rc.d/init.d/snmpd stop notrap
	
	# Stop rebootd on N70 Software Update
        if [ "$MODEL_TYPE" = "N70" -o "$MODEL_TYPE" = "N50" -o "$MODEL_TYPE" = "BIOS" ]; then
                kill -9 `/sbin/pidof rebootd`
                rm -f /tmp/.rebootd
        fi
fi

# run lcma and log output, overwriting old log
echo "Update or Recovery of the Flash Card has been requested."
echo "Error messages will be logged to /var/log/lcma/client_task.log"
lcma $@ 2>/tmp/lcma/lcma.log

# Restart rebootd on N70 Software Update
if [ "$DELTA_UPDATE" = "y" ]; then
        if [ "$MODEL_TYPE" = "N70" -o "$MODEL_TYPE" = "N50" -o "$MODEL_TYPE" = "BIOS" ]; then
                /IBM/bin/rebootd &
        fi
fi

# get the lcma return value, for success or error trap
VAL=$?
if [ $VAL -eq 0 ]
then
	ENTERPRISE=3
else
	ENTERPRISE=4
fi

# get task id from -t parameter, if no t param then TASK_ID is empty
TASK_ID=`echo $@ | sed -n "s/.* -t \([^ ]*\).*$/\1/p"`

# get the url of the server to send the lcma logs from the -l parameter.
# If no -l parm the LOG_SERVER_URL is empty
#LOG_SERVER_URL=`echo $@ | sed -n "s/.* -l \([^ ]*\).*$/\1/p"`

# If snmpd is not running, then start it.
# snmpd is started here for the flash update case, because
# it is not copied to the ram disk (see /IBM/bin/softwareUpdate.sh),
# and will not be running.
SNMP_RUNNING=`/etc/rc.d/init.d/snmpd status | grep "running"`
if [ -z "$SNMP_RUNNING" ]; then              
        # Need to start snmpd, do not send cold start trap. 
        /etc/rc.d/init.d/snmpd start notrap
        # Give it 5 sec to start up before sending the trap
        sleep 5
fi

# send lcma completion success or error trap with task oid
/IBM/bin/gentrap 6 $ENTERPRISE /IBM/bin/lcmaStart $TASK_ID >> /tmp/lcma/lcma.log 2>&1

# cp lcma.log to client_task.log so we can use lcma to get it
cp /tmp/lcma/lcma.log /var/log/lcma/client_task.log

# ftp the log file to the TCM server, if specified
#if [ "$LOG_SERVER_URL" != "" ]; then
# /IBM/bin/ftp_utl /var/log/lcma/client_task.log $LOG_SERVER_URL --append
#fi	
