# adjrtc
#  This file contains functions to be used by most or all
#  shell scripts in the /opt/nec/drtc.
#
# COPYRIGHT (C) NEC CORPORATION 2009, 2010
# Rev.1

# Source function library.
. /opt/nec/hasdrtc/drtc_functions
[ -f /etc/hasdrtc.conf ] && . /etc/hasdrtc.conf

MODEFILE=/etc/adjtime
ADJRANGE=${ADJRANGE=5}
MESSAGE1=hasdrtc
NR_CRETRY=${NR_CRETRY=50}
SLEEP_CRETRY=${SLEEP_CRETRY=10}

get_adjtime () {
  UTC=""
  if /bin/grep ^UTC$ $MODEFILE > /dev/null 2>&1; then UTC="--utc"; fi
  local _TIME=`date $UTC '+%Y%m%d%H%M'`
  YYYY=${_TIME:0:4}
  MMDD=${_TIME:4:4}
  HHMM=${_TIME:8:4}
}

function need_adjust () {
  get_adjtime
  if [ $(($YYYY  % 2)) -ne 0 -o $(($YYYY / 10 % 2)) -ne 1 ]
  then
    return 1
  fi
  case $MMDD in
  "0229" | "0301")
    ;;
  *)
    return 1;
    ;;
  esac

  if (( 60 <= $ADJRANGE ));then
    ADJRANGE=59
  fi
  if (( $ADJRANGE <= 10#$HHMM ));then
    return 1;
  fi
  get_systime

  return 0
}
need_adjust && drtc_exe -WRITE $NR_CRETRY $SLEEP_CRETRY && exit 1
exit 0
