#!/bin/bash
# $Id: chk-time,v 0.2 2002/07/13 17:25:45 bish Exp $
# (C) 2002 USM Bish <bish@nde.vsnl.net.in>
# Released under GNU GPL v2 - http://www.gnu.org/copyleft/gpl.html
#
# Purpose: To check and change system time

###########################################################################
#	Constants
###########################################################################

PROGNAME=$(basename $0)
VERSION="0.2"

###########################################################################
#	Functions
###########################################################################

function header
{
    echo
    echo "$PROGNAME               Version : $VERSION"
    echo
}
    
function chk_root
{
    if ! [ `whoami` = "root" ]; then
       echo "$PROGNAME : ERROR - needs root privileges"
       exit 1
    fi
}
               
function usage
{
    echo "Usage: ${PROGNAME} [-h | --help] [-c] [-s]"
}


function helptext
{
	local tab=$(echo -en "\t\t")
		
	cat <<- -EOF-

        ${PROGNAME}                      ver. ${VERSION}	
	
        This is a program to check and change time on RTC
	
        $(usage)
	
        Options:
	
        -h, --help	Display this help message and exit.
        -c              Check existing system time
        -s              Set system time
			
        NOTE: You must be the superuser to run this script.
		
-EOF-
}	


###########################################################################
#	Program starts here
###########################################################################

chk_root

if [ "$1" = "--help" ] || [ "$1" = "" ]; then
   helptext
   exit 0
fi

# Process arguments - edit to taste

while getopts ":hcs" opt; do

    case $opt in
    
    c )	header
        echo "Existing time on your system is :"
        date
        echo "Use: [$PROGNAME -s] to set/ change current values"
        echo  
        ;;
    
    s ) header	
        echo "Setting system time" 
        echo
        echo -en "Enter month [01-12] : "
        read MT
        echo -en "Enter date [01-31]  : "
        read DT
        echo -en "Enter years [NNNN]  : "
        read YR
        echo -en "Enter Hours [00-23] : "
        read HR
        echo -en "Enter Mins [00-59]  : "
        read MN
        echo -en "Enter Secs [00-59]  : "
        read SC
        STRING=" $MT/$DT/$YR $HR:$MN:$SC"
        echo
        echo $STRING
        echo -en "Does this Date/Time string look Okay [ y/n ] ? "
        read YN
        case "$YN" in
        Y|y) echo "Setting the date/time"
             date -s "$STRING"
             ;;
          *) echo "Blummer ... do it again !"
             exit 1
        esac        
        ;;
    
    h )	helptext
        exit ;;
        
    * )	usage
	exit 1

    esac

done

exit 0
