#!/bin/sh
# 
# Testing the installation: the version, paths and number of extensions should be correct
# This test entirely depends on the CamitK version (version string, number of extensions...)
# (see the expectedConfigOutput)
# 
# For a CamiTK major or minor version update:
# - update getExpectedValue for "sid"
# - update checkInstalledVersion for "sid"
# - update getExpectedExtensionCount for "sid"
#
# For a CamiTK hotfix update:
# - update checkInstalledVersion for "sid"
#
# echo $? get the last returned value of the script
# a return value of 0 indicates success (by convention)
# The value return by this script corresponds to the config test that failed
# (see log for config test id)
# 
set -e

# cleanup on exit
cleanup() {
    # backup the current exit status
    currentExitValue=$?
    # cleanup current dir
    rm -rf $workingDir
    # use the backup value (otherwise the result of the "rm -rf" command above will
    # be used, and that's probably always 0 !)
    exit $currentExitValue
}

# if a problem occurs, call the clean method
trap "cleanup" 0 INT QUIT ABRT PIPE TERM EXIT

# --------------------------------------------------------------------------
#
# Get ready
#
# --------------------------------------------------------------------------
echo "========== checking camitk configuration =========="
exitStatus=0 # nothing bad. By convention exit 0 indicates success

echo "===== Creating temporary directory ====="
workingDir=$(mktemp --tmpdir -d camitk-test-tmp.XXXXXXXXXX)
cd $workingDir

echo "===== Get CamiTK configuration ====="
# debian-ci, gives a warning, this warning polutes the output,
# which in turns gives the wrong expected version
# This should get only the proper output
xvfb-run --auto-servernum --server-num=1 --error-file ./xvfb-error camitk-config --config > ./config-output 2> ./config-error
camitkConfig=$(cat config-output)

echo "===== xvfb-error ====="
cat ./xvfb-error

echo "===== config-error ====="
cat ./config-error

echo "===== config-output ====="
cat ./config-output

echo "===== camitkConfig ====="
echo $camitkConfig

# ---------------------- expected value ----------------------
getExpectedValue() {
  case "$1" in
    "CamiTK Short Version")
      case "$1" in
        "jessie")
          echo "camitk-3.3" 
          ;;    
        "sid" | *)
          echo "camitk-4.0"
          ;;
      esac
      ;;
    "CamiTK Global Installation Directory")
        # always the same value
        echo "/usr"
        ;;
    "Number of Component Extensions")
      case "$1" in
        "jessie")
          echo "12" 
          ;;    
        "sid" | *)
          echo "11"
          ;;
      esac
      ;;       
    "Number of Action Extensions")
      case "$1" in
        "jessie")
          echo "71" 
          ;;    
        "sid" | *)
          echo "19"
          ;;
      esac
      ;;
    "Number of File Extensions Supported")
      case "$1" in
        "jessie" | "sid" | *)
          echo "35"
          ;;
      esac
      ;;
    "Number of Actions")
      case "$1" in
        "jessie")
          echo "81"
          ;;
         "sid" | *)
          echo "91"
          ;;
      esac
      ;;
  esac
}

# ---------------------- installed version ----------------------
getInstalledVersion() {
  echo $(echo $camitkConfig | cut -f2 -d" ")
}

checkInstalledVersion() {
  case "$1" in
    "4.0.4")
      echo "sid"
      ;;
    "3.3.2")
      echo "jessie"
      ;;
    *)
      echo "unknown version: [$1]"
      ;;
  esac
}

# ---------------------- extension count ----------------------
getExtensionCount() {
  echo $(echo "$camitkConfig" | grep "\[G\]" | wc -l)
}

getExpectedExtensionCount() {
  case "$1" in
    "jessie")
      echo "83" 
      ;;    
    "sid" | *) 
      echo "30" 
      ;;
  esac
}

# ---------------------- get config ----------------------
# get a specific value from config, text to parse from
# camitk-config --config is the first parameter of the function
# get the value after the string "... " and before the first space
getConfigValue() {
  echo $(echo "$camitkConfig" | grep "$1" | sed 's/^.*\.\.\. //g' | cut -f1 -d" ")
}

# ---------------------- check value ----------------------
# use camitk-config to check a value and compare to 
# expected value
checkValue() {
  checkedValue="$1"
  value=$(getConfigValue "$checkedValue")
  echo "===== $checkValueId- $checkedValue: $value ====="
  expected=$(getExpectedValue "$checkedValue")
  if [ "$value" != "$expected" ]; then
    echo "Error: unexpected $checkedValue ($value != $expected)"
    exitStatus=$checkValueId
  else
    echo "OK"
  fi
  # increase id
  checkValueId=$((checkValueId+1))
}

# --------------------------------------------------------------------------
#
# All tests are here
#
# --------------------------------------------------------------------------

installedVersion=$(getInstalledVersion)
echo "===== 1- Detected installed CamiTK version is $installedVersion ====="
version=$(checkInstalledVersion $installedVersion)
if [ "$version" = "unknown" ]; then
  echo -n "Error: unexpected version "
  exitStatus=1
else
  echo -n "OK "
fi
echo "($version)"

value=$(getExtensionCount)
echo "===== 2- Number of extensions: $value ====="
expected=$(getExpectedExtensionCount $version)
if [ "$value" -ne "$expected" ]; then
  echo "Error: unexpected number of globally installed extensions ($value != $expected)"
  exitStatus=1
else
  echo "OK"
fi

# init the id (next test is the third test)
checkValueId=3
checkValue "CamiTK Short Version"
checkValue "CamiTK Global Installation Directory"
checkValue "Number of Component Extensions"
checkValue "Number of Action Extensions"
checkValue "Number of File Extensions Supported"
checkValue "Number of Actions"

exit $exitStatus
