#!/bin/bash

error() {
    local ERROR="$1"; shift

    if [ -n "${ERROR}" ]; then echo "Error: ${ERROR}"; fi
    exit 1
}

usage() {
    local ERROR="$1"; shift

    set +x

    echo "Installs sina version from fat tarball."
    echo ""
    echo "A fat tarball contains a sina binary plus several required shared libs."
    echo "- it is created on all systems where sina succeeds to build (as part of the arb build)."
    echo "- it is provided for download for such systems on http://www.arb-home.de/"
    echo "- it allows to use sina on (some) of those systems where sina fails to build with arb."
    echo ""
    echo "Usage: arb_sina_install_from_fat_tarball.sh <fat-tarball> <arbhome> [--no-check]"
    echo "    <fat-tarball>    path of the fat sina tarball"
    echo "    <arbhome>        install inside this arb installation"
    echo ""
    echo "If --no-check is specified => skip call to sina binary."
    echo "To uninstall specify 'UNINSTALL' for <fat-tarball>."
    echo ""

    error "${ERROR}"
}

main() {
    local TARBALL="$1"; shift
    local ARBHOME="$1"; shift
    local THIRDARG="$1"; shift

    if [ "${TARBALL}" = "-h" -o "${TARBALL}" = "--help" ]; then usage; fi
    if [ -z "${ARBHOME}" ]; then usage "Missing arguments"; fi

    local CHECK=1
    if [ -n "${THIRDARG}" ]; then
        if [ "${THIRDARG}" = "--no-check" ]; then
            CHECK=0
        else
            usage "Unknown third argument '${THIRDARG}'"
        fi
    fi

    local ARB_PATH="$(dirname $0)/arb_path.sh"

    ARBHOME=$(${ARB_PATH} -r $ARBHOME)

    # remove all trailing slashes from directory and append one:
    shopt -s extglob; ARBHOME=${ARBHOME%%+(/)}/

    local UNINSTALL=0
    if [ "${TARBALL}" = "UNINSTALL" ]; then UNINSTALL=1; fi

    if [ $UNINSTALL = 0 ]; then
        TARBALL=$(${ARB_PATH} -r $TARBALL)
        test -e "${TARBALL}" || usage "No such file: ${TARBALL}"
    fi
    test -d "${ARBHOME}" || usage "No such directory: ${ARBHOME}"

    local BINDIR=${ARBHOME}bin/
    test -d "${BINDIR}" || error "Expected 'bin' directory inside <arbhome>, i.e. ${BINDIR} (wrong <arbhome> specified?)"

    local ARB_BINARY=${BINDIR}arb_ntree
    test -x "${ARB_BINARY}" || error "Expected main arb binary ${ARB_BINARY} (wrong <arbhome> specified?)"

    local TARGETDIR=${BINDIR}fatsina/
    if [ $UNINSTALL = 1 ]; then
        if [ ! -d ${TARGETDIR} ]; then
            error "sina installation directory '${TARGETDIR}' not found (nothing to uninstall)."
        fi

        echo "* uninstalling fatsina from arb installation ${ARBHOME}"
        rm -rf ${TARGETDIR} || error "Failed to remove '${TARGETDIR}'."
    else
        if [ -d ${TARGETDIR} ]; then
            error "sina installation directory '${TARGETDIR}' already exists."
        fi

        echo "* installing from fat sina tarball ${TARBALL}"
        echo "* installing into arb installation ${ARBHOME}"

        mkdir -p ${TARGETDIR} || error "Failed to make directory '${TARGETDIR}'."
        cd ${TARGETDIR} || error "Failed to cd to '${TARGETDIR}'."
        tar -zxf ${TARBALL} || error "Failed to unpack '${TARBALL}'."

        if [ ${CHECK} = 1 ]; then
            local SINACALL="LD_LIBRARY_PATH=${TARGETDIR} ${TARGETDIR}sina --has-cli-vers ARB7.1"
            # local SINACALL_VIABASH="bash -c '( ${SINACALL} )'"

            echo "* checking dynamic linkage of installed sina binary:"
            echo "  using command: bash -c '( ${SINACALL} )'"
            echo "------------------------------------------------------------"
            bash -c "( ${SINACALL} )"
            echo "------------------------------------------------------------"
            echo "Expected output between dashed lines above would be similar to"
            echo "** SINA (SILVA Incremental Aligner) 1.2.3 present"
            echo ""
            echo "If it shows dynamic linking errors, you can try to fix that by"
            echo "deleting single libraries mentioned in error messages."
            echo ""
            echo "If that does not help, this sina-fat-tarball is incompatible with"
            echo "your operation system."
            echo "You could try fat-tarballs built on different OS versions."
            echo ""
        else
            echo "* assuming remote install (skipping compatibility test)"
        fi
    fi
}

# set -x

main $*
