#!/bin/bash

################################################################################
# Institute of Communication and Computer Systems (ICCS)
# i-sense Research Group
#
#        Test all REST resources in the local development application server.
#   Usage: ./test-localappserver.sh [USERNAME PASSWORD]
#   Requirements: Bash, GNU Wget
#
# Author: 
#    Miltiadis Koutsokeras, miltos.koutsokeras@iccs.gr
################################################################################


### Script setup
_SELF_="$(basename "$(readlink -e "${BASH_SOURCE[0]}")" && echo X)" && \
readonly _SELF_="${_SELF_%$'\nX'}"
_SELFDIR_="$(dirname "$(readlink -e "${BASH_SOURCE[0]}")" && echo X)" && \
readonly _SELFDIR_="${_SELFDIR_%$'\nX'}"


### Includes
# Nothing


### Constants
readonly USERNAME="${1:-Not Set}"  # user name
readonly PASSWORD="${2:-Not Set}"  # user password
readonly SERVER_URL="${3:-https://circ4life.iccs.gr}"
readonly REALM="CIRC4LIFE"
readonly TESTSDIR="${_SELFDIR_}/localappserver"


### Global variables
# Nothing


### Functions
# Print the script usage and exit
print_usage_and_exit( ) {
    echo "Usage: $(basename ${_SELF_}) [USERNAME PASSWORD SERVER_URL]"
    echo "If USERNAME and PASSWORD are provided, additional tests with
authorization from Access Control Manager are performed."
    echo "Optional arguments:"
    echo "USERNAME: Username to use for accessing protected resources."
    echo "PASSWORD: Password for specified USERNAME."
    echo "SERVER_URL: The Access Control Manager server URL (default: https://circ4life.iccs.gr)."

    exit 1
}


### Main program
if [ ! ${#} -eq 0 -a ! ${#} -eq 3 ]
then
    print_usage_and_exit
fi

# Perform all tests in TESTSDIR

# With authentication
if [ "x${USERNAME}x" != "xNot Setx" -a "x${PASSWORD}x" != "xNot Setx" ]
then
    echo "---------------------------------------------------------------------"
    echo "AUTHENTICATION WITH USER ${USERNAME}"
    for module in EcoCreditCalculator EndUserModule RecycleModule RetailerModule
    do
        echo
        echo "###### TEST: ${module} ##########################################"
        # TestModuleResources.sh is setup to work from _SELFDIR_ only
        pushd "${_SELFDIR_}"
        ./TestModuleResources.sh "${TESTSDIR}/${module}Resources.txt" "${USERNAME}" "${PASSWORD}" "${module}WS" "${REALM}" "${SERVER_URL}"
        popd
        echo "#################################################################"
    done
    echo "---------------------------------------------------------------------"
else
    # No authentication
    echo "---------------------------------------------------------------------"
    echo "NO AUTHENTICATION"
    for module in EcoCreditCalculator EndUserModule RecycleModule RetailerModule
    do
        echo
        echo "###### TEST: ${module} ##############################################"
        # TestModuleResources.sh is setup to work from _SELFDIR_ only
        pushd "${_SELFDIR_}"
        ./TestModuleResources.sh "${TESTSDIR}/${module}Resources.txt"
        popd
        echo "#####################################################################"
    done
    echo "---------------------------------------------------------------------"
fi

