#!/bin/bash
#----------------------------------------------------------------------------
PACKAGE=$(ls -tr ../../build/orthologer_*.tgz 2>/dev/null | tail -n1)
[[ -f $PACKAGE ]] || {
    echo "error: no package file found in ../build - need to run makepkg.sh" 1>&2
    exit 255
}

# create install and work environment
mkdir -p RunLogs   || exit 255
mkdir -p local/bin || exit 255
mkdir -p package   || exit 255
mkdir -p work      || exit 255
touch work/orthologer_conf.sh

if [[ ! -f package/install_pkg.sh ]]; then
    echo "untarring $PACKAGE into $(pwd)/package"
    tar -xzf $PACKAGE -C ./package  --strip-components=1 || exit 255
else
    echo "package is already untarred at $(pwd)/package"
fi

export PREFIX=$(pwd)/local
if [[ -f $PREFIX/bin/ODB-mapper ]]; then
    echo ">>> SKIP: package is already installed"
else
    echo ">>> Install package in prefix $PREFIX"
    [[ -f package/install_pkg.sh ]] || { echo "error: missing install_pkg.sh in $(pwd)/package"; exit 255; }
    cd package
    ./install_pkg.sh || exit 255
fi

exit 0
