#!/bin/ksh

#
# ident	"@(#)get_startup 1.7	05/04/14 SMI"
#
#
#  Echo run as root from a cron entry that fires this script off every 30 minutes
#
#  Script Steps:
#  0. Check to make sure PatchPro service process is not running
#  1. Create the netrc file
#  2. Save any existing netrc file and copy ours out
#  3. Get the startup.cfg file
#  4. Replace netrc file
#  5. Diff and update if needed
#
#
#  Fix for Bug ID 5054859 
#    get_startup cron job on the SP can cause the DSP upgrade to fail
#
# The cron job performs a get of the startup.cfg on the DSP. While the get is 
# in progress PatchPro issues a reboot warm on the MIC then saves the 
# running config to the startup.cfg, which is opened by the ftp get, this 
# window of time is small. When a save startup is issued a function puts 
# a lock on the startup task. Since the ftp daemon is running at the time 
# it is shutdown, the semaphore for the ftp session is not released. The 
# lock is waiting on the semaphore. This has a two fold ripple effect:
#
# 1- The config cannot be saved so the reboot warm never completes, hence 
#    PatchPro fails to complete the upgrade. 
# 2- The SP never completes the cron job so ./netrc is not cleaned up and 
#    every 30 minutes a new cron job is run, but fails to complete.
#
# If PatchPro is running just exit and wait for the next 30 minute
# interval.
#
if ps -aef | grep -i pprosvc | grep -v grep > /dev/null 2>&1; then
  exit 0
fi
#
cd /tmp
#
#  1. Create the netrc file
#
touch my_netrc
echo "machine dsp00 login root password sun1" >> my_netrc
echo "macdef init " >> my_netrc
echo "bin" >> my_netrc
echo "prompt" >> my_netrc
echo "get startup.cfg" >> my_netrc
echo "quit" >> my_netrc
echo "" >> my_netrc

#
#  2. Save any existing netrc file and copy ours out
#
if [ -f /.netrc ] ;
then
    mv /.netrc netrc.$$
fi
mv my_netrc /.netrc
chmod 600 /.netrc

#
#  6221957 - Make sure the DSP is on-line before we ftp to it.
#     We can't really do this without leaving a small window,
#     so we timeout the FTP operation (1 minute seems VERY adequate)
#            
#  3. Get the startup.cfg file
#
ftp -T 60 dsp00 1> /dev/null 2>&1
if [ $? != 0 ] ;
then
    echo dsp00 Off-line
    rm -f /.netrc
    if [ -f netrc.$$ ]; then
      mv netrc.$$ /.netrc
    fi
    exit 0
fi

#
#  4. Replace netrc file
#
rm -f /.netrc
if [ -f netrc.$$ ] ;
then
    mv netrc.$$ /.netrc
fi

#
#  5. Diff and update if needed
#
if [ ! -d /export/backup ] ;
then
    mkdir -p /export/backup
fi

diff startup.cfg /export/backup/startup.cfg 1> /dev/null 2>&1
if [ $? != 0 ] ;
then
#   echo Updating
    cp -p startup.cfg /export/backup/startup.cfg
fi

