#!/usr/bin/env bash

set -eu

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"

# shellcheck disable=SC1091
source utils/log

function main {
    case ${1:-""} in
        prepare) prepare_localization_strings;;
        upload) upload_to_crowdin;;
        download) download_from_crowdin;;
        sync-local-files) sync_localizations;;
        verify) verify;;
        "")
            echo "Available subcommands: prepare, upload, download, sync-local-files and verify"
            ;;
        *)
            echo "Unknown parameter: $1"
            exit 1
            ;;
    esac
}

function sync_localizations {
    # Update desktop strings in messages.pot
    log_header "Extracting localization strings from desktop app source code"
    pushd ../desktop/packages/mullvad-vpn
    npm run update-translations
    popd

    # Update android strings and add Android strings to messages.pot
    log_header "Extracting localization strings from android app source code"
    pushd ../android/translations-converter/
    cargo run
    popd
}

function update_relay_locations_pot {
    log_header "Retrieving relay locations from server list and translating by using map data"
    pushd ../desktop/packages/mullvad-vpn/scripts

    # Add translations from geo data
    python3 fetch-relay-locations.py
    python3 integrate-relay-locations.py

    popd
}

function commit_changes {
    if [[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]]; then
        git commit -a -S -m "$1"
    fi
}

function prepare_localization_strings {
    sync_localizations
    commit_changes "Update messages.pot"

    update_relay_locations_pot
    commit_changes "Update relay-locations.pot"
}

function ensure_crowdin_api_key {
    test ! -z "$CROWDIN_API_KEY"
}

function upload_to_crowdin {
    ensure_crowdin_api_key

    log_header "Uploading translations to crowdin"

    pushd ../desktop/packages/mullvad-vpn
    crowdin upload sources
    crowdin upload translations
    popd
}

function download_from_crowdin {
    ensure_crowdin_api_key

    log_header "Downloading translations from crowdin"
    pushd ../desktop/packages/mullvad-vpn
    crowdin download
    popd

    # Add new translations to Android xml-files
    log_header "Updating Android xml-files with new translations"
    sync_localizations
    commit_changes "Update translations"
}

function verify {
    sync_localizations
    git diff

    # shellcheck disable=SC2251
    ! git status -s | grep .
    local out_of_sync=$?

    pushd ../desktop/packages/mullvad-vpn
    npm exec ts-node scripts/verify-translations-format.ts
    local incorrect_format=$?
    popd

    if [ "$out_of_sync" -ne 0 ] || [ "$incorrect_format" -ne 0 ]; then
        exit 1
    fi
}

main "$@"

