#!/bin/bash

tokens_file='/etc/direct-tokens/api_test_tokens'
client_login='at-direct-script-client'
operator_login='at-direct-super'
oauth_token=$(grep $operator_login $tokens_file |awk '{print $2}' | head -n1)


SCRIPTPATH=`readlink -f $0`
SCRIPTDIR=`dirname $SCRIPTPATH`

source $SCRIPTDIR/beta_number

SERVICE_URL="$JSON_URL/dynamictextadtargets"
#echo $SERVICE_URL

REQUEST='
  {
    "method":"add",
    "params": {
        "Webpages" : [
            {
                    "StrategyPriority": "LOW",
                    "AdGroupId": 1201976301,
                    "Bid": 1500000,
                    "ContextBid": 1500000,
                    "Conditions": [
                        {
                            "Operand": "DOMAIN",
                            "Operator": "NOT_CONTAINS_ALL",
                            "Arguments": [ "dom1.ru" ]
                        }
                    ],
                    "Name": "name1"
            },
            {
                    "StrategyPriority": "LOW",
                    "AdGroupId": 1202120791,
                    "Bid": 1500000,
                    "ContextBid": 1500000,
                    "Conditions": [
                        {
                            "Operand": "DOMAIN",
                            "Operator": "NOT_CONTAINS_ALL",
                            "Arguments": [ "dom1.ru" ]
                        }
                    ],
                    "Name": "name1"
            },
            {
                    "StrategyPriority": "LOW",
                    "AdGroupId": 1201976301,
                    "Bid": 1500000,
                    "ContextBid": 1500000,
                    "Conditions": [
                        {
                            "Operand": "DOMAIN",
                            "Operator": "CONTAINS_ANY",
                            "Arguments": [ "dom2.ru" ]
                        }
                    ],
                    "Name": "name2"
            },
            {
                    "StrategyPriority": "LOW",
                    "AdGroupId": 1201976301,
                    "Bid": 1500000,
                    "ContextBid": 1500000,
                    "Conditions": [
                        {
                            "Operand": "DOMAIN",
                            "Operator": "NOT_CONTAINS_ALL",
                            "Arguments": [ "asdasdasdasd.com" ]
                        }
                    ],
                    "Name": "name3"
            },
            {
                "AdGroupId" : 1176583716,
                "Name" : "Test all pages",
                "Bid" : 5000000,
                "ContextBid" : 10000000,
                "StrategyPriority" : "LOW"
            }
        ]
    }
  }';

case "$1" in
    *-r*)
        echo "$REQUEST"
        exit
        ;;
esac

curl \
     -k \
    -H "Authorization: Bearer $oauth_token" \
    -H "Client-Login: $client_login" \
    -d "$REQUEST" \
    "$SERVICE_URL"

echo
