#!/bin/bash

set -xe

SOURCE_REPOSITORIES=(
  ipl-web
  ipl-validator
  icingaweb2
  icingaweb2-module-director
  icingaweb2-module-vspheredb
  icingadb-web
  icingaweb2-module-reporting
  icingaweb2-module-graphite
  icingaweb2-module-cube
  #icingaweb2-module-idoreports
  icingaweb2-module-aws
  icingaweb2-module-businessprocess
  #icingaweb2-module-doc
  #icingaweb2-module-fileshipper
  icingaweb2-module-jira
  #icingaweb2-module-nagvis
  icingaweb2-module-pdfexport
  #icingaweb2-module-pnp
  #icingaweb2-module-puppetdb
  #icingaweb2-module-test
  #icingaweb2-module-vsphere
  icingaweb2-module-x509
  #icingaweb2-module-toplevelview
  icingaweb2-module-audit
  icingaweb2-module-elasticsearch
  #icingaweb2-module-eventdb
  #icingaweb2-module-generictts
  #icingaweb2-module-lynxtechnik
)

# Fetch repositories which do not exist yet in the directory
FETCHED_REPOS=();
for repo_name in "${SOURCE_REPOSITORIES[@]}"; do
  if [ ! -d "sources.d/$repo_name" ]; then
    FETCHED_REPOS+=( $repo_name );

    DEFAULT_BRANCH="master"
    HTTP_STATUS=$(curl -Is https://api.github.com/repos/Icinga/$repo_name/branches/master | head -n 1 | awk '{ print $2 }')
    if [ "$HTTP_STATUS" == "301" ]; then
      DEFAULT_BRANCH="main"
    fi

    wget -q -O - https://github.com/Icinga/$repo_name/archive/$DEFAULT_BRANCH.tar.gz | tar xz
    mv $repo_name-$DEFAULT_BRANCH/ sources.d/$repo_name
  fi
done

for repo_name in "${SOURCE_REPOSITORIES[@]}"; do
  if [ ! -d $repo_name ]; then
    mkdir $repo_name
  fi

  # Create a list of files xgettext should scan
  find -L sources.d/$repo_name -regex ".*\.\(php\|phtml\)" ! -path "*/vendor/*" ! -path "*/test/*" > $repo_name/catalog.txt

  xgettext --language=PHP \
           --keyword=translate \
           --keyword=translate:1,2c \
           --keyword=translatePlural:1,2 \
           --keyword=translatePlural:1,2,4c \
           --keyword=mt:2 \
           --keyword=mt:2,3c \
           --keyword=mtp:2,3 \
           --keyword=mtp:2,3,5c \
           --keyword=t \
           --keyword=t:1,2c \
           --keyword=tp:1,2 \
           --keyword=tp:1,2,4c \
           --keyword=N_ \
           --from-code=utf-8 \
           --files-from=$repo_name/catalog.txt \
           --sort-by-file \
           --copyright-holder="Icinga GmbH" \
           --package-name="Icinga L10n" \
           --package-version=$(git describe --always) \
           --default-domain=icinga \
           --output=$repo_name/messages.pot

  # Use current year in the copyright for new templates without one yet
  REPLACE_CODE=$(cat << PYTHON
import sys
input = sys.stdin.read()
from datetime import date
result = input.replace(
    '# Copyright (C) YEAR Icinga GmbH',
    '# Copyright (C) {0} Icinga GmbH'.format(date.today().year),
    1
)
sys.stdout.write(result)
PYTHON
)
  cat $repo_name/messages.pot | python3 -c "$REPLACE_CODE" > temp; mv temp $repo_name/messages.pot

  # Cleanup created files and directories
  rm $repo_name/catalog.txt
  if [[ " ${FETCHED_REPOS[*]} " =~ " ${repo_name} " ]]; then
    rm -rf sources.d/$repo_name
  fi
done

# Check for changes (new messages) that need to be committed
CHANGES=1
git diff -U0 \
  | grep -Pe '^[+-]{1}(?!#:)[^+-]+' \
  | grep -qv -e '."Project-Id-Version:' -e '."POT-Creation-Date:' \
|| CHANGES=0

if [ $CHANGES -eq 0 ]; then
  echo "No new messages found.";
  git checkout */messages.pot
else
  echo "New messages found!";
  # Working tree is left dirty as the following step creates a new pull request
fi
