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


absmtpreport() {
    case $result in
	0)
		echo "Successfully set Sun MTP startup date to $response"
		echo "You will need to restart Sun MTP 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 "Sun MTP date has been specified in relative format"
		echo "For overriding, reset Sun MTP/MBM date and retry"
		;;
	4)
		echo "Invalid date specification for Sun MTP"
		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 Sun MTP system administrator for assistance"
		;;  
    esac
    }

absmbmreport() {
    case $result in
	0)
		echo "Successfully set Sun MBM startup date to $response"
		echo "You will need to restart Sun MBM 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 "Sun MBM date has been specified in relative format"
		echo "For overriding, reset Sun MTP/MBM date and retry"
		;;
	4)
		echo "Invalid date specification for Sun MBM"
		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 Sun MBM system administrator for assistance"
		;;  
    esac
    }

curmtpreport() {
	case $result in
	    0)
		    echo "Successfully set Sun MTP current date to $response"
		    echo "You will need to restart Sun MTP 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 "Sun MTP date has been specified in absolute format"
		    echo "For overriding, reset Sun MTP/MBM date and retry"
		    ;;
	    4)
		    echo "Invalid date specification for Sun MTP"
		    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 Sun MTP system administrator for assistance"
		    ;;  
	esac
	}

curmbmreport() {
    case $result in
	0)
		echo "Successfully set Sun MBM current date to $response"
		echo "You will need to restart Sun MBM 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 "Sun MBM date has been specified in absolute format"
		echo "For overriding, reset Sun MTP/MBM date and retry"
		;;
	4)
		echo "Invalid date specification for Sun MBM"
		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 Sun MBM system administrator for assistance"
		;;  
    esac
    }

# Current date/time for Sun MBM 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 Sun MBM current date"
	read response
    else 
	if [ $failcount -eq 1 ]; then
	    echo "Press <Enter> to continue with setting Sun MTP current date"
	    read response
	fi
    fi
    $KIXCLS
    echo "Sun MTP and MBM Current Date Configuration"
    echo "------------------------------------------"
    echo ""
    echo "The date entered on this panel will be treated as the current date"
    echo "for BOTH Sun MTP and MBM. By \"current\", we mean that the date/time"
    echo "shall be transparently incrementing with each passing second even if"
    echo "Sun MBM and/or MTP systems are not running. This will ensure that" 
    echo "Sun MBM and MTP date/time are in sync."
    echo ""
    echo "For example, if you specify 12/31/1999-23:50:00 now and bring"
    echo "Sun MTP and MBM up after 20 minutes, their date/time will be"
    echo "01/01/2000-00:10:00"
    echo ""
    echo "Enter current Sun MTP and MBM 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 Sun MTP current date only if Sun MBM current date has been 
# successfully set

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


# Sun MBM 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 "Sun MBM Startup Date Configuration"
    echo "----------------------------------"
    echo ""
    echo ""
    echo ""
    echo ""
    echo "Enter Sun MBM 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
    }


# Sun MTP 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 "Sun MTP Startup Date Configuration"
    echo "----------------------------------"
    echo ""
    echo ""
    echo ""
    echo ""
    echo "Enter Sun MTP 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 "Sun MTP 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 "             Sun MTP Date/Time Configuration Utility       $DATEVAR"
echo " "
echo " "
echo "             1.  Set Sun MTP startup date"
echo "             2.  Set Sun MBM startup date"
echo "             3.  Set current date for both Sun MBM and MTP"
echo ""
echo "             D.  Display present date/time configuration"
echo "             R.  Reset Sun MTP/MBM 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: Sun MTP configuration (option 1 and 3) disabled"
    nokixsys=1
else
    echo ""
fi

if [ ! -n "$PACK" ]; then
    echo "Warning - PACK not set:  Sun MBM 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 Sun MTP 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 Sun MBM 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 Sun MTP configuration"
	    echo "and PACK environment variable for Sun MBM 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 Sun MBM date"
	  else
	      if [ ! -d "$PACK" ]; then
		  echo ""
		  echo "PACK directory $PACK does not exist. Unable to display Sun MBM date"
	      else
		  kxukebm_setdate $PACK/datefile "e" 'd'
		  result=$?
		  if [ $result -eq 7 ]; then
		      echo "No existing Sun MBM date configuration"
		  else
		      if [ $result -ne 0 ]; then
		          echo "Error in displaying Sun MBM date. Please check file permissions"
		      fi
		  fi
	      fi
	  fi
	  if [ "$nokixsys" -eq 1 ]; then
	      echo ""
	      echo "KIXSYS environment variable not set. Unable to display Sun MTP date"
	  else
	      if [ ! -d "$KIXSYS" ]; then
		  echo ""
		  echo "KIXSYS directory $KIXSYS does not exist. Unable to display Sun MTP date"
	      else
		  kxukebm_setdate $KIXSYS/datefile "u" 'd'
		  result=$?
		  if [ $result -eq 7 ]; then
		      echo "No existing Sun MTP date configuration"
		  else
		      if [ $result -ne 0 ]; then
			  echo "Error in displaying Sun MTP 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 Sun MBM date"
	  else
	      if [ ! -d "$PACK" ]; then
	       	  echo ""
	  	  echo "PACK directory $PACK does not exist. Unable to reset Sun MBM date"
	      else
	          kxukebm_setdate $PACK/datefile "" 'r'
		  result=$?
		  if [ $result -eq 0 ]; then
		      echo "Successfully reset Sun MBM date"
		  else 
		      if [ $result -eq 2 ]; then
		          echo "$PACK/datefile does not exist. No existing Sun MBM date configuration"
		      else
		          echo "Error in resetting Sun MBM date. Please check file permissions"
		      fi
		  fi
	      fi
	  fi

	if [ "$nokixsys" -eq 1 ]; then
	    echo ""
	    echo "KIXSYS environment variable not set. Unable to reset Sun MTP date"
	else
	    if [ ! -d "$KIXSYS" ]; then
		echo ""
		echo "KIXSYS directory $KIXSYS does not exist. Unable to reset Sun MTP date"
	    else
		kxukebm_setdate $KIXSYS/datefile "" 'r'
		result=$?
		if [ $result -eq 0 ]; then
		    echo "Successfully reset Sun MTP date"
		else 
		    if [ $result -eq 2 ]; then
			echo "$KIXSYS/datefile does not exist. No existing Sun MTP date configuration"
		    else
		        echo "Error in resetting Sun MTP 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

