#!/bin/sh
set -e

pkg=sift

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  # shellcheck disable=SC2064
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cp -a /usr/share/doc/${pkg}/examples/lacI.alignedfasta "${AUTOPKGTEST_TMP}"
cp -a /usr/share/doc/${pkg}/examples/lacI.subst "${AUTOPKGTEST_TMP}"

cd "${AUTOPKGTEST_TMP}"

if ! /usr/bin/info_on_seqs lacI.alignedfasta lacI.subst lacI.SIFTprediction \
        > info-on-seqs.log 2>&1; then
    echo "info_on_seqs failed:" >&2
    cat info-on-seqs.log >&2
    exit 1
fi

awk -F '\t' '
BEGIN {
    substitution[1] = "K2S";  prediction[1] = "TOLERATED"
    substitution[2] = "P3M";  prediction[2] = "TOLERATED"
    substitution[3] = "V15K"; prediction[3] = "DELETERIOUS"
}
{
    if (!(NR in substitution)) {
        printf "unexpected output line %d: %s\n", NR, $0 > "/dev/stderr"
        exit 1
    }
    if (NF != 6) {
        printf "line %d: expected 6 fields, got %d\n", NR, NF > "/dev/stderr"
        exit 1
    }
    if ($1 != substitution[NR] || $2 != prediction[NR]) {
        printf "line %d: expected %s %s, got %s %s\n", NR,
               substitution[NR], prediction[NR], $1, $2 > "/dev/stderr"
        exit 1
    }
    if ($3 !~ /^[0-9]+\.[0-9]+$/ || $3 < 0 || $3 > 1) {
        printf "line %d: invalid SIFT score: %s\n", NR, $3 > "/dev/stderr"
        exit 1
    }
}
END {
    if (NR != 3) {
        printf "expected 3 predictions, got %d\n", NR > "/dev/stderr"
        exit 1
    }
}
' lacI.SIFTprediction
