#!/usr/bin/env bash

# Safe bash settings
# -e            Exit on command fail
# -u            Exit on unset variable
# -o pipefail   Exit if piped command has error code
set -eu -o pipefail

cd "$(dirname "$0")/.."

if [ -z "${LOKALISE_TOKEN-}" ] && [ ! -f .lokalise_token ] ; then
    echo "Lokalise API token is required to download the latest set of" \
        "translations. Please create an account by using the following link:" \
        "https://lokalise.co/signup/3420425759f6d6d241f598.13594006/all/" \
        "Place your token in a new file \".lokalise_token\" in the repo" \
        "root directory."
    exit 1
fi

# Load token from file if not already in the environment
[ -z "${LOKALISE_TOKEN-}" ] && LOKALISE_TOKEN="$(<.lokalise_token)"

PROJECT_ID="3420425759f6d6d241f598.13594006"
LOCAL_DIR="$(pwd)/translations/downloads"
FILE_FORMAT=json

mkdir -p ${LOCAL_DIR}

docker run \
     -v ${LOCAL_DIR}:/opt/dest/locale \
     --rm \
     lokalise/lokalise-cli-2@sha256:f1860b26be22fa73b8c93bc5f8690f2afc867610a42de6fc27adc790e5d4425d lokalise2 \
     --token ${LOKALISE_TOKEN} \
     --project-id ${PROJECT_ID} \
     file download \
     --export-empty-as skip \
     --format json \
     --json-unescaped-slashes=true \
     --replace-breaks=false \
     --original-filenames=false \
     --unzip-to /opt/dest

./node_modules/.bin/gulp check-downloaded-translations