#!/bin/sh

PATH=/usr/bin:/usr/sbin:/bin:/sbin

[ -n "$(which hwclock 2>/dev/null)" ] || exit 0

UTC=no

sync_opt=--hctosys
if [ -e /etc/timezone ]; then
  read TZ 2>/dev/null < /etc/timezone
  if [ -n "$TZ" ]; then
    export TZ
    sync_opt=--systz
  fi
fi

if [ "x$UTCx" = "xyesx" ]; then
  tz_opt=--utc
else
  tz_opt=--localtime
fi

case "$1" in
  start)
    echo -n "Setting timezone and system clock: "
    hwclock $sync_opt $tz_opt
    if [ $? != 0 ]; then
      echo "[Fail]"
    else
      echo "[OK]"
    fi
  ;;
  stop)
    echo -n "Syncing system time to hwclock: "
    hwclock --systohc
    if [ $? != 0 ]; then
      echo "[Fail]"
    else
      echo "[OK]"
    fi
  ;;
  *)
    echo "$0 [start|stop]"
  ;;
esac
