#!/bin/sh
set -e

append_str() {
  while read -r msg; do
    msgfile=$(printf '%s' "$msg" | cut -d'|' -f1)
    msgline=$(printf '%s' "$msg" | cut -d'|' -f2)
    msgctxt=$(printf '%s' "$msg" | cut -d'|' -f3)
    msgid=$(printf '%s' "$msg" | cut -d'|' -f4-)

    if [ -z "$msgctxt" ]; then
        printf "\n#: %s:%s\nmsgid %s\nmsgstr \"\"\n\n" \
          "$msgfile" "$msgline" "$msgid" >> aegisub.pot
    else
        printf "\n#: %s:%s\nmsgctxt %s\nmsgid %s\nmsgstr \"\"\n\n" \
          "$msgfile" "$msgline" "$msgctxt" "$msgid" >> aegisub.pot
    fi
  done
}

find ../src ../src/command -name '*.cpp' -o -name '*.h' \
  | xgettext --files-from=- -o - --c++ --sort-by-file \
             -k_ -kwxTRANSLATE -kwxGETTEXT_IN_CONTEXT:1c,2 -kSTR_MENU -kSTR_DISP -kSTR_HELP -kCOMMAND_GROUP:5 \
             -kfmt_tl -kfmt_plural:2,3 \
  | sed 's/SOME DESCRIPTIVE TITLE./Aegisub 3.5/' \
  | sed 's/YEAR/2005-2014/' \
  | sed "s/THE PACKAGE'S COPYRIGHT HOLDER/Rodrigo Braz Monteiro, Niels Martin Hansen, Thomas Goyne et. al./" \
  | sed 's/PACKAGE/Aegisub/' \
  | sed 's/VERSION/3.5.0/' \
  | sed 's/FIRST AUTHOR <EMAIL@ADDRESS>/Niels Martin Hansen <nielsm@aegisub.org>/' \
  | sed 's/CHARSET/UTF-8/' \
  > aegisub.pot

for f in default_menu.json default_menu_platform.json osx/default_menu.json; do
    sed '/"text"/!d;s/^.*"tlcontext" : \("[^"]*"\).*"text" : \("[^"]*"\).*$/default_menu.json|0|\1|\2/;s/^.*"text" : \("[^"]*"\).*$/default_menu.json|0||\1/' ../src/libresrc/"$f" \
      | append_str
done

grep '"[A-Za-z ]*" : {' -n ../src/libresrc/default_hotkey.json \
  | sed 's/^\([0-9]*\):.*\("[^"]*"\).*$/default_hotkey.json|\1||\2/' \
  | append_str

find ../automation -name '*.lua' -o -name '*.moon' \
  | LC_ALL=C sort \
  | xargs grep 'tr"[^"]*"' -o -n \
  | sed 's/\(.*\):\([0-9]*\):tr\(".*"\)/\1|\2||\3/' \
  | append_str

grep '^_[A-Za-z0-9]*=.*' ../packages/win_installer/fragment_strings.iss.in | while read line
do
  printf '%s\n' "$line" \
    | sed 's/[^=]*=\(.*\)/packages\/win_installer\/fragment_strings.iss|1||"\1"/' \
    | append_str
done

# Keep the xgettext calls last so that they normalize the format after our manual patching

xgettext ../packages/desktop/aegisub.desktop.in.in \
  --language=Desktop --join-existing --omit-header -o aegisub.pot

xgettext ../packages/desktop/aegisub.metainfo.xml.in.in \
  --language=AppData --join-existing --omit-header -o aegisub.pot
