#!/bin/sh

DONE=0

msgbox_save () {
  dialog --aspect 15 --stdout --yes-label "$MSG_YES" --no-label "$MSG_NO" --backtitle "$MSG_CFG_TITLE" --title "$MSG_MENU_SAVE ?" --yesno "$MSG_SAVE_DESC" 0 0 && $CONFIG_PLUGINS_PATH/save
}

msgbox_exit () {
  dialog --aspect 15 --stdout --yes-label "$MSG_YES" --no-label "$MSG_NO" --backtitle "$MSG_CFG_TITLE" --title "$MSG_MENU_EXIT ?" --defaultno --yesno "$MSG_EXIT_DESC" 0 0 && DONE=1
}

main_menu () {
  menu=`dialog --no-cancel --aspect 15 --stdout --backtitle "$MSG_CFG_TITLE" --title "$MSG_WELCOME $MSG_CFG_TITLE" --menu "$MSG_MENU_DESC" 0 0 0 lang "$MSG_MENU_LANG ..." time "$MSG_MENU_TIME ..." video "$MSG_MENU_VIDEO ..." net "$MSG_MENU_NETWORK ..." remote "$MSG_MENU_REMOTE ..." bootargs "$MSG_MENU_BOOTARGS" "" "" save "$MSG_MENU_SAVE ..." "" "" quit "$MSG_MENU_EXIT ..."`

  case $menu in
    lang|time|video|net|remote|bootargs)
      $CONFIG_PLUGINS_PATH/$menu
      ;;
    save)
      msgbox_save
      ;;
    quit)
      msgbox_exit
      ;;
  esac
}

# disable kernel messages to avoid screen corruption
echo 0 > /proc/sys/kernel/printk

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

# get i18n strings
get_i18n

# start automountd in case we're called from installator
initctl start automountd

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

initctl emit -n shutdown REBOOT=yes
