#!/bin/bash
set -beEu -o pipefail

# Do required submodule setup to support building the browser
# If need, do the submodule init, also check from having remains of
# old htslib copy to deal with switch from old version.

if [ -e htslib ] ; then
    echo "*********************************************************************" >&2
    echo "Note: an outdated kent/src/htslib/ exists in the working tree." >&2
    echo "      It is no longer used and can be removed."   >&2
    echo "*********************************************************************" >&2
fi

if [ !  -e submodules/htslib/.git -o ! -e submodules/htslib/htscodecs/.git ] ; then
    if git rev-parse --is-inside-work-tree >/dev/null 2>&1 ; then
        echo "Note: initializing git submodules" >&2
        (set -x; git -c protocol.file.allow=always submodule update --init --recursive)
    elif [ ! -e submodules/htslib/hts.c -o ! -e submodules/htslib/htscodecs/htscodecs/htscodecs.c ] ; then
        # not a git working tree (e.g. a source release); submodule sources must
        # already be present since they can not be checked out without git.
        echo "Error: submodule sources under submodules/htslib are missing and this" >&2
        echo "       is not a git working tree, so they can not be initialized." >&2
        exit 1
    fi
fi

