#!/bin/bash
# this is the module for native language support

#native language support screen
nls_screen()  {

  local FONT_HELP KEYMAP_HELP LANG_HELP EDITOR_HELP

  local COMMAND

  mkdir -p ${FINALFILES}/etc/profile.d

  while true ;do

    local curkeymap
    check_dependency keymap            &&
    curkeymap=$(get_dependency keymap) ||
    curkeymap="unset"

      FONT_HELP="Select a new default console font."
    KEYMAP_HELP="Select a new default keyboard mapping. (is $curkeymap)"
      LANG_HELP="Select a new default language. (currently is \"$LANG\")"
    EDITOR_HELP="Select a new default editor."
    COMMAND=`run_dialog  --cancel-label  "Continue"                 \
             --ok-label "Select"                                    \
             --title  "Native Language Support Menu"                \
             --item-help                                            \
             --menu "Select options to customize your SMGL install" \
             0 0 0                                                 \
             "Font"    "Font     Menu "  "$FONT_HELP"               \
             "Keymap"  "Keymap   Menu "  "$KEYMAP_HELP"             \
             "Lang"    "Language Menu "  "$LANG_HELP"               \
             "Editor"  "Editor   Menu "  "$EDITOR_HELP"`           ||
    return 0

    case  $COMMAND in
        Keymap)  keymap_menu  ;;
          Font)  font_menu    ;;
          Lang)  lang_menu    ;;
        Editor)  editor_menu  ;;
    esac
  done

}

keymap_menu()  {

  local KEYMAP
  declare -a KEYMAPS
  get_keymaps                                           &&

  KEYMAP=$(memory_menu "KEYMAP" "Keymap Selection Menu"  \
           "" "Please select your preferred keymapping." \
           "defkeymap" "${KEYMAPS[@]}")                 &&

  unset KEYMAPS                                         &&

  debug_log "NLS" 1 "Chose keymapping $KEYMAP"          &&
  try_run loadkeys  $KEYMAP                             &&
  fill_dependency keymap $KEYMAP                        &&
  sed -i "s/^KEYMAP=.*$/KEYMAP=$KEYMAP/" ${FINALFILES}/etc/sysconfig/keymap

}

keymap_auto() {
  sed -i "s/^KEYMAP=.*$/KEYMAP=defkeymap/" ${FINALFILES}/etc/sysconfig/keymap
}

font_menu()  {

  local FONT
  declare -a FONTS
  get_consolefonts                                       &&

  FONT=$(memory_menu "FONT" "Console Font Selection Menu" \
         "" "Please select your preferred console fonts." \
         "default8x16" "${FONTS[@]}" )                   &&

  debug_log "NLS" 1 "Chose console font $FONT"           &&

  try_run consolechars  -f  $FONT                        &&
  fill_dependency font $FONT                             &&

  sed -i "s/^CONSOLECHARS_ARGS=.*$/CONSOLECHARS_ARGS=\"-f $FONT\"/" \
    ${FINALFILES}/etc/sysconfig/keymap                   &&

  unset FONTS
}

lang_menu()  {

  local HELP="While sorcery is entirely in English it is possible to
change the languages of many other programs.  Please
select your preferred language."

  declare -a LANGS
  get_languages                             &&

  LANG=$(memory_menu "LANG" "Language Selection Menu" \
         "" "$HELP" "POSIX" "${LANGS[@]}" ) &&
  export LANG                               &&
  fill_dependency lang $LANG                &&

  run_template $TEMPLATE_DIR/lc.sh $TEMPLATE_DIR/nls.patterns \
    >${FINALFILES}/etc/profile.d/lc.sh      &&
  unset LANGS                               &&

  debug_log "NLS" 1 "Chose LANG=$LANG"
}

editor_menu()  {

  local ELVIS_DSC="elvis is a clone of the ex/vi text editor"
  local   JED_DSC="jed is an editor with emulation for emacs, EDT, and Wordstar"
  local  NANO_DSC="nano is an enchanced free pico clone"
  local   VIM_DSC="VIM is an improved version of vi"
  local   REAL_ED

  local HELP="The Install/Rescue disc provides three easy to use
editors, elvis, nano, and jed. Due to the limited
size of the CD, vim and emacs can not be used during
installation. However, you can select vim, which still
uses elvis during the install, vim on the final system."

  REAL_ED=$(memory_menu "editor" "Select your preferred editor" \
           "--item-help" "$HELP" "nano"           \
           "nano"  "a pico clone"    "$NANO_DSC"  \
           "jed"   "Powerful editor" "$JED_DSC"   \
           "elvis" "A vi clone"      "$ELVIS_DSC" \
           "vim"   "VI IMproved"     "$VIM_DSC"   ) &&

  # REAL_ED is the editor that will be installed on the new system
  # EDITOR is the editor used throughout the install process

  if [ "${REAL_ED}" = "vim" ]; then
    export EDITOR="elvis"
  else
    export EDITOR=$REAL_ED
  fi                                                &&

  debug_log "NLS" 1 "chose $REAL_ED as EDITOR"      &&

  si_queue $REAL_ED                                 &&
  fill_dependency editor $REAL_ED                      &&

  run_template $TEMPLATE_DIR/editor.sh $TEMPLATE_DIR/nls.patterns \
    >${FINALFILES}/etc/profile.d/editor.sh
}

get_keymaps()  {

  local idx=0
  local line
  while read line ; do
    KEYMAPS[idx++]=$line
  done <$STATE_DIR/keyMapList

}

get_consolefonts()  {

  local idx=0
  local line
  while read line ; do
    FONTS[idx++]=$line
  done <$STATE_DIR/fontList

}

get_languages()  {

  local idx=0
  local line
  while read line ; do
    LANGS[idx++]=$line
  done <$STATE_DIR/languageList

}
