#!/usr/bin/env bash
# Copyright (C) 2021  Leipzig University Library
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# @author  Robert Lange <lange@ub.uni-leipzig.de>
# @license https://opensource.org/licenses/GPL-3.0 GNU GPLv3

# this script merges token files and removes unused / duplicate tokens with parent and target translation file *.ini
# assumes You are in projects base dir

# if new translations are on alpha, first copy files via:
# scp -r [user]@139.18.19.237:/usr/local/vufind/[instance]/[issue_number]/data/i18n/languages /[path_to_instance_directory]/data/i18n/languages

Help()
{
   # Display Help
   echo "Usage: $0 path/to/language/with/new/tokens path/to/language/file/of/instance [path/to/parent/file/of/instance]"
   echo "example command 1: \"devops/i18n-merge.sh data/i18n/languages/de.ini de_zi4/languages/de.ini local/languages/de.ini\"";
   echo "if new translations are on alpha, first copy files via: scp -r [user]@139.18.19.237:/usr/local/vufind/[instance]/[issue_number]/data/i18n/languages /[path_to_instance_directory]/data/i18n/languages"
}

while getopts ":h" option; do
   case $option in
      h) # display Help
         Help
         exit;;
     \?) # incorrect option
         echo "Error: Invalid option"
         exit 1;
   esac
done

CACHE_FILE=$1; INSTANCE_FILE=$2; PARENT_FILE=$3;

HAS_ERROR=0;
if [ -z "$CACHE_FILE" ] || [ -z "$INSTANCE_FILE" ]; then
  Help
  exit 1;
else
  if [ ! -f "$CACHE_FILE" ]
  then
    echo "Language File "$CACHE_FILE" does not exist on your filesystem."; HAS_ERROR=1;
  fi

  if [ ! -f "$INSTANCE_FILE" ]
  then
    echo "Language File "$INSTANCE_FILE" does not exist on your filesystem."; HAS_ERROR=1;
  fi

  if [ ${CACHE_FILE##*/} != ${INSTANCE_FILE##*/} ]
  then
    echo "Cache file "$CACHE_FILE" and language file "$INSTANCE_FILE" must have same file name.";
    HAS_ERROR=1;
  fi
fi

if [ -z "$CACHE_FILE" ]; then
  if [ ! -f "$PARENT_FILE" ]; then
    echo "Parent Language File "$PARENT_FILE" does not exist on your filesystem.";
    HAS_ERROR=1;
  fi
  if [ ${CACHE_FILE##*/} != ${PARENT_FILE##*/} ]; then
    echo "Cache file "$CACHE_FILE" and parent file "$PARENT_FILE" must have same file name.";
    HAS_ERROR=1;
  fi
fi

if [ ! -z "$PARENT_FILE" ]; then
  if [ ${INSTANCE_FILE##*/} != ${PARENT_FILE##*/} ]; then
    echo "Instance file "$INSTANCE_FILE" and parent file "$PARENT_FILE" must have same file name.";
    HAS_ERROR=1;
  fi
fi

if [ $HAS_ERROR = 1 ]
then
  exit 1;
fi

echo "Read new tokens from $CACHE_FILE."
declare -A newTokens
declare -a newTokensOrder
while IFS= read -r line || [ -n "$line" ] # handle possible missing newline in last line
do
  if [ ! -z "$line" ]; then # ignore empty lines
    key=$(echo "$line" | awk -F: '{ st = index($0,"=");print substr($0,0,st-1)}')
    value=$(echo "$line" | awk -F: '{ st = index($0,"=");print substr($0,st+1)}')
    newTokens["$key"]="$value"
    newTokensOrder+=("$key");
  fi
done < $CACHE_FILE

echo "Reading and minify existing tokens from $INSTANCE_FILE"
declare -A instanceTokens
declare -A instanceComments
declare -a instanceTokensOrder
i=0
while IFS= read -r line || [ -n "$line" ]; # handle possible missing newline in last line
do
  if [ -z "$line" ]; then
    #echo "empty"
    instanceTokensOrder+=("X_empty_X");
  elif [[ ${line:0:1} == "#" ]] || [[ ${line:0:1} == ";" ]]; then
    #echo "comment"
    instanceTokensOrder+=("X_comment_$i")
    instanceComments["X_comment_$i"]="$line"
  elif [[ ${line:0:11} == "@parent_ini" ]]; then
    #echo "relative path to parent ini"
    instanceTokensOrder+=("X_comment_$i")
    instanceComments["X_comment_$i"]="$line"
    #PARENT_FILE=$(echo "$line" | awk -F: '{ st = index($0,"=");print substr($0,st+3)} '  | sed 's/.$//')
    #PARENT_FILE=$(echo "${PARENT_FILE#@(.)}")
    #PARENT_FILE="${INSTANCE_FILE}/${PARENT_FILE}"
    #echo "$PARENT_FILE"
  else
    key=$(echo "$line" | awk -F: '{ st = index($0,"=");print substr($0,0,st-1)}')
    value=$(echo "$line" | awk -F: '{ st = index($0,"=");print substr($0,st+1)}')
    #echo "$line"
    # only add to instanceTokensOrder when key entry is NOT already existing -> remove duplicates / ignored by letting latest entry win
    if [[ -n "${instanceTokens["$key"]}" ]]; then
      echo "* remove duplicate token \"${key}\" with obsolete value ${instanceTokens["$key"]} by $value."
    else
      instanceTokensOrder+=("$key");
    fi
    instanceTokens["$key"]="$value";
  fi
  ((i=i+1))
done < $INSTANCE_FILE

if [ -f "$PARENT_FILE" ]; then
  echo "Reading and minifying existing tokens from $PARENT_FILE..."
  declare -A parentTokens
  declare -a parentTokensOrder
  while IFS= read -r line || [ -n "$line" ]
  do
  if [ -z "$line" ] || [[ ${line:0:1} == "#" ]] || [[ ${line:0:1} == ";" ]] || [[ ${line:0:11} == "@parent_ini" ]]; then
    continue
  else
    key=$(echo "$line" | awk -F: '{ st = index($0,"=");print substr($0,0,st-1)}')
    value=$(echo "$line" | awk -F: '{ st = index($0,"=");print substr($0,st+1)}')
    #echo "$line"
    # only add to instanceTokensOrder when key entry is NOT already existing -> remove duplicates / ignored by letting latest entry win
    if [[ ${instanceTokens["$key"]} == "$value" ]] ; then
      #instanceTokens["$key"]="$value";
      echo "* remove duplicate token \"${key}\" and $value with parent file."
      instanceTokens["$key"]="X_duplicate_X";
    fi
  fi
  done < $PARENT_FILE
fi

echo "Adding new token translations..."
for i in "${!newTokensOrder[@]}"
do
  if [[ -n "${instanceTokens[${newTokensOrder[$i]}]}" ]]; then
    echo "* replace token \"${newTokensOrder[$i]}\" with old value ${instanceTokens[${newTokensOrder[$i]}]} by new value $value"
  else
    instanceTokensOrder+=("${newTokensOrder[$i]}");
    echo "* add new token \"${newTokensOrder[$i]}\" with value ${newTokens[${newTokensOrder[$i]}]}"
  fi
  instanceTokens["${newTokensOrder[$i]}"]="${newTokens[${newTokensOrder[$i]}]}";
done

echo "4) Writing merged translations to instance file..."
true > "${INSTANCE_FILE}"
for i in "${!instanceTokensOrder[@]}"
do
  key="${instanceTokensOrder[$i]}"
  if [[ "$key" == "X_empty_X" ]]; then
    #echo "empty"
    echo -en '\n' >> "${INSTANCE_FILE}"
  elif [[ ${key:0:10} == "X_comment_" ]]; then
    #echo "comment"
    echo "${instanceComments[${key}]}" >> "${INSTANCE_FILE}"
  elif [[ "${instanceTokens[${key}]}" != "X_duplicate_X" ]]; then
    #echo "$key"
    echo "${key}=${instanceTokens[${key}]}" >> "${INSTANCE_FILE}"
  fi
done
