#!/bin/bash

SCRIPT=$( basename "$0" )

VERSION="1.0.0"

if [ -z "$LINUX_PORT" ]; then
    LINUX_PORT=1337
fi

if [ -z "$LINUX_SERVER" ]; then
    LINUX_SERVER=localhost
fi

function usage
{
    local txt=(
        "Client script for verifying kmom04 routes."
        "Usage: $SCRIPT [options] <command> [arguments]"
        ""
        "Commands:"
        "  hello              Gets $LINUX_SERVER:$LINUX_PORT/"
        "  html               Gets $LINUX_SERVER:$LINUX_PORT/index.html"
        "  status             Gets $LINUX_SERVER:$LINUX_PORT/status"
        "  sum [numbers]      Gets $LINUX_SERVER:$LINUX_PORT/sum?<prams>"
        "  filter [numbers]   Gets $LINUX_SERVER:$LINUX_PORT/filter?<params>"
        "  404                Gets $LINUX_SERVER:$LINUX_PORT/asd"
        "  all                Gets all above in the order they are displayed."
        ""
        "Options:"
        "  --help, -h     Print help."
        "  --version, -v  Print version."
    )

    printf "%s\n" "${txt[@]}"
}

function badUsage
{
    local message="$1"
    local txt=(
        "For an overview of the command, execute:"
        "$SCRIPT --help"
    )

    [[ $message ]] && printf "$message\n"

    printf "%s\n" "${txt[@]}"
}

function version
{
    local txt=(
        "$SCRIPT version $VERSION"
    )

    printf "%s\n" "${txt[@]}"
}

function concatinate
{
    local concatinated=""
    for str in "$@"; do
        if [[ "$concatinated" != "" ]]; then
            concatinated+="&"
        fi
        concatinated+=$str
    done

    echo "$concatinated"
}

function app-hello
{
    curl "$LINUX_SERVER:$LINUX_PORT/"
}

function app-html
{
    curl "$LINUX_SERVER:$LINUX_PORT/index.html"
}

function app-status
{
    curl "$LINUX_SERVER:$LINUX_PORT/status"
}

function app-sum
{
    local query=$(concatinate $@)
    curl "$LINUX_SERVER:$LINUX_PORT/sum?$query"
}

function app-filter
{
    local query=$(concatinate $@)
    curl "$LINUX_SERVER:$LINUX_PORT/filter?$query"
}

function app-404
{
    curl "$LINUX_SERVER:$LINUX_PORT/asd"
    curl -I "$LINUX_SERVER:$LINUX_PORT/asd"
}

function app-all
{
    local HELLO=$(app-hello)
    printf "\n%s\n%s\n" "Hello" "$HELLO"

    local HTML=$(app-html)
    printf "\n%s\n%s\n" "Html" "$HTML"

    local STATUS=$(app-status)
    printf "\n%s\n%s\n" "Status" "$STATUS"

    local SUM=$(app-sum 2 3 7)
    printf "\n%s\n%s\n" "Sum 2 3 7" "$SUM"

    local FILTER=$(app-filter 23 48 37 81 3 42)
    printf "\n%s\n%s\n" "Filter 23 48 37 81 3 42" "$FILTER"

    local NOT_FOUND=$(app-404)
    printf "\n%s\n%s\n" "404" "$NOT_FOUND"
}

while (( $# ))
do
    case "$1" in
        --help | -h)
            usage
            exit 0
        ;;
        --version | -v)
            version
            exit 0
        ;;
        hello \
        | html \
        | status \
        | sum \
        | filter \
        | 404 \
        | all)
            command=$1
            shift
            app-$command $*
            exit 0
        ;;
        *)
            badUsage "Option/command not recognized."
            exit 1
        ;;
    esac
done

badUsage
exit 1
