#!/bin/bash

# Generate JS bindings for Cocos2D-X
# ... using Android NDK system headers
# ... and push these changes to remote repos

# Dependencies
#
# For bindings generator:
# (see tools/tojs/genbindings.py for the defaults used if the environment is not customized)
#
#  * $PYTHON_BIN
#  * $CLANG_ROOT
#  * $NDK_ROOT
#

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_ROOT="$DIR"/../..
TOJS_ROOT="$PROJECT_ROOT"/tools/tojs
GENERATED_WORKTREE="$PROJECT_ROOT"/frameworks/js-bindings/bindings/auto
COMMITTAG="[AUTO]"

# Exit on error
set -e

if [ "$PLATFORM"x = "ios"x ]; then
    mkdir -p $HOME/bin
    pushd $HOME/bin
    curl -O http://pyyaml.org/download/pyyaml/PyYAML-3.10.zip
    unzip PyYAML-3.10.zip 2> /dev/null > /dev/null
    cd PyYAML-3.10
    sudo python setup.py install 2> /dev/null > /dev/null
    cd ..
    curl -O https://pypi.python.org/packages/source/C/Cheetah/Cheetah-2.4.4.tar.gz
    tar xzf Cheetah-2.4.4.tar.gz
    cd Cheetah-2.4.4
    sudo python setup.py install 2> /dev/null > /dev/null
    popd
elif [ $(command -v apt-get) ]; then
    sudo apt-get --force-yes --yes install python-yaml python-cheetah
fi

generate_bindings_glue_codes()
{
    echo "Create auto-generated jsbinding glue codes."
    pushd "$TOJS_ROOT"
    ./genbindings.py
    popd
}

if [ "$GEN_BINDINGS"x != "YES"x ]; then
    generate_bindings_glue_codes
    exit 0
fi

pushd "$PROJECT_ROOT"
#Set git user for cocos2d-js repo
git config user.email ${GH_EMAIL}
git config user.name ${GH_USER}
popd

rm -rf "$GENERATED_WORKTREE"
mkdir "$GENERATED_WORKTREE"

# 1. Generate js bindings
generate_bindings_glue_codes

echo
echo Bindings generated successfully
echo


if [ -z "${COMMITTAG+aaa}" ]; then
# ... if COMMITTAG is not set, use this machine's hostname
    COMMITTAG=`hostname -s`
fi

echo
echo Using "'$COMMITTAG'" in the commit messages
echo

ELAPSEDSECS=`date +%s`
echo Using "$ELAPSEDSECS" in the branch names for pseudo-uniqueness

GENERATED_BRANCH=autogeneratedbindings_"$ELAPSEDSECS"


# 2. In Bindings repo, Check if there are any files that are different from the index

pushd "$PROJECT_ROOT"

# Run status to record the output in the log
git status

echo
echo Comparing with HEAD ...
echo

# Don't exit on non-zero return value
set +e
git diff --stat --exit-code

DIFF_RETVAL=$?
if [ $DIFF_RETVAL -eq 0 ]
then
    echo
    echo "No differences in generated files"
    echo "Exiting with success."
    echo
    exit 0
else
    echo
    echo "Generated files differ from HEAD. Continuing."
    echo
fi

# Exit on error
set -e


COCOS_BRANCH=update_js_bindings_"$ELAPSEDSECS"

git add -f --all "$GENERATED_WORKTREE"
git checkout -b "$COCOS_BRANCH"
git commit -m "$COMMITTAG : updating jsbinding automatically"

#Set remotes
git remote add upstream https://${GH_USER}:${GH_PASSWORD}@github.com/${GH_USER}/cocos2d-js.git 2> /dev/null > /dev/null

echo "Pushing to Robot's repo ..."
git push -fq upstream "$COCOS_BRANCH" 2> /dev/null

# 7. 
echo "Sending Pull Request to base repo ..."
curl --user "${GH_USER}:${GH_PASSWORD}" --request POST --data "{ \"title\": \"$COMMITTAG : updating jsbinding automatically\", \"body\": \"\", \"head\": \"${GH_USER}:${COCOS_BRANCH}\", \"base\": \"${TRAVIS_BRANCH}\"}" https://api.github.com/repos/cocos2d/cocos2d-js/pulls 2> /dev/null > /dev/null

popd