#!/bin/sh

set -x

$SMSBIN/smsinit

###########################################
# This script checks the settings of 
# /etc/prod,/etc/dev,prodccs,devccs and
# DBNROOT on the current machine
###########################################

########################################
# SMS Script PASSes in the Following Variables
# envir=prod  {prod or dev)
########################################

########################################
# Find What Machine You are On and Define
# What the Other Machine Should Be
########################################
current_machine_prefix=`hostname | cut -c1`
if [ "$current_machine_prefix" = s ]
then
   current_machine=stratus
   other_machine=cirrus
else
   current_machine=cirrus
   other_machine=stratus
fi

########################################
# Check What is in /etc/prod and /etc/dev Because
# These Files Drive the cron tab services switch
# (These are switched in the "/utility_ibm/jswap_bkup" Job)
########################################
prod_machine=`cat /etc/prod`
dev_machine=`cat /etc/dev`

if [ "$envir" = "prod" ]
then
   ########################################
   # We are Running Production on Current Machine
   # So the Etc Files should be
   #       /etc/prod = current_machine
   #       /etc/dev  = other_machine
   # (These are switched in the "/utility_ibm/jswap_bkup" Job)
   ########################################
   if [ "$current_machine" = "$prod_machine" -a "$other_machine" = "$dev_machine" ]
   then
      etc_file_status="PASS"
   else
      etc_file_status="FAIL"
   fi
else
   ########################################
   # We are Running Production on Other Machine
   # So the Etc Files should be
   #       /etc/prod = other_machine
   #       /etc/dev  = current_machine
   # (These are switched in the "/utility_ibm/jswap_bkup" Job)
   ########################################
   if [ "$current_machine" = "$dev_machine" -a "$other_machine" = "$prod_machine" ]
   then
      etc_file_status="PASS"
   else
      etc_file_status="FAIL"
   fi
fi

########################################
# Check What the prodsp and devsp aliases are
# These Aliases Are What Users Use to Access the machines
# (These are switched in the "Set Prod Machine Step")
########################################
prod_alias=`host prodccs | awk -F. '{print $1}'`
dev_alias=`host devccs | awk -F. '{print $1}'`

if [ "$envir" = "prod" ]
then
   ########################################
   # We are Running Production on Current Machine
   # So the Aliases should be
   #       prodsp = current_machine
   #       devsp  = other_machine
   # (These are switched in the "Set Prod Machine Step")
   ########################################
   if [ "$current_machine" = "$prod_alias" -a "$other_machine" = "$dev_alias" ]
   then
      alias_status="PASS"
   else
      alias_status="FAIL"
   fi
else
   ########################################
   # We are Running Production on Current Machine
   # So the Aliases should be
   #       prodsp = other_machine
   #       devsp  = current_machine
   # (These are switched in the "Set Prod Machine Step")
   ########################################
   if [ "$current_machine" = "$dev_alias" -a "$other_machine" = "$prod_alias" ]
   then
      alias_status="PASS"
   else
      alias_status="FAIL"
   fi
fi

########################################
# Check What the Variable DBNROOT is set to in ~nwprod/.profile
# (These are set by hand)
########################################
if [ "$envir" = prod ]
then
   if [ "$DBNROOT" = "/gpfs/dbnet/iodprod/dbnet_siphon" ]
   then
      dbnroot_status="PASS"
   else
      dbnroot_status="FAIL"
   fi
else
   if [ "$DBNROOT" = "/gpfs/dbnet/iodprod/dbnet_siphon" ]
   then
      dbnroot_status="FAIL"
   else
      dbnroot_status="PASS"
   fi
fi

set +x
echo "----------------------------------------"
echo "             Config Status"
echo "----------------------------------------"
echo "This Machine          : $current_machine"
echo "DBNROOT=              : $DBNROOT"
echo "Prodccs Hostname      : $prod_alias"
echo "Devccs  Hostname      : $dev_alias"
echo "Prod Machine /etc/prod: $prod_machine"
echo "Dev  Machine /etc/dev : $dev_machine"
echo ""
echo "DBNROOT Envir Status  : $dbnroot_status"
echo "Alias Status          : $alias_status"
echo "Etc File Status       : $etc_file_status"
echo "----------------------------------------"
set -x

$SMSBIN/smslabel DBNROOT_STATUS $dbnroot_status
$SMSBIN/smslabel ALIAS_STATUS $alias_status
$SMSBIN/smslabel ETC_FILE_STATUS $etc_file_status

if [ "$etc_file_status" = "FAIL" -o \
     "$alias_status" = "FAIL" -o \
     "$dbnroot_status" = "FAIL" ]
then
   $SMSBIN/smsabort
else
   $SMSBIN/smscomplete
fi
