. $DIR/.ltm_instance_$GCE_PROJECT

# The hostname has to match the common name field of the certificate, as set
# in util/gce-do-setup when running openssl to create a self-signed cert.
# curl will fail otherwise, stating that the common names do not match.
LTM_HOSTNAME="a.$GCE_PROJECT.gce-xfstests"
function ltm_post_json() {
    if test -n "$GCE_LTM_INT_IP"; then
    curl -s --cacert $GCE_LTM_SERVER_CERT -H "Content-Type: application/json" \
	--resolve "$LTM_HOSTNAME:443:$GCE_LTM_INT_IP" -X POST "$@" -k | jq '.'
    else
    curl -s --cacert $GCE_LTM_SERVER_CERT -H "Content-Type: application/json" \
	--resolve "$LTM_HOSTNAME:443:$GCE_LTM_EXT_IP" -X POST "$@" -k | jq '.'
    fi
}

function ltm_post_failed() {
    if run_gcloud compute instances describe "xfstests-ltm" &> /dev/null; then
	echo "The ltm instance seems to be running. Try deleting the"
	echo "$DIR/.ltm_instance_$GCE_PROJECT file and re-running launch-ltm to recreate"
	echo "the file. If the issue persists, try relaunching the LTM"
	echo "If configuration changes have been made, it may be necessary"
	echo "to delete and re-launch the LTM."
    else
	echo "The ltm doesn't seem to be running. Try launching the ltm"
	echo "instance with launch-ltm. This will recreate the"
	echo ".ltm_instance_$GCE_PROJECT file."
    fi
}

function send_to_ltm() {
    local cmd_to_send=$1
    shift

        # Failed login will create an empty cookie file, so ensure
        # the file exists and contains a cookie - sometimes ltm_post_json
        # will succeed even when login fails, so we cannot simply remove
        # the cookie file upon ltm_post_json failure
    if test ! -f "$DIR/.ltm_cookie_$GCE_PROJECT" || \
            ! grep "a.$GCE_PROJECT.gce-xfstests" "$DIR/.ltm_cookie_$GCE_PROJECT" &> /dev/null
    then
        echo "login attempt " >> /tmp/ltm-auto-resume.debug
        # just create a new login session and store it in the cookie
        ltm_post_json -c $DIR/.ltm_cookie_$GCE_PROJECT -d "{\"password\":\"$GCE_LTM_PWD\"}" \
            "https://$LTM_HOSTNAME/login"

        if [ ! $? = 0 ]; then
        echo "Login failed."
        ltm_post_failed
        return 1
        fi
        echo
    fi

    if test -n "$LTM_INFO"; then
        ltm_post_json -b $DIR/.ltm_cookie_$GCE_PROJECT "https://$LTM_HOSTNAME/status"
        if [ $? != 0 ]; then
        echo "Request failed."
        ltm_post_failed
        return 1
        fi
        echo
    else
    LTM_OPTS=""
    if [ -n "$NO_REGION_SHARD" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"no_region_shard\":true"
    fi
    if [ -n "$BUCKET_SUBDIR" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"bucket_subdir\":\"$BUCKET_SUBDIR\""
    fi
    if [ -n "$GS_KERNEL" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"gs_kernel\":\"$GS_KERNEL\""
    fi
    if [ -n "${GCE_REPORT_EMAIL+x}" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"report_email\":\"$GCE_REPORT_EMAIL\""
    fi
    if [ -n "${GCE_JUNIT_EMAIL+x}" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"junit_email\":\"$GCE_JUNIT_EMAIL\""
    fi
    if [ -n "$COMMIT" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"commit_id\":\"$COMMIT\""
    fi
    if [ -n "$GIT_REPO" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"git_repo\":\"$GIT_REPO\""
    fi
    if [ -n "$BRANCH" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"branch_name\":\"$BRANCH\""
    fi
    if [ -n "$WATCHER_ID" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"unwatch\":\"$WATCHER_ID\""
    fi
    if [ -n "$BISECT_BAD" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"bad_commit\":\"$BISECT_BAD\""
    fi
    if [ -n "$BISECT_GOOD" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"good_commit\":\"$BISECT_GOOD\""
    fi
    if [ -n "$KCONFIG" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"kconfig\":\"$KCONFIG\""
    fi
    if [ -n "$KCONFIG_OPTS" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"kconfig_opts\":\"$KCONFIG_OPTS\""
    fi
    if [ -n "$KBUILD_OPTS" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"kbuild_opts\":\"$KBUILD_OPTS\""
    fi
    if [ -n "$ARCH" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"arch\":\"$ARCH\""
    fi
    if [ -n "$MONITOR_TIMEOUT" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"monitor_timeout\":\"$MONITOR_TIMEOUT\""
    fi
    if [ -n "$TESTRUNID" ]; then
	LTM_OPTS="${LTM_OPTS:+$LTM_OPTS, }\"test_run_id\":\"$TESTRUNID\""
    fi
    if [ -n "$LTM_OPTS" ]; then
	LTM_OPTS="\"options\": {$LTM_OPTS}"
    fi
    # Create OPTS.

    ltm_post_json -b $DIR/.ltm_cookie_$GCE_PROJECT -d \
	"{\"orig_cmdline\": \"$cmd_to_send\"${LTM_OPTS:+, $LTM_OPTS}}" \
	"https://$LTM_HOSTNAME/gce-xfstests"

    if [ $? != 0 ]; then
	echo "Request failed."
	ltm_post_failed
	return 1
    fi
    echo

    fi
}
