#!/bin/bash
set -e

pkg="kalign"

if [ "$AUTOPKGTEST_TMP" = "" ] ; then
  AUTOPKGTEST_TMP=`mktemp -d /tmp/${pkg}-test.XXXXXX`
  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 *

# Passing input files via -i never works for some reason inside the testing chroot
#   LOG : kalign -i BB11001.tfa -f msf -o out.msf 
# ERROR : Input alignment format could not be detected. (rwalign.c line 314)
# ERROR : Function "detect_alignment_format(b, &type)" failed. (rwalign.c line 187)
# ERROR : Function "tmp_msa = read_input(param->infile[i],tmp_msa)" failed. (run_kalign.c line 383)
# ERROR : Function "run_kalign(param)" failed. (run_kalign.c line 353)

# However, input through stdin works
# Input via -i also works outside the chroot, just not inside it
  
echo TEST 1: Passing sequences via stdin
cat example.fa | kalign -f fasta > out.afa

# echo TEST 2: Passing sequences via flags
# kalign -f fasta -i example.fa -o out.afa

echo TEST 3: Combining multiple input files
# kalign -i seqsA.fa seqsB.fa seqsC.fa -f fasta > combined.afa 
cat seqsA.fa seqsB.fa seqsC.fa | kalign -f fasta > combined.afa 

echo TEST 4: Align sequences and output the alignment in MSF format
# kalign -i BB11001.tfa -f msf -o out.msf
cat BB11001.tfa | kalign -f msf -o out.msf
cat BB12006.tfa | kalign -f msf -o out.msf
cat BB30014.tfa | kalign -f msf -o out.msf

echo TEST 5: Align sequences and output the alignment in clustal format
# kalign -i BB11001.tfa -f clu -o out.clu
cat BB11001.tfa | kalign -f clu -o out.clu
cat BB12006.tfa | kalign -f clu -o out.clu
cat BB30014.tfa | kalign -f clu -o out.clu

echo TEST 6: Re-align sequences in an existing alignment
# kalign -i BB11001.msf -o out.afa
cat BB11001.msf | kalign -o out.afa
cat BB12006.msf | kalign -o out.afa
cat BB30014.msf | kalign -o out.afa

echo "PASS"
