#!/usr/bin/bash
#rktime: A utility to show the time in different places.
#author: Richard Keech, Red Hat Asia-Pacific, rkeech@redhat.com
#copyright (c) G.Richard Keech.

#This program is released under the terms of the GNU General Public
#License, version 2.

LANG=C
#=================================================================
# Shell function showtime () formats time for a single timezone.
showtime ()
{
  ZONE=$1

  olddate=$date

  date=$(TZ=$ZONE /bin/date --date="$rt" --iso=date)

  if [ "${TIMEFORMAT}x" = "12hx" ]
  then
    time=$(TZ=$ZONE /bin/date --date="$rt" "+%_l:%M %p")
  else
    time=$(TZ=$ZONE /bin/date --date="$rt" +%H:%M)
  fi

  offset=$(TZ=$ZONE /bin/date --date="$rt" +%z)
  if [ "${TIMEOFFSET}x" = "onx" ]
  then
    time="$time $offset"
  fi

  day=$(TZ=$ZONE /bin/date --date="$rt" +%a)
  hour=$(TZ=$ZONE /bin/date --date="$rt" +%H)


  if [ "${ZONE}x" = "UTCx" ] 
  then
    ZONE=${YELLOW}${ZONE}${RC}
  fi

  if [ ${localoffset}x = ${offset}x ]
  then
    ZONE="${ZONE} ${RED}<-${RC}"
  fi
  case $day in
    Mon|Tue|Wed|Thu|Fri) weekday=true;;
    *) weekday=false;;
  esac

  if [ $hour -ge 8 -a $hour -le 18 ]
  then
    workhour=true
  else
    workhour=false
  fi

  if [ "${olddate}x" != "${date}x" -a "${olddate}x" != x ]
  then
    echo ------------------new day----------------------
  fi

  if [ $workhour = true -a $weekday = true ]
  then
    echo  $date ${WEEKDAY_COLOR}$time $day ${RC}$ZONE
  else
    if [ $weekday = true ]
    then
      # after hours
      echo $date ${AFTERHOURS_COLOR}$time $day ${RC}$ZONE
    else
      #weekend
      echo $date ${WEEKEND_COLOR}$time $day ${RC}$ZONE
    fi
  fi
} # End of showtime ()
#--------------------------------------------------------------------------
# Check that file has appropriate security.
checkfile () {

  if [ ! -f "${1}" ]
  then
    echo  "File \"${1}\" does not exist or is not in the expected location."
    return 1
  fi

  lsout="$(ls -l $1 2> /dev/null)"
  othermodes=$(echo $lsout|awk '{print $1}'|cut -c8-10)
  owner=$(echo $lsout|awk '{print $3}')
  group=$(echo $lsout|awk '{print $4}')

  if [ "${othermodes}z" != "r--z" -a "${othermodes}z" != "---z" ]
  then
    error For security, file $1 must only be \
writable and executable \(where appropriate\) by owner and group. 
  fi

  if [ "${owner}x" != rootx -a "${owner}x" != binx ]
  then
    error For security, file $1 must be owned by only root or bin.
  fi

  if [ "${group}x" != rootx -a "${group}x" != binx ]
  then
    error For security, file $1 must be associated only with \
 group root or bin.
  fi

}  # checkfile ()
#--------------------------------------------------------------------------
help () {
  PROG=$(basename $0)

  cat <<EOF
  usage:
       $PROG [options]

  options:

    --nocolor,-n     Do not display color.
    --color          Display color (default).
    --key,-k         Print explanatory key information.
    --12h,-p         Show time in 12 hour format.
    --24h,-h         Show time in 24 hour format (default).
    --offsets,-z     Display offsets from UTC, eg +0900 is 9 hours ahead.
    --nooffsets      Do not display offsets (default).
    --test,-t        Display using a time which demonstrates many of the 
                     useful aspects of the program.
    --help           Print usage information.

EOF
}
#--------------------------------------------------------------------------
# Handle an error
error () {
  echo Error: $*
  help
  exit 1
}
#=================================================================
# Main block
#-----------------------------------------------------------------------
# Reference time saved to a variable, so the calculation of
# date and time for all the zones is based on the same base.
rt=$(TZ=UTC /bin/date)

#-----------------------------------------------------------------------
# Set defaults
TIMEOFFSET=off # Display of time offsets (eg +0900 for 9h ahead of UTC).
COLOR=on  # The time is color-cued.  Set off in config file if not required.
CONFIGFILE="/etc/rktime.conf"
TIMEFORMAT=24h
USERCONFIGFILE="$HOME/.rktime.conf"
KEY=off   # This determines whether the key (explanatory text) is shown.
#Items in the zone list are entries under /usr/share/zoneinfo
ZONES="US/Pacific US/Eastern UTC Europe/London Europe/Paris Europe/Berlin \
Asia/Calcutta Asia/Singapore Asia/Hong_Kong Asia/Tokyo Australia/Brisbane"

# Set the color variables
# ISO 6429 character sequences for colors etc
lc='[1;'
BLACK=${lc}30m;  
RED=${lc}31m;	 
GREEN=${lc}32m;  
YELLOW=${lc}33m; 
BLUE=${lc}34m;   
PURPLE=${lc}35m; 
CYAN=${lc}36m;   
WHITE=${lc}37m;  
RC=${lc}0m  # reset character

WEEKDAY_COLOR=$GREEN
AFTERHOURS_COLOR=$BLUE
WEEKEND_COLOR=$WHITE
#-----------------------------------------------------------------------
# Load the optional configuration file
if [ -f "$CONFIGFILE" ]
then
  checkfile $CONFIGFILE
  . $CONFIGFILE
fi
# Load the optional user configuration file
if [ -f "$USERCONFIGFILE" ]
then
  . $USERCONFIGFILE
fi
#------------------------------------------------------------------------
# Process command-line arguments
for i in $*
do
  case $i in
    --nocolor|-n) COLOR=off; shift;;
    --color)  COLOR=on; shift;;
    --key|-k) KEY=on;shift;;
    --12h|-p) TIMEFORMAT=12h;shift;;
    --24h|-h) TIMEFORMAT=24h;shift;;
    --offsets|-z) TIMEOFFSET=on;shift;;
    --nooffsets) TIMEOFFSET=off;shift;;
    --test|-t)rt="Fri Jul 28 14:08:23 UTC 2000";shift;;
    --help) help;exit;;
    -*|--*) error $i invalid option;;
    *) error $i invalid argument;;
  esac
done
#----------------------------------------------------------------------
if [ "$COLOR" != "on" ]
then
  # Reset the color variables so no color is set.
  BLACK=""
  RED=""
  GREEN=""
  YELLOW=""
  BLUE=""
  PURPLE=""
  CYAN=""
  WHITE=""
  #------------------------------------------------------------------
  BRIGHT=""
  UNDER=""
  FLASH=""
  RC=""  # reset character
  WEEKDAY_COLOR=""
  AFTERHOURS_COLOR=""
  WEEKEND_COLOR=""
fi
#-----------------------------------------------------------------
localoffset=$(date --date="$rt" +%z)
# Iterate through the zone list and show the formatted time.
for zone in $ZONES
do
  showtime $zone
done

if [ $KEY = "on" ]
then
cat <<EOF
  ${WEEKDAY_COLOR}weekday color${RC}
  ${AFTERHOURS_COLOR}afterhours color${RC}
  ${WEEKEND_COLOR}weekend color${RC}
  ${RED}<-  localtime ${RC} 
EOF
fi

