#!/bin/sh

setup() {
    echo "Setting up ${1}"
    cd $cwd
    cd $1

    git checkout $RELEASEBRANCH
    git pull
    cd js/packages/agelocme-mobile
    npm install
}

build() {
    echo "Building ${1}"
    cd $cwd
    cd $1
    cd js/packages/agelocme-mobile

    if [[ $1 == *"emea"* ]]
    then
        gulp build --prod
        appID=2b466de413781960d3ab5a9227a838cf
    elif [[ $1 == *"sea"* ]]
    then
        gulp build --sea --prod
        appID=2375133862fabd66740406bdffb98a98
    elif [[ $1 == *"spac"* ]]
    then
        gulp build --spac --prod
        appID=226ad053f7b248b384ca36650c93002f
    elif [[ $1 == *"na"* ]]
    then
        gulp build --na --prod
        appID=8bbfddd7b4f74cef86bc650bfe5ae57f
    elif [[ $1 == *"genloc"* ]]
    then
        gulp build --japan --prod
        appID=5c4f7d7aa2ce3a6987af63d41730afcb
    fi

    grunt --environment prod
    grunt images --environment prod

    phonegap platform add android
    cp $cwd/release-signing.properties platforms/android/

    phonegap build android --release
#    There is an issue with the gigya plugin.
#       On android it likes the version to be "~1.0.0"
#       On iOS it likes it to be "https://github.com/gigya/cordova-plugin-gigya"
#
#       The cordova-plugin-gigya needs to be removed after android is done, then change
#       config-base.xml then do another gulp build and phonegap build ios.

#   Not using the gigya plugin because of too many issues.  Go ahead and build ios here.
    phonegap build ios

    puck -submit=auto -api_token=db160347d6c24aaebb7875396f0fdbe2 -app_id=$appID -notify=false -notes=ProductionBuild -download=true platforms/android/build/outputs/apk/android-release.apk
}

buildIos() {
    echo "Building ${1}"
    cd $cwd
    cd $1
    cd js/packages/agelocme-mobile

#    There is an issue with the gigya plugin.
#       On android it likes the version to be "~1.0.0"
#       On iOS it likes it to be "https://github.com/gigya/cordova-plugin-gigya"
#
#       The cordova-plugin-gigya needs to be removed after android is done, then change
#       config-base.xml then do another gulp build and phonegap build ios.

    phonegap plugin rm corodva-plugin-gigya

    if [[ $1 == *"emea"* ]]
    then
        gulp build --ios --prod
    elif [[ $1 == *"sea"* ]]
    then
        gulp build --ios --sea --prod
    elif [[ $1 == *"spac"* ]]
    then
        gulp build --ios --spac --prod
    elif [[ $1 == *"na"* ]]
    then
        gulp build --ios --na --prod
    elif [[ $1 == *"genloc"* ]]
    then
        gulp build --ios --japan --prod
    fi

    phonegap plugin add cordova-plugin-gigya

    phonegap build ios
}



# Check to make sure the user has all the required command line programs
command -v git >/dev/null 2>&1 || { echo >&2 "You must have git installed. Aborting build"; exit 1; }
command -v npm >/dev/null 2>&1 || { echo >&2 "You must have npm installed. Aborting build"; exit 1; }
command -v puck >/dev/null 2>&1 || { echo >&2 "You must have puck cli installed from the hockeyapp website. Aborting build"; exit 1; }

# Navigate to the directory the script exists in
scriptdir=`dirname "$BASH_SOURCE"`
cd $scriptdir
cwd=`pwd`

#prompt for release branch
echo "What branch do you want to build from"
read RELEASEBRANCH

#read the current location for the jks file to sign the release build with
jksFile=$(head -n 1 release-signing.properties | cut -d "=" -f 2)
#prompt user for their location of the jks file
read -p "What is the location of your jks file: [$jksFile] " -e newJksFile
newJksFile=${newJksFile:-$jksFile}

# if we have a new location then update release-signing.properties with the new location of the jks
if [ $jksFile != $newJksFile ]
then
    sed -i -e "s|$jskFile|$newJksFile|" release-signing.properties
fi

#cleanup projects
echo "Root "
sudo rm -r ns*

# clone project for each market
git clone ssh://git@bitbucket.nuskin.net/fa/webcomponents.git nsagelocmeemea
git clone ssh://git@bitbucket.nuskin.net/fa/webcomponents.git nsagelocmesea
git clone ssh://git@bitbucket.nuskin.net/fa/webcomponents.git nsagelocmespac
git clone ssh://git@bitbucket.nuskin.net/fa/webcomponents.git nsagelocmena
git clone ssh://git@bitbucket.nuskin.net/fa/webcomponents.git nsgenlocme

# run npm install and jspm install for each market
setup nsagelocmeemea
setup nsagelocmesea
setup nsagelocmespac
setup nsagelocmena
setup nsgenlocme

#build the app for each market and upload to hockeyapp
build nsagelocmeemea
build nsagelocmesea
build nsagelocmespac
build nsagelocmena
build nsgenlocme
#buildIos nsagelocmeemea
#buildIos nsagelocmesea
#buildIos nsagelocmespac
#buildIos nsagelocmena
#buildIos nsgenlocme

echo "Android builds have been uploaded to hockeyapp.  IOS builds are built but xcode needs to be opened to make necessary changes and upload to testflight through xcode."


