#!/bin/bash
set -e

pkg=cluster3

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  # Double quote below to expand the temporary directory variable now versus
  # later is on purpose.
  # shellcheck disable=SC2064
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cp -a /usr/share/doc/${pkg}/examples/* "${AUTOPKGTEST_TMP}"

cd "${AUTOPKGTEST_TMP}"

gunzip -r *

run_test() {
  # $1: test number
  # $2: infile
  # $3: outfile
  # $4: reference
  echo -e "Test $1"
  cluster3 -f $2 > /dev/null 2>&1
  if [ "$?" -ne "0" ]; then
      echo -e "\e[31m\e[1mTest $1 could not be run.\e[0m"
      exit 1
  fi
  diff $3 $4 --suppress-common-lines
  if [ "$?" -ne "0" ]; then
    echo -e "\e[31m\e[1m$3 did not match the reference.\e[0m"
    exit 1
  fi
  echo -e "\e[92m\e[1mPassed\e[0m"
  echo
}

run_test 1 cyano.txt cyano.nrm ref-cyano.nrm
run_test 2 demo.txt demo.nrm ref-demo.nrm

