#!/bin/bash
set -e

pkg=ncbi-entrez-direct

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 *

echo -e "\e[93m\e[1mTest 1: amino-acid-composition\e[0m"
aac1=$(echo qwertyuiopasdfghjklzxcvbnm | amino-acid-composition | cut -f2 | tr -d '\n')
test $aac1 = 11111111111111111111111111
aac2=$(echo acdefglki | amino-acid-composition | cut -f2 | tr -d '\n')
test $aac2 = 10010101010111010000000000
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 2: esample\e[0m"
esample -article 
esample -book 
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 3: exclude-uid-lists\e[0m"
exclude-uid-lists list1 list2 
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 4: filter-stop-words\e[0m"
filter-stop-words < stopwords
echo -e "\e[92m\e[1mPassed\e[0m"
echo 
echo -e "\e[93m\e[1mTest 5: gbf2xml\e[0m"
gbf2xml < sample.gbf
echo -e "\e[92m\e[1mPassed\e[0m"
echo 
echo -e "\e[93m\e[1mTest 6: intersect-uid-lists\e[0m"
intersect-uid-lists list1 list2
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 7: join-into-groups-of\e[0m"
join-into-groups-of 3 < list1
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 8: sort-uniq-count\e[0m"
sort-uniq-count < list3
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 9: sort-uniq-count-rank\e[0m"
sort-uniq-count-rank < list3
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 10: transmute json to xml\e[0m"
transmute -j2x < sample.json
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 11: transmute xml to json\e[0m"
transmute -x2j < sample.xml
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 12: word-at-a-time\e[0m"
head -n10 stopwords | tr '\n' ' ' | word-at-a-time
echo -e "\e[92m\e[1mPassed\e[0m"
echo 
echo -e "\e[93m\e[1mTest 13: xml2tbl\e[0m"
cat sample.xml | xml2tbl
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 14: xtract\e[0m"
xtract -input sample_query.xml -pattern Gene-commentary  -match Gene-commentary_type:1 -element Gene-commentary_accession
echo -e "\e[92m\e[1mPassed\e[0m"
echo

