#!/bin/bash

set -e
set -u

if ! [ -f /.dockerenv ] && ! grep -q 'devices:/docker' /proc/1/cgroup ; then
  echo "Not running inside docker, exiting to avoid data damage." >&2
  exit 1
fi

SERVER="${1:-}"
SELECT="${2:-all}"
OUTPUT_DIRECTORY="${3:-/code/}"
FORMAT="${4:-pretty}"
NGCP_TYPE="sppro"

if [ -z "${SERVER}" ] ; then
  echo "Usage: $0 <testsystem> [<testset>] [<output_directory>] [junit]" >&2
  echo
  echo "Usage example: $0 192.168.88.162"
  echo "Usage example: $0 192.168.88.162 fast /results/ junit"
  echo "Possible test set: all, stable, fast, t/api-rest/api-root.t"
  echo "Default test set: all"
  exit 1
fi

if [ "${SELECT}" = "stable-pro" ] || [ "${SELECT}" = "stable-carrier" ] ; then
  SELECT="stable"
fi

if [ "${SELECT}" = "stable" ] ; then
  echo "Test selection (${NGCP_TYPE}): ${SELECT}"
  SELECT=$(echo ./t/api-rest/api-{all-links,balanceintervals,bannedips,bannedusers,billingfees,billingnetworks,billingprofiles,billingzones,calllists,calls,cert-auth,cfdestinationsets,contracts,conversations,customercontacts,customers,emailtemplates,faxes,journals,headerrulesets,lnp,ncoslevels,pbxdevicemodels,pbxdevices,peeringgroups,peeringrules,peeringinboundrules,peeringservers,preferences,profilepackages,resellers,rewriterules,rewriterulesets,root,soundsets,subscriberregistrations,subscribers,systemcontacts,threads,topuplogs,trustedsources,valid-patch,vouchers,method-override}.t)
elif [ "${SELECT}" = "stable-ce" ] ; then
  NGCP_TYPE="spce"
  echo "Test selection (${NGCP_TYPE}): ${SELECT}"
  SELECT=$(echo ./t/api-rest/api-{all-links,balanceintervals,bannedips,bannedusers,billingfees,billingnetworks,billingprofiles,billingzones,calllists,calls,cert-auth,cfdestinationsets,contracts,conversations,customercontacts,customers,emailtemplates,faxes,journals,lnp,ncoslevels,pbxdevicemodels,pbxdevices,peeringgroups,peeringrules,peeringinboundrules,peeringservers,preferences,profilepackages,resellers,rewriterules,rewriterulesets,root,soundsets,subscriberregistrations,subscribers,systemcontacts,threads,topuplogs,trustedsources,valid-patch,vouchers,method-override}.t)
elif [ "${SELECT}" = "fast" ] ; then
  echo "Test selection (${NGCP_TYPE}): ${SELECT}"
  SELECT=$(echo ./t/api-rest/api-{bannedips,bannedusers,billingnetworks,billingzones,calls,cert-auth,cfdestinationsets,headerrulesets,ncoslevels,peeringgroups,peeringrules,peeringinboundrules,peeringservers,resellers,rewriterules,root,soundsets,systemcontacts,valid-patch,vouchers,method-override}.t)
elif [ "${SELECT}" = "fast-ce" ] ; then
  NGCP_TYPE="spce"
  echo "Test selection (${NGCP_TYPE}): ${SELECT}"
  SELECT=$(echo ./t/api-rest/api-{bannedips,bannedusers,billingnetworks,billingzones,calls,cert-auth,cfdestinationsets,ncoslevels,peeringgroups,peeringrules,peeringinboundrules,peeringservers,resellers,rewriterules,root,soundsets,systemcontacts,valid-patch,vouchers,method-override}.t)
elif [ "${SELECT}" = "all" ] ; then
  echo "Test selection: all"
  SELECT=$(echo ./t/api-rest/*.t)
else
  echo "Test selection: ${SELECT}"
fi

echo "################################################################################"
echo "Finished main setup, now running tests ..."

export CATALYST_SERVER_SUB="https://${SERVER}:443"
export CATALYST_SERVER="https://${SERVER}:1443"
export NGCP_SESSION_ID="$(printf %03d $((RANDOM % 1000)))$(date +%s)"
export NGCP_TYPE
RC=0

if [ "${FORMAT}" = "junit" ] ; then
  # shellcheck disable=SC2086
  prove -ofmv --timer --formatter TAP::Formatter::JUnit -l -It/lib $SELECT | \
    tee -a "${OUTPUT_DIRECTORY}/api-rest.xml"
  RC=${PIPESTATUS[0]}
else
  # api-threads.t and api-balanceintervals.t are failing with the "-Pretty option" :(
  # shellcheck disable=SC2086
  prove -ofmv --color -l -It/lib $SELECT | tee -a "${OUTPUT_DIRECTORY}/api-rest.pretty"
  RC=${PIPESTATUS[0]}
fi

echo "Finished test execution, test execution returned with exit code ${RC}."
for file in "${OUTPUT_DIRECTORY}/api-rest.pretty" "${OUTPUT_DIRECTORY}/api-rest.xml" ; do
  if [ -f "$file" ] ; then
    echo "Test results available at ${file}"
  fi
done
echo "################################################################################"

exit "${RC}"
