#!/bin/bash

# Import vars from os-release file
.  /etc/os-release

echo

if [[ $EUID -ne 0 ]]; then
   echo "This utility must be run as root."
   echo
   exit 1
fi

function welcome {
  echo "Welcome to the Libki Server Translation Utility"
  echo "-----------------------------------------------"
  echo
}

if ! { [ "$1" == "-h" ] || [ "$1" == "--help" ]; }
then
  if [ ! -f ./lib/Libki.pm ]
  then
    welcome

    echo "This utility can only be run from your libki-server directory."
    echo
    echo "Use --help to see script options."
    echo
    exit 1
  fi
fi

if [ $# -eq 0 ]; then
  welcome

  echo "No arguments supplied."
  echo
  echo "Use --help to see see script options."
  echo
  exit 1
fi

# Flags
while [ ! $# -eq 0 ]
do
  case "$1" in
    --help | -h)
      welcome

      echo "This utility can only be run from your libki-server directory."
      echo
      echo "Use --generate or -g to update messages.pot."
      echo
      echo "Use --update or -u followed by your po file to update it to the current messages.pot."
      echo
      echo "    Example: ./libki-i18n.sh -u lib/Libki/I18N/sv.po"
      echo
      echo "Use --cleanup or -c to remove backups and extra files generated by your po editor software."
      echo
      exit 0
      ;;

    --cleanup | -c)
      welcome

      cd lib/Libki/I18N/
      find . -type f -not -name '*.po' -not -name '*.pot' -delete
      echo "Cleanup complete!"
      echo
      echo "Everything but *.pot and *.po files was deleted from lib/Libki/I18N/."
      echo

      exit 0
      ;;

    --generate | -g)
      welcome

      # Install xgettext.pl if needed
      cpanm Locale::Maketext::Extract
      echo

      rm lib/Libki/I18N/messages.pot
      xgettext.pl --output=lib/Libki/I18N/messages.pot --directory=root/
      cat lib/Libki/I18N/extras.pot >> lib/Libki/I18N/messages.pot

      echo "Generation complete. Enjoy your fresh messages.pot file!"
      echo

      exit 0
      ;;

    --update | -u)
      welcome

      if [ ! $2 ]
      then
        welcome

        echo "This needs to be followed by your po file in order to update it to the current messages.pot."
        echo
        exit 1
      fi

      if [ "debian" == $ID ]
      then
          apt=$( dpkg-query -W gettext 2>&1 )

          if [[ $apt == *"dpkg-query"* ]]
          then
            apt update
            apt install gettext -y
          fi
      elif [ "alpine" == $ID ]
      then
        apk update 2>&1
        apk add gettext 2>&1
      else
          echo "Linux distrobution $NAME is not supported, please ensure that gettext is intalled."
      fi

      timestamp=$(date +"%y%m%d_%H:%M")

      sed -i '/^#:/ d' $2

      msgmerge -U $2 lib/Libki/I18N/messages.pot --suffix=_backup_$timestamp

      echo
      echo "Update complete. Your translation file is updated to the current messages.pot."
      echo
      echo "A backup of your old file was created, too. Please delete that once your translation is done."
      echo
      exit 0
      ;;
      *)
      welcome

      echo "Use --help for instructions."
      echo
      exit 0
      ;;
  esac
  break
done

exit 0
