#!/bin/sh

DONE=0

setup_timetype () {
  local keymap_old=`cmdline_default timetype UTC`

  timetype=`dialog --no-cancel --stdout --backtitle "$MSG_CFG_TITLE" --title "$MSG_TIMETYPE" --default-item UTC --menu "$MSG_TIMETYPE_DESC " 0 0 0 LOCAL "Local time" UTC "Universal Time"`

  cat /etc/adjtime | grep -v "^UTC" | grep -v "^LOCAL" >/tmp/adjtime
  echo "$timetype" >>/tmp/adjtime && cat /tmp/adjtime >/etc/adjtime
}

setup_timeserver () {
  cur_timeserver=`cat /etc/network | grep "^TIME_SERVER" | sed 's%TIME_SERVER="\(.*\)".*%\1%'`
  timeserver=`dialog --stdout --backtitle "$MSG_CFG_TITLE" --title "$MSG_TIMESERVER"  --inputbox "$MSG_TIMESERVER_DESC" 0 0 "$cur_timeserver"`
  [ -n "$timeserver" ] && sed_in_place /etc/network "s/^TIME_SERVER=\".*\"/TIME_SERVER=\"$timeserver\"/g"
}

time_menu () {
  menu=`dialog --no-cancel --aspect 15 --stdout --backtitle "$MSG_CFG_TITLE" --title "$MSG_MENU_TIME" --menu "$MSG_TIME_DESC" 0 0 0 timetype "$MSG_TIME_MENU_TIMETYPE ..." timeserver "$MSG_TIME_MENU_TIMESERVER ..." "" "" quit "$MSG_RETURN ..."`

  case $menu in
    timetype)
      setup_timetype
      ;;
    timeserver)
      setup_timeserver
      ;;
    quit)
      DONE=1
      ;;
  esac
}

# include configurator common file
. /usr/share/configurator/common

# get i18n strings
get_i18n

while [ $DONE != 1 ]; do
  time_menu
done
