#!/bin/sh
#
#    Copyright (c) 2001 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    File name:   date
#    Module name: fabos/src/utils/sys
#
#    This script is wrapper to the traditional /bin/date command
#    to be able to save the changed date into flash. It also
#    propagates date to the counterpart CP.
#
export PATH=/fabos/sbin:/fabos/bin:/bin:/usr/bin:/sbin

if [ $# -eq 0 ]; then
   /bin/date
elif [ $# -eq 1 ] || \
     [ "$1" = "-u" ] || \
     [ "$1" = "--utc" ] || \
     [ "$1" = "--universal" ] || \
     [ `echo $1|cut -d' ' -f1|  wc --bytes 2> /dev/null` -gt 2  -a \
       `echo $1 |cut -c -2 2> /dev/null` = "-s"	] || \
     [ `echo $1|cut -d' ' -f1|  wc --bytes 2> /dev/null` -gt 2  -a \
       `echo $1 |cut -c -3 2> /dev/null` = "--s"	]; then

        user=`/usr/bin/whoami`
#Find whether it is read or write command.
	setOp=0
        if [ `echo $1|cut -d' ' -f1|  wc --bytes 2> /dev/null` -gt 2 ] && [ `echo $1 |cut -c -2` = "-s" \
              -o `echo $1 |cut -c -3` = "--s" ]; then
               setOp=1
        elif [ $# -eq 1 ]; then
	       arg=`echo $1 |cut -c -1`
               if [ "$arg" != "-" \
                    -a "$arg" != "+" ]; then
	            setOp=1
	       fi
	elif [ $# -gt 1 ]; then
               if [ "$1" = "-u" \
         	    -o "$1" = "--utc" \
                    -o "$1" = "--universal" ]; then
		  setOp=2
               fi
	fi

#Check for active CP or non Ulys switch. 
	cp=Nonulys
	switchType=`/sbin/sin |grep "Platform:" |cut -d' ' -f2 2> /dev/null`	
	if [ "$switchType" = "SWBD10," ]; then
		cp=`/fabos/sbin/hashow |grep "Local" |cut -d' ' -f6 2> /dev/null`
	fi

	if [ $setOp -eq 0 ]; then
	        /bin/date "$*"
#It is a write command so check if it is valid user and Active CP.
        elif [ "$user" = "root" -o "$user" = "admin" ] &&
             [ "$cp" = "Active" -o "$cp" = "Nonulys" ]; then

		if [ $setOp -eq 1 ]; then
	           /bin/date "$*"
		else
	           /bin/date $*	
		fi

                if [ $? -eq 0 ]; then

                   /sbin/hwclock -w
#Find if counterpart CP exists and then proceed.
                   output=`/sbin/sin|grep "Processor No"  2> /dev/null`
	           if [ $? -eq 0 ]; then	
                      IDX=`sin |grep "Processor No"| cut -d: -f2 2> /dev/null`
                      if [ $IDX -eq 0 ]; then
                         RIDX=6
                      else
                         RIDX=5
                      fi
		   fi

	           output=`/fabos/sbin/hashow 2>/dev/null`	
                   if [ $? -eq 0 -a "$cp" = "Active" ]; then
                      redundant=`/fabos/sbin/hashow |grep "Remote" |cut -d' ' -f6 2> /dev/null`
		      if [ "$redundant" = "Standby" -a $setOp -eq 1 ]; then
                         /usr/bin/rsh 10.0.0.$RIDX rdate -s 10.0.0.`expr $IDX + 5`
	              elif [ "$redundant" = "Standby" -a $setOp -eq 2 ]; then
                         /usr/bin/rsh 10.0.0.$RIDX /bin/date $*
	              fi		
		
                      if [ "$redundant" = "Standby" -a $? -eq 0 ]; then
                         /usr/bin/rsh 10.0.0.$RIDX /sbin/hwclock -w
                      fi
                   fi
                fi
        else
           echo "date: cannot set date: operation not permitted"
        fi
else
        /bin/date $*
fi

