#!/bin/bash

#
#
# This script downloads all translations from crowdin,
# and make a pull request on github if necessary
#
#

# see: https://crowdin.com/page/api/export
# see: https://crowdin.com/page/api/download

. "$(dirname $0)/globals"

BRANCH=crowdin_translations
GITHUB_PR_TITLE="crowdin: New translations"
PR_EXISTS=0
DDATE=$(date +"%Y-%m-%d %H:%M:%S")
ZIPFILE=all.zip
#default base branch
BASE_BRANCH=master

finish () {
    debug "cleaning up"
    # revert everything until last commit
    _do git branch | grep -q "\* ${BRANCH}" && ( _do git checkout . ; _do git reset ; )
    _do git checkout ${BASE_BRANCH} || exit 1
    [[ -f ${ZIPFILE} ]] && _do rm "${ZIPFILE}"
}

# current branch
CURRENT_BRANCH=`git name-rev --name-only HEAD`
BRANCH_SOURCE='\&branch=master'

# check if we should create a PR
DO_PR=0
if [ $# -eq 1 ]; then
    if [ $1 -eq pr ]; then
        DO_PR=1
    fi
fi

# check branch
if [ $CURRENT_BRANCH = release ]; then
    debug "Syncing with branch 'release'"
    BASE_BRANCH=release
    BRANCH_SOURCE='\&branch=release'
fi

# update BASE_BRANCH
_do git pull upstream ${BASE_BRANCH} || die "couldn't git pull upstream ${BASE_BRANCH}."

# check for existing PR
_do curl -i --get "https://api.github.com/repos/cgeo/cgeo/pulls" -o "${OUT}" \
    || die "listing pull requests failed."
grep -q "Status: 200 OK" "${OUT}" || ( cat "${OUT}" ; die "reading list of pull requests failed." ; )
grep -qF "${GITHUB_PR_TITLE}" "${OUT}" && PR_EXISTS=1

if [ $PR_EXISTS -eq 0 ]; then
    debug "We don't have an open Pull Request on github."
    # remove branch if exists
    _do git checkout ${BASE_BRANCH} || die "Couldn't git checkout ${BASE_BRANCH}."
    _do git branch -D "${BRANCH}"
else
    debug "We have an open Pull Request on github."
fi

# prepare branch for PR
if [ $DO_PR -eq 1 ]; then
    if git branch | grep -q "${BRANCH}"; then
        :
    else
        _do git branch "${BRANCH}" || die "Couldn't create branch."
    fi
    _do git checkout "${BRANCH}" || die "Couldn't switch to branch."
fi

# package the language files (allowed every 30 min)
debug "packaging language files."
crowdin_surf "https://api.crowdin.com/api/project/cgeo/export?key=${CROWDIN_APIKEY}${BRANCH_SOURCE}"

# download and unpack translations
[[ -f ${ZIPFILE} ]] && rm ${ZIPFILE}
_do wget "https://api.crowdin.com/api/project/cgeo/download/all.zip?key=${CROWDIN_APIKEY}${BRANCH_SOURCE}" -O ${ZIPFILE} \
     || die "crowdin download failed."
_do unzip -o ${ZIPFILE} || die "unzip of ${ZIPFILE} failed."

# fix unicode entities for ellipsis character
for f in */*/*values-*/strings.xml; do
  sed -i 's/&#8230;/…/g' $f
done

# apply local patches to particular strings
. "$progdir/local-patches"

# check for changes
if [ -z "$(git diff)" ]; then
    debug "no changes, finishing."
    finish
    exit
fi

# commit changes either to BASE_BRANCH or translations BRANCH, depending on whether we prepare a PR
AMEND=""
if [ $DO_PR -eq 1 ]; then
    [[ ! -z "$(git log ${BASE_BRANCH}..${BRANCH})" ]] && AMEND="--amend"
fi

_do git commit -a "${AMEND}" -m \"${GITHUB_PR_TITLE}\" || die "commit failed."

# prepare and create PR
if [ $DO_PR -eq 1 ]; then
    _do git push -f origin "${BRANCH}" || die "git push failed."

    # create pull request
    if [ $PR_EXISTS -eq 0 ]; then
        _do curl -i -u "${GITHUB_USER}:${GITHUB_PASSWORD}" -d \'{\"title\":\"${GITHUB_PR_TITLE}\",\"body\":\"downloaded ${DDATE}\",\"head\":\"${GITHUB_USER}:${BRANCH}\",\"base\":\"${BASE_BRANCH}\"}\' "https://api.github.com/repos/cgeo/cgeo/pulls" -o "${OUT}" || die "creating the pull request failed."
        grep -q "201 Created" "${OUT}" || die "pull request not created."
    fi
fi

# clean up
finish

