# Functions for the bflng script
# Tim Yang <tdy@gmx.com>

. /etc/rc.conf
. /etc/rc.d/functions

SRC=/usr/share/bfcommander/bflng.d
DEST=/usr/share/bfcommander/language
E_HELP=65
E_USG=66
E_ROOT=67
E_RM=68

# [0]=LNG tarball, [1+]=LNG file(s)
DE_LNG=(bfc-german-utf8-0.3.9.tar.gz german_utf8.lng)
HU_LNG=(hungarian-0.3.8.tar.gz hungarian.lng)
PL_LNG=(polish-0.3.9.tar.bz2 polish.lng)
PT_lng=(pt_BR.tar.gz portuguese.lng)
RU_LNG=(bfc-0.3.9-ru-lng-full.tar.gz russian_utf8.lng russian_cp1251.lng \
        russian_koi8r.lng)
SK_LNG=(slovak-0.3.8.lng.gz slovak.lng)

help_exit() {
  echo "Usage:"
  echo "  bflng [-option] [language]"
  echo "Options:"
  echo "  -a   add"
  echo "  -r   remove"
  echo "  -h   help"
  echo "Language packs:"
  echo "  de   German"
  echo "  hu   Hungarian"
  echo "  pl   Polish"
  echo "  pt   Portuguese"
  echo "  ru   Russian"
  echo "  sk   Slovak"
  echo "  all  All"
  exit $E_HELP
}

usage_exit() {
  echo "==> 'bflng -h' for usage help"
  exit $E_USG
}

root_exit() {
  printf "${C_FAIL}${PREFIX_HL} ${C_MAIN}Root privileges required${C_CLEAR}\n"
  exit $E_ROOT
}

printclr() {
  printf "${C_CLEAR}"
}

printadd() {
  printf "\033[1;32;40mreinstalling${C_CLEAR}"
}

printrm() {
  printf "${C_FAIL}$1${C_CLEAR}"
  stat_fail
  exit $E_RM
}

stat_lng() {
  printf "${C_OTHER}:: ${C_MAIN}$1"
}

add_lng() {
  case "$LNG" in
    de)
      [ "$UID" -ne 0 ] && root_exit
      stat_lng "Installing German language files... "
      [ -f "$DEST/${DE_LNG[1]}" ] && printadd || printclr
      tar xzf $SRC/${DE_LNG[0]} -C $DEST --strip=2 &> /dev/null
      [ $? -gt 0 ] && stat_fail || stat_done
      ;;
    hu)
      [ "$UID" -ne 0 ] && root_exit
      stat_lng "Installing Hungarian language files... "
      [ -f "$DEST/${HU_LNG[1]}" ] && printadd || printclr
      tar xzf $SRC/${HU_LNG[0]} -C $DEST &> /dev/null
      [ $? -gt 0 ] && stat_fail || stat_done
      ;;
    pl)
      [ "$UID" -ne 0 ] && root_exit
      stat_lng "Installing Polish language files... "
      [ -f "$DEST/${PL_LNG[1]}" ] && printadd || printclr
      tar xjf $SRC/${PL_LNG[0]} -C $DEST \
        --transform='s/-0.3.9//' &> /dev/null
      [ $? -gt 0 ] && stat_fail || stat_done
      ;;
    pt)
      [ "$UID" -ne 0 ] && root_exit
      stat_lng "Installing Portuguese language files... "
      [ -f "$DEST/${PT_LNG[1]}" ] && printadd || printclr
      tar xzf $SRC/${PT_LNG[0]} -C $DEST \
        --transform='s/pt_BR/portuguese/' &> /dev/null
      [ $? -gt 0 ] && stat_fail || stat_done
      ;;
    ru)
      [ "$UID" -ne 0 ] && root_exit
      stat_lng "Installing Russian language files... "
      [ -f "$DEST/${RU_LNG[1]}" -o \
        -f "$DEST/${RU_LNG[2]}" -o \
        -f "$DEST/${RU_LNG[3]}" ] && printadd || printclr
      tar xzf $SRC/${RU_LNG[0]} -C $DEST --strip=1 \
        --transform="s/utf8.*/${RU_LNG[1]}/" \
        --transform="s/cp1251.*/${RU_LNG[2]}/" \
        --transform="s/koi8r.*/${RU_LNG[3]}/" &> /dev/null
      [ $? -gt 0 ] && stat_fail || stat_done
      rm -f $DEST/readme.html
      ;;
    sk)
      [ "$UID" -ne 0 ] && root_exit
      stat_lng "Installing Slovak language files... "
      [ -f "$DEST/${SK_LNG[1]}" ] && printadd || printclr
      zcat $SRC/${SK_LNG[0]} > $DEST/${SK_LNG[1]} 2> /dev/null
      [ $? -gt 0 ] && stat_fail || stat_done
      ;;
    all)
      [ "$UID" -ne 0 ] && root_exit
      echo
      printhl "Installing all extra language packs..."
      printsep
      echo
      $0 -a de
      $0 -a hu
      $0 -a pl
      $0 -a pt
      $0 -a ru
      $0 -a sk
      ;;
    *)
      usage_exit
  esac
}

rm_lng() {
  case "$LNG" in
    de)
      [ "$UID" -ne 0 ] && root_exit
      stat_lng "Removing German language files... "
      [ -f "$DEST/${DE_LNG[1]}" ] && printclr || printrm "not installed"
      rm -f $DEST/${DE_LNG[1]} &> /dev/null
      [ $? -gt 0 ] && stat_fail || stat_done
      ;;
    hu)
      [ "$UID" -ne 0 ] && root_exit
      stat_lng "Removing Hungarian language files... "
      [ -f "$DEST/${HU_LNG[1]}" ] && printclr || printrm "not installed"
      rm -f $DEST/${HU_LNG[1]} &> /dev/null
      [ $? -gt 0 ] && stat_fail || stat_done
      ;;
    pl)
      [ "$UID" -ne 0 ] && root_exit
      stat_lng "Removing Polish language files... "
      [ -f "$DEST/${PL_LNG[1]}" ] && printclr || printrm "not installed"
      rm -f $DEST/${PL_LNG[1]} &> /dev/null
      [ $? -gt 0 ] && stat_fail || stat_done
      ;;
    pt)
      [ "$UID" -ne 0 ] && root_exit
      stat_lng "Removing Portuguese language files..."
      [ -f "$DEST/${PT_LNG[1]}" ] && printclr || printrm "not installed"
      rm -f $DEST/${PT_LNG[1]} &> /dev/null
      [ $? -gt 0 ] && stat_fail || stat_done
      ;;
    ru)
      [ "$UID" -ne 0 ] && root_exit
      stat_lng "Removing Russian language files... "
      [ -f "$DEST/${RU_LNG[1]}" -o \
        -f "$DEST/${RU_LNG[2]}" -o \
        -f "$DEST/${RU_LNG[3]}" ] && printclr || printrm "not installed"
      rm -f $DEST/{${RU_LNG[1]},${RU_LNG[2]},${RU_LNG[3]}} &> /dev/null
      [ $? -gt 0 ] && stat_fail || stat_done
      ;;
    sk)
      [ "$UID" -ne 0 ] && root_exit
      stat_lng "Removing Slovak language files... "
      [ -f "$DEST/${SK_LNG[1]}" ] && printclr || printrm "not installed"
      rm -f $DEST/${SK_LNG[1]} &> /dev/null
      [ $? -gt 0 ] && stat_fail || stat_done
      ;;
    all)
      [ "$UID" -ne 0 ] && root_exit
      echo
      printhl "Removing all extra language packs..."
      printsep
      echo
      $0 -r de
      $0 -r hu
      $0 -r pl
      $0 -r pt
      $0 -r ru
      $0 -r sk
      ;;
    *)
      usage_exit
  esac
}
