#!/bin/bash

# Script to set the system time zone.  This script is intended for use
# by the HMC's Customize Console Date/Time task.
#
# Usage:
#     customizeTimeZone <time_zone_info>
#
#     <time_zone_info> must be one of the following:
#                      1) NotInit - Set system time zone to not initialized
#                      2) GMT     - Set system time zone to Greenwich Mean Time
#                      3) Valid time zone in xxx/yyy format, for example
#                         America/New_York represents New York's Time Zone
#
# Exit status = 0 if no errors occur; non-zero otherwise.
#
# Some other things to consider doing in the future:
# . Restart the cron daemon so that it can react to the time change
# . Restart the atd daemon so that it can react to the time change
# . Turn off the display power management system (DPMS), so the screen
#      saver, etc doesn't kick in when it shouldn't.
#
# Module History:
# 00 03/01/2005  J. Bieber                Initial release
# 01 03/20/2006  J. Bieber     D66A FBUGS Use passed temporary directory

actzTrace "XTODCTZT: -> customizeDateTime $*"

if (($# < 2)); then
   actzTrace "XTODCTZF: Missing required argument(s)."
   exit 1
fi

tmpDirectory=$1                                                                        # -01
tempFileName=$2                                                                        # -01
timeZone=$3                                                                            # -01

# Parent directory for the time zone definition files.  This is where timeconfig looks.
timeZoneDir='/usr/share/zoneinfo'
localTimeFile='/etc/localtime'
# -01 tmpDirectory='/tmp/'
clockFile='/etc/sysconfig/clock'
newClockFile=$tmpDirectory$tempFileName
backupLocal='/tmp/localtime'
backupClock='/tmp/clock'

actzTrace "XTODCTZF:    Temporary file name is $newClockFile."

# First, make a backup copy of the localtime and clock file.
if [[ -e $localTimeFile ]]; then
  actzTrace "XTODCTZF:    Backing up $localTimeFile to $backupLocal."
  cp -p $localTimeFile $backupLocal
else
  # No localtime file; be sure no tmp file exists either.
  if [[ -e $backupLocal ]]; then
    rm -f $backupLocal
  fi
fi

if [[ -e $clockFile ]]; then
  actzTrace "XTODCTZF:    Backing up $clockFile to $backupClock."
  cp -p $clockFile $backupClock
fi

# Decode verb
if [[ $timeZone == "NotInit" ]]; then
  actzTrace "XTODCTZF:    Parm = NotInit"

  if [[ -e $newClockFile ]]; then
    # Step One: Delete the local time zone file
    if [[ -e $localTimeFile ]]; then
      actzTrace 'XTODCTZF:    Deleting $localTimeFile to $backupLocal.'
      rm -f $localTimeFile
    fi

    # Step Two: Copy temp file overwriting clock file
    actzTrace "XTODCDTF:    Copying $newClockFile to $clockFile."
    cp -f $newClockFile $clockFile
  else
    actzTrace "XTODCTZF:    The $newClockFile does not exist."
    exit 2
  fi
else
  timeZoneName=$timeZone
  timeZoneFileA='Etc/'$timeZoneName
  timeZoneFileB=$timeZoneName

  actzTrace "XTODCTZF:    Time zone definition file=$timeZoneDir/$timeZoneFileA"

  # If the time zone definition file for the new time zone does not exist, use
  # zic to create it now.  This file will be created in the standard place and
  # will be usable by timeconfig.
  if [[ ! -e $timeZoneDir/$timeZoneFileA ]]; then
    actzTrace "XTODCTZF:    Time Zone file ($timeZoneDir/$timeZoneFileA) does not exist."
    actzTrace "XTODCTZF:    Attempt 2: Time zone definition file=$timeZoneDir/$timeZoneFileB"

    if [[ ! -e $timeZoneDir/$timeZoneFileB ]]; then
      actzTrace "XTODCTZF:    Time Zone file ($timeZoneDir/$timeZoneFileB) does not exist."
      exit 3
    fi
  fi

  if [[ -e $newClockFile ]]; then
    # Step One: Set local time file to proper zone
    actzTrace "XTODCTZF:    Linking $timeZoneDir/$timeZone to $localTimeFile."
    ln -sf $timeZoneDir/$timeZone $localTimeFile

    # Step Two: Copy temp file overwriting clock file
    actzTrace "XTODCTZF:    Copying $newClockFile to $clockFile."
    cp -f $newClockFile $clockFile
  else
    actzTrace "XTODCTZF:    The $newClockFile does not exist."
    exit 2
  fi
fi

actzTrace "XTODCTZT: <- customizeTimeZone"

exit 0

