#!/usr/bin/env bash

clear

function usage {
    echo "Error: $*"
    echo " "
    echo "usage: $0 <test-set> <environment>"
    echo " "
    echo "where: test-set in 'MOTD', 'CRON', 'all' (default)"
    echo "       environment in 'dev' (default), 'test', 'prod'"
    exit 1
}

function select_values {
case $1 in
    (MOTD | all)
        concat_array MOTDTESTS TESTS
        concat_array MOTDPARAMS PARAMS
        ;&
    (CRON | all)
        concat_array CRONTESTS TESTS
        concat_array CRONPARAMS PARAMS
        ;;
    *)
        echo "WTF? WHICHSET = '$WHICHSET'"
        exit 1
esac
}

function concat_array {
    for key in $($1); do
        ${2}+=( [$key]=${1[$key]} )
    done
}

WHICHSET=${1:-all}
WHICHENV=${2:-dev}
if [[ ! $WHICHSET =~ (MOTD|CRON|all) ]]; then
    usage "invalid test set '$WHICHSET'"
fi
if [[ ! $WHICHENV =~ (dev|test|prod) ]]; then
    usage "invalid environment '$WHICHENV'"
fi

NOW=$(date +%Y-%m-%d_%H:%M:%s)

declare -A ENVIRONMENTS
ENVIRONMENTS=([dev]="localhost:5000"
              [test]="dad-test.zooxlabs.com"
              [prod]="dad.zooxlabs.com"
              )

declare -A TESTS
declare -A PARAMS

declare -A MOTDTESTS
MOTDTESTS=([110]="show"
       [120]="hide"
       [130]="set"
       [140]="clear"
       )

declare -A CRONTESTS
CRONTESTS=([10]="update_merge_batch"
       [20]="update_merge_batch"
       [25]="update_merge_batch"
       [30]="merge_batch_from_s3"
       [35]="merge_batch_from_s3"
       [40]="merge_batch_from_s3"
       [50]="github_mergeable_retry"
       [55]="github_mergeable_retry"
       [60]="github_mergeable_retry"
       [70]="pull_requests_from_github"
       [75]="pull_requests_from_github"
       [80]="pull_requests_from_github"
       [85]="pull_requests_from_github"
       )

declare -A MOTDPARAMS
MOTDPARAMS=([110]=""
        [111]="message=<a href=\"mailto:rfagen@zoox.com?subject=DAD%20Feedback&amp;body=the%20body%0D%0Aline%202\">"
        [112]="message"
        [113]="alert_level=info"
        [114]="alert_level=warning"
        [115]="alert_level=warming"
        [116]="alert_level=danger"
        [120]=""
        [111]="message"
        [112]="message"
        [113]="alert_level=info"
        [114]="alert_level=warning"
        [115]="alert_level=warming"
        [116]="alert_level=danger"
        [130]=""
        [111]="message"
        [112]="message"
        [113]="alert_level=info"
        [114]="alert_level=warning"
        [115]="alert_level=warming"
        [116]="alert_level=danger"
        [140]=""
        [111]="message"
        [112]="message"
        [113]="alert_level=info"
        [114]="alert_level=warning"
        [115]="alert_level=warming"
        [116]="alert_level=danger"
)

declare -A CRONPARAMS
CRONPARAMS=([10]="--update-log-tests&--debug"
        [20]="--update-log-tests"
        [25]="--debarge"
        [30]="--debug"
        [35]="--debarge"
        [40]=""
        [50]="--debug"
        [55]="--debarge"
        [60]=""
        [70]="--debug"
        [75]="--scope=full&--state=all"
        [80]="--scope=fool"
        [85]="--state=closed&--scope=incremental"
        )

select_values $WHICHSET

echo "${TESTS}"
echo "${PARAMS}"
exit

for scrnm in ${!PARAMS[@]}
do
        echo curl -L -m 3600 -o log.${NOW}.${TESTS[${scrnm}]}.${scrnm} "http://${ENVIRONMENTS[$WHICHENV]}/api/crontrigger/${TESTS[${scrnm}]}?${PARAMS[${scrnm}]}"
        curl -L -m 3600 -o log.${NOW}.${TESTS[${scrnm}]}.${scrnm} "http://${ENVIRONMENTS[$WHICHENV]}/api/crontrigger/${TESTS[${scrnm}]}?${PARAMS[${scrnm}]}"
        DAD_ENVIRONMENT=development ./wrapper.sh ${TESTS[${scrnm}]}
done

tail log.${NOW}.*

echo "http://eponymous:5000/api/MOTD/show?message=%3Ca%20href%3D%22mailto%3Arfagen%40zoox.com%3Fsubject%3DDAD%2520Feedback%26amp%3Bbody%3Dplease%20copy%20and%20paste%20what%20looks%20funny%20and%20send%20it%20to%20me.%250A%250A%250A%250AThanks%21%22%3ESend%20Feedback%3C%2Fa%3E%3Cp%3EPlease%20click%20to%20email%2C%20and%20then%20copy%20and%20paste%20what%20looks%20funny%3C%2Fp%3E&alert_level=info"
