#!/bin/bash

#
# This script uploads a game-campaign.json file to the server
# The WORLD_GAME_Default campaign is for all users. The json
# file needs to be valid json, either the entire gamedb.json
# or a patch/subset of it.
#
# This script is tested to work under cygwin bash on windows too
#

if [ $# -lt 4 ]
then
  echo "Wrong number of arguments"
  echo "Usage:"
  echo "    $0  campaign.json  ClientId  UserType:UserName  Password  [DataCenter] Campaign_name segment_name"
  echo "Example:"
  echo "    $0  gamedb-patch.json  2204:58739:0.0.1:ios:appstore  game:surv_gs_user  password  mdc WORLD_GAME_Default WORLD_GAME"
  exit 0
fi

# JSON file
CAMPAIGN_FILE=$1

# GameLoft ClientId for the game
CLIENT_ID=$2

# Credentials
USERNAME=$3
PASSWORD=$4
SCOPE="config config_structure"

DATACENTER=$5
if [ "$DATACENTER" == "" ]
then
  DATACENTER="mdc"
fi

# Campaign to update
CAMPAIGN=$6
# Segment to use
SEGMENT=$7

# Servers
EVE_URL=http://eve.gameloft.com:20001
# Get Pandora URL from EVE
PANDORA_URL=`curl -s $EVE_URL/config/$CLIENT_ID/datacenters/$DATACENTER/urls | sed 's/\,/,\'$'\n/g' | grep "pandora" | cut -d '"' -f 4`
# Translate/redirect beta/gold address to the beta or gold master address to ensure it can work and it will be replicated to all DCs
# Note: .com -> .org  (http://palantir.gameloft.org/master_fed.html)
PANDORA_URL=`echo $PANDORA_URL | sed 's/vbeta\.gameloft\.com/vbeta-master\.gameloft\.org/' | sed 's/vgold\.gameloft\.com/vgold-master\.gameloft\.org/'`
# Get Janus URL from Pandora
AUTH_URL=`curl -s $PANDORA_URL/locate/auth`
# Get Hestia URL from Pandora
CONFIG_URL=`curl -s $PANDORA_URL/locate/config`

# Authorize and get access-token
ACCESS_TOKEN=`curl -k -s -d '' https://$AUTH_URL/authorize -d "username=$USERNAME&password=$PASSWORD&scope=$SCOPE&client_id=$CLIENT_ID" | cut -d '"' -f 4`

echo
echo "Updating $CAMPAIGN_FILE to CRM"
echo
echo "  Client ID:    $CLIENT_ID"
echo "  Data Center:  $DATACENTER"
echo "  Username:     $USERNAME"
echo "  Eve:          $EVE_URL"
echo "  Pandora:      $PANDORA_URL"
echo "  Janus:        $AUTH_URL"
echo "  Hestia:       $CONFIG_URL"
echo "  Access token: $ACCESS_TOKEN"
echo

# A temp file to concatenate data in to (the .XXX part is for cygwin mktemp compatibility)
TMP_CAMPAIGN_FILE=`mktemp -t tmp-campaign-cluster.XXX`

# Pack the campaign_cluster data in to a file for posting (could be too large for passing
# through the command line)
echo 'campaign_clusters=[{"matcher":"world","id":"'$CAMPAIGN'","effects":[{
     "operation":"set","optional":false,"id":null,"value":' > $TMP_CAMPAIGN_FILE
cat $CAMPAIGN_FILE >> $TMP_CAMPAIGN_FILE
echo ',"selector":"game"}]}]' >> $TMP_CAMPAIGN_FILE

# Make the POST request to hestia to upload the game campaign
echo
echo "Server response:"
echo
curl -k -s -d '' https://$CONFIG_URL/configs/campaigns/$CAMPAIGN \
	--data-urlencode "access_token=$ACCESS_TOKEN" \
	--data-urlencode "name=$CAMPAIGN" \
    --data-urlencode "start_date=2015-12-01 00:00:00Z" \
	--data-urlencode "users_segment_id=$SEGMENT" \
	--data-urlencode "override=True" \
	--data-urlencode "priority=1.0" \
	--data-binary @$TMP_CAMPAIGN_FILE

echo
rm $TMP_CAMPAIGN_FILE
echo


