#! /bin/sh
#**********************************************************************#
#*                                                                    *#
#* Copyright (c) 2001 by Sun Microsystems, Inc.                       *#
#* All rights reserved.                                               *#
#*                                                                    *#
#**********************************************************************#


absmtpreport() {
    case $result in
	0)
		echo "Successfully set MTP Region startup date to $response"
		echo "You will need to restart MTP Region for the new date setting to take effect"
		;;
	1)
		echo "Write permission denied for $KIXSYS/datefile"
		echo "Please correct permissions and retry"
		;;
	2)	
		echo "Write failed for $KIXSYS/datefile"
		echo "Please check disk/directory and retry"
		;;
	3)
		echo "MTP Region date has been specified in relative format"
		echo "For overriding, reset the MTP Region and the Batch Node date and retry"
		;;
	4)
		echo "Invalid date specification for MTP Region"
		echo "Date should be between 1970 and 2030"
		echo "Use MM/DD/YYYY-HH:MM:SS format and retry"
		;;
	5)
		echo "Internal processing error in date configuration"
		echo "Please contact MTP Region administrator for assistance"
		;;  
    esac
    }

absmbmreport() {
    case $result in
	0)
		echo "Successfully set Batch Node startup date to $response"
		echo "You will need to restart the Batch Node for the new date setting to take effect"
		;;
	1)
		echo "Write permission denied for $PACK/datefile"
		echo "Please correct permissions and retry"
		;;
	2)	
		echo "Write failed for $PACK/datefile"
		echo "Please check disk/directory and retry"
		;;
	3)
		echo "Batch Node date has been specified in relative format"
		echo "For overriding, reset the MTP Region  and the Batch Node date and retry"
		;;
	4)
		echo "Invalid date specification for Batch Node"
		echo "Date should be between 1970 and 2030"
		echo "Use MM/DD/YYYY-HH:MM:SS format and retry"
		;;
	5)
		echo "Internal processing error in date configuration"
		echo "Please contact Batch Node system administrator for assistance"
		;;  
    esac
    }

curmtpreport() {
	case $result in
	    0)
		    echo "Successfully set MTP Region current date to $response"
		    echo "You will need to restart MTP Region for the new date setting to take effect"
		    ;;
	    1)
		    echo "Write permission denied for $KIXSYS/datefile"
		    echo "Please correct permissions and retry"
		    ;;
	    2)	
		    echo "Write failed for $KIXSYS/datefile"
		    echo "Please check disk/directory and retry"
		    ;;
	    3)
		    echo "MTP Region date has been specified in absolute format"
		    echo "For overriding, reset the MTP Region and the Batch Node date and retry"
		    ;;
	    4)
		    echo "Invalid date specification for MTP Region"
		    echo "Date should be between 1970 and 2030"
		    echo "Use MM/DD/YYYY-HH:MM:SS format and retry"
		    ;;
	    5)
		    echo "Internal processing error in date configuration"
		    echo "Please contact MTP Region administrator for assistance"
		    ;;  
	esac
	}

curmbmreport() {
    case $result in
	0)
		echo "Successfully set Batch Node current date to $response"
		echo "You will need to restart Batch Node for the new date setting to take effect"
		;;
	1)
		echo "Write permission denied for $PACK/datefile"
		echo "Please correct permissions and retry"
		;;
	2)	
		echo "Write failed for $PACK/datefile"
		echo "Please check disk/directory and retry"
		;;
	3)
		echo "Batch Node date has been specified in absolute format"
		echo "For overriding, reset the MTP Region and the Batch Node date and retry"
		;;
	4)
		echo "Invalid date specification for Batch Node"
		echo "Date should be between 1970 and 2030"
		echo "Use MM/DD/YYYY-HH:MM:SS format and retry"
		;;
	5)
		echo "Internal processing error in date configuration"
		echo "Please contact Batch Node system administrator for assistance"
		;;  
    esac
    }

# Current date/time for Batch Node and MTP Panel

setmtpmbmdate() {
    failcount=0
    kxukebm_setdate $PACK/datefile "UKCHECK" 'c'
    result=$?
    if [ $result -ne 0 ]; then
	curmbmreport
	failcount=1
    fi
    kxukebm_setdate $KIXSYS/datefile "UKCHECK" 'c'
    result=$?
    if [ $result -ne 0 ]; then
	curmtpreport
	if [ $failcount -eq 1 ]; then
	    echo "Press <Enter> to return to menu"           
	    read response
	    return
	fi
	failcount=2
    fi
    if [ $failcount -eq 2 ]; then
	echo "Press <Enter> to continue with setting Batch Node current date"
	read response
    else 
	if [ $failcount -eq 1 ]; then
	    echo "Press <Enter> to continue with setting MTP Region current date"
	    read response
	fi
    fi
    $KIXCLS
    echo "MTP Region and batch Node Current Date Configuration"
    echo "----------------------------------------------------"
    echo ""
    echo "The date entered on this panel will be treated as the current date"
    echo "for BOTH MTP Region and Batch Node. By \"current\", we mean that the date/time"
    echo "shall be transparently incrementing with each passing second even if"
    echo "the Batch Node and/or the MTP Region are not running. This will ensure that" 
    echo "the Batch Node and the MTP Region date/time are in sync."
    echo ""
    echo "For example, if you specify 12/31/1999-23:50:00 now and bring"
    echo "the MTP Region and the Batch Node up after 20 minutes, their date/time will be"
    echo "01/01/2000-00:10:00"
    echo ""
    echo "Enter current MTP Region and Batch Node date in the format MM/DD/YYYY-HH:MM:SS"
    echo ""
    read response
    if [ "$response" = "" ]; then
	response="noinput"
    fi
    echo ""
    echo ""
    format='c'
    kxukebm_setdate $PACK/datefile $response $format
    result=$?
    curmbmreport
# Proceed with MTP Region current date only if Batch Node current date has been 
# successfully set

    kxukebm_setdate $KIXSYS/datefile $response $format
    result=$?
    curmtpreport
    echo "Press <Enter> to continue"
    read response
    }


# Batch Node startup date Panel

setmbmdate () {
    kxukebm_setdate $PACK/datefile "UKCHECK" 'a'
    result=$?
    if [ $result -ne 0 ]; then
	absmbmreport
	echo "Press <Enter> to continue"
	read response
	return 
    fi
    $KIXCLS
    echo "Batch Node Startup Date Configuration"
    echo "-------------------------------------"
    echo ""
    echo ""
    echo ""
    echo ""
    echo "Enter Batch Node Startup Date in the format MM/DD/YYYY-HH:MM:SS"
    echo ""
    read response
    if [ "$response" = "" ]; then
	response="noinput"
    fi
    format='a'
    kxukebm_setdate $PACK/datefile $response $format
    result=$?
    absmbmreport
    echo "Press <Enter> to continue"
    read response
    }


# MTP Region startup date format 

setkixdate () {
    kxukebm_setdate $KIXSYS/datefile "UKCHECK" 'a'
    result=$?
    if [ $result -ne 0 ]; then
	absmtpreport
	echo "Press <Enter> to continue"
	read response
	return 
    fi
    $KIXCLS
    echo "MTP Region Startup Date Configuration"
    echo "-------------------------------------"
    echo ""
    echo ""
    echo ""
    echo ""
    echo "Enter MTP Region Startup Date in the format MM/DD/YYYY-HH:MM:SS"
    echo ""
    read response
    if [ "$response" = "" ]; then
	response="noinput"
    fi
    format='a'
    kxukebm_setdate $KIXSYS/datefile $response $format
    result=$?
    absmtpreport
    echo "Press <Enter> to continue"
    read response
    }

trap 'echo "MTP Region Date/Time Configuration aborted!";exit' 1 2 3 15

KIXCLS=clear
case `uname -m` in
      i386)
      	KIXCLS='tput clear';;
      DPX/2300)
      	KIXCLS='tput clear';;
      sun*)
#-----------------------Set echo name -----------------------------------#
        if test -d /usr/5bin
        then
           PATH=/usr/5bin:$PATH
        else
           BSD=YES;export BSD
        fi;;
esac
export KIXCLS

if [ ! -n "$KIXSYS"  -a  ! -n "$PACK" ]; then

    echo "Neither KIXSYS nor PACK environment variable is set"
    echo "Terminating Date/Time configuration"
    exit 1

fi

#
# Figure out if we are on NT or UNIX
#
OS_PLATFORM=`uname`
#
# Set up variables according to platform
#
if [ "$OS_PLATFORM" = "Windows_NT" ]
then
    NULL_DEVICE=NUL
else
    NULL_DEVICE=/dev/null
fi

#-----------------------------------------------------------------------#
#  The following code is required for systems where echo always adds a  #
#  new line.  The code checks for new line suppressing and makes it as  #
#  variable to be used                                                  #
#-----------------------------------------------------------------------#
if  echo "\c" | grep c >$NULL_DEVICE 2>&1 ; then    
    N='-n'
else
    C='\c'
fi
export N C

nobatch=0
nokixsys=0

flag=1
# ----------------------------------------------------------------------------
# - This is the menu screen
# ----------------------------------------------------------------------------
while [ $flag -le 10 ]
do

$KIXCLS
DATEVAR=`date '+%m/%d/%Y   %H:%M:%S'`
export DATEVAR

# Main Menu

echo "                 Date/Time Configuration Utility           $DATEVAR"
echo " "
echo " "
echo "             1.  Set MTP Region startup date"
echo "             2.  Set Batch Node startup date"
echo "             3.  Set current date for both MTP Region and Batch Node"
echo ""
echo "             D.  Display present date/time configuration"
echo "             R.  Reset current date to the UNIX system date"
echo "             H.  Help"
echo "             E.  Exit configuration "
echo " "
echo " "
echo " "
echo "--------------------------------------------------------------------------------"
echo ""

if [ ! -n "$KIXSYS" ]; then
    echo "Warning - KIXSYS not set: MTP Region configuration (option 1 and 3) disabled"
    nokixsys=1
else
    echo ""
fi

if [ ! -n "$PACK" ]; then
    echo "Warning - PACK not set:  Batch Node configuration (option 2 and 3) disabled"
    nobatch=1
else
    echo ""
fi

echo ""

echo $N "Enter the option number corresponding to the selection you want: $C"
read opt

case $opt in
     1) if [ "$nokixsys" -eq 1 ]; then
	    echo ""
	    echo "Option 1 disabled - Set KIXSYS environment variable for MTP Region configuration"
	    echo "Press <Enter> to continue"
	    read response
	else
	    if [ ! -d "$KIXSYS" ]; then
		echo ""
		echo "Request Ignored - KIXSYS directory $KIXSYS does not exist"
		echo "Press <Enter> to continue"
		read response
	    else 
		if [ ! -w "$KIXSYS/datefile"  -a -f "$KIXSYS/datefile" ]; then
		    echo ""
		    echo "Request Ignored - File $KIXSYS/datefile is not writable"
		    echo "Press <Enter> to continue"
		    read response
		else
		    setkixdate
		fi
	    fi
	fi
	;;
     2) if [ "$nobatch" -eq 1 ]; then
	    echo ""
	    echo "Option 2 disabled - Set PACK environment variable for Batch Node configuration"
	    echo "Press <Enter> to continue"
	    read response
	else 
	    if [ ! -d "$PACK" ]; then
		echo ""
		echo "Request Ignored - PACK directory $PACK does not exist"
		echo "Press <Enter> to continue"
		read response
	    else 
		if [ ! -w "$PACK/datefile"  -a -f "$PACK/datefile" ]; then
		    echo ""
		    echo "Request Ignored - File $PACK/datefile is not writable"
		    echo "Press <Enter> to continue"
		    read response
	        else
	            setmbmdate
        	fi
	    fi
	fi
	;;
     3) if [ "$nobatch" -eq 1  -o "$nokixsys" -eq 1 ]; then
	    echo ""
	    echo "Option 3 disabled - Set KIXSYS environment variable for MTP Region configuration"
	    echo "and PACK environment variable for Batch Node configuration"
	    echo "Press <Enter> to continue"
	    read response
	else
	    if [ ! -d "$PACK" ]; then
		echo ""
		echo "Request Ignored - PACK directory $PACK does not exist"
		echo "Press <Enter> to continue"
		read response
	    else
		if [ ! -w "$PACK/datefile"  -a -f "$PACK/datefile" ]; then
		    echo ""
		    echo "Request Ignored - File $PACK/datefile is not writable"
		    echo "Press <Enter> to continue"
		    read response
		else
		    if [ ! -d "$KIXSYS" ]; then
			echo ""
			echo "Request Ignored - KIXSYS directory $KIXSYS does not exist"
			echo "Press <Enter> to continue"
			read response
		    else
			if [ ! -w "$KIXSYS/datefile"  -a -f "$KIXSYS/datefile" ]; then
			    echo ""
			    echo "Request Ignored - File $KIXSYS/datefile is not writable"
			    echo "Press <Enter> to continue"
			    read response
			else
			    setmtpmbmdate
			fi
		    fi
		fi
	    fi
	fi
	;;
     D|d) if [ "$nobatch" -eq 1 ]; then
	      echo ""
	      echo "PACK environment variable not set. Unable to display Batch Node date"
	  else
	      if [ ! -d "$PACK" ]; then
		  echo ""
		  echo "PACK directory $PACK does not exist. Unable to display Batch Node date"
	      else
		  kxukebm_setdate $PACK/datefile "e" 'd'
		  result=$?
		  if [ $result -eq 7 ]; then
		      echo "No existing Batch Node date configuration"
		  else
		      if [ $result -ne 0 ]; then
		          echo "Error in displaying Batch Node date. Please check file permissions"
		      fi
		  fi
	      fi
	  fi
	  if [ "$nokixsys" -eq 1 ]; then
	      echo ""
	      echo "KIXSYS environment variable not set. Unable to display MTP Region date"
	  else
	      if [ ! -d "$KIXSYS" ]; then
		  echo ""
		  echo "KIXSYS directory $KIXSYS does not exist. Unable to display MTP Region date"
	      else
		  kxukebm_setdate $KIXSYS/datefile "u" 'd'
		  result=$?
		  if [ $result -eq 7 ]; then
		      echo "No existing MTP Region date configuration"
		  else
		      if [ $result -ne 0 ]; then
			  echo "Error in displaying MTP Region date. Please check file permissions"
		      fi
		  fi
	      fi
	  fi
	  echo "Press <Enter> to continue"
	  read response
	  ;;
     R|r) if [ "$nobatch" -eq 1 ]; then
	      echo ""
	      echo "PACK environment variable not set. Unable to reset Batch Node date"
	  else
	      if [ ! -d "$PACK" ]; then
	       	  echo ""
	  	  echo "PACK directory $PACK does not exist. Unable to reset Batch Node date"
	      else
	          kxukebm_setdate $PACK/datefile "" 'r'
		  result=$?
		  if [ $result -eq 0 ]; then
		      echo "Successfully reset Batch Node date"
		  else 
		      if [ $result -eq 2 ]; then
		          echo "$PACK/datefile does not exist. No existing Batch Node date configuration"
		      else
		          echo "Error in resetting Batch Node date. Please check file permissions"
		      fi
		  fi
	      fi
	  fi

	if [ "$nokixsys" -eq 1 ]; then
	    echo ""
	    echo "KIXSYS environment variable not set. Unable to reset MTP Region date"
	else
	    if [ ! -d "$KIXSYS" ]; then
		echo ""
		echo "KIXSYS directory $KIXSYS does not exist. Unable to reset MTP Region date"
	    else
		kxukebm_setdate $KIXSYS/datefile "" 'r'
		result=$?
		if [ $result -eq 0 ]; then
		    echo "Successfully reset MTP Region date"
		else 
		    if [ $result -eq 2 ]; then
			echo "$KIXSYS/datefile does not exist. No existing MTP Region date configuration"
		    else
		        echo "Error in resetting MTP Region date. Please check file permissions"
		    fi
		fi
	    fi
	fi
	echo "Press <Enter> to continue"
	read response
	;;
     E|e) $KIXCLS
	  exit;;
     H|h) $KIXCLS
	if [ ! -n "$UNIKIX" ]; then
	    ${PAGER-'/bin/more'} $PACK/ish/kixdate.help
	else
	    ${PAGER-'/bin/more'} $UNIKIX/help/kixdate.help
	fi
	echo "Press <Enter> to continue"
	read response
	;;
     *) echo "Wrong option selected"
	echo "Press <Enter> to continue"
	read response
	;;
esac
done

