#!/bin/sh -e

PROKKADBPATH=${PROKKA_DBDIR:-~/.local/lib/prokka/db}
mkdir --parents ${PROKKADBPATH}

if which prokka >/dev/null ; then
   # PROKKAVERSION=`prokka -v | sed 's/^.*[[:space:]]\+//'`
   # Sometimes prokka reports wrong version
   PROKKAVERSION=`apt-cache policy prokka | grep "^ \*\*\*" | sed 's/^ \*\*\* \([.0-9]\+\)[+-].*/\1/'`
else
   echo "prokka not installed."
   exit 1
fi
set -x
wget -np -N https://github.com/tseemann/prokka/archive/v${PROKKAVERSION}.tar.gz
for data in hmm kingdom ; do
    tar -xaf v${PROKKAVERSION}.tar.gz --no-anchored "$data"
    rm -rf ${PROKKADBPATH}/$data
    mv prokka-${PROKKAVERSION}/db/$data ${PROKKADBPATH}
done
cp -rf /usr/share/prokka/db/cm ${PROKKADBPATH}/
cp -rf /usr/share/prokka/db/genus ${PROKKADBPATH}/

prokka --setupdb
