#!/bin/sh

ipaddr=""
true=1
FALSE=0
CONFLAG=0

error() {
  echo "Exiting download SCRIPT \"$0\" due to a possible user error\n"
  exit 1
}
error1() {
  echo "Exiting download SCRIPT \"$0\" due to a possible PATCH related error\n"
  exit 1
}

trap "error" 1 2 3

Patchpro_contract=$FALSE
if [ "$#" -eq 1 ]; then
  if [ "$#"="ppro" ]; then
    Patchpro_contract=$true
  fi
fi

echo  '\n\n###############################################################################'
echo '###  This is SE 3xxx Download Pogram V1.7              ###'
echo '###  Please refer README for installation instructions ###'
echo  '###############################################################################\n\n'

# Checking for Download program mdload
PWD=`pwd`
MLOAD=`ls ${PWD}/mdload 2>/dev/null`
if [ "X$MLOAD" = "X" ]; then
	echo '\n\n*** ERROR! *** The donwload   program   "mdload"' 
	echo '*** ERROR! *** does NOT exist in the current PATH'
	echo '*** ERROR! *** Please   check  the   README   for'
	echo '*** ERROR! *** installation  instructions -------\n\n'
	error
fi

VERSION=`${MLOAD} -v | /usr/bin/cut -d' ' -f3`

if [ "${VERSION}" != '1.7' ]; then
	echo '\n\n*** ERROR! *** The download program "mdload" is not the'
	echo '*** ERROR! *** correct Version for this patch'
	echo '*** ERROR! *** Please send an email to the submitter of this PATCH\n\n'
	error1
fi


echo  'Please Enter HOSTNAME OR \nIP ADDRESS Of SE 3xxx for upgrade: \c'
read ipaddr    
ping $ipaddr 1>/dev/null 2>/dev/null
ERRCODE=$?      
if [ $ERRCODE -ne $FALSE ]; then
  echo '\n*** WARNING! *** invalid IP adress OR target is unreachable.'
  echo '*** WARNING! *** please  check if  the SE 3xxx  is connected'
  echo '*** WARNING! *** to  a  network  AND  has a valid IP address\n'
  error
fi

${MLOAD} $ipaddr

exit 0
