#!/bin/sh
#
# bootstrap script to get the tools needed to build the specs within a UNIX shell

export LC_ALL=
FOUND=
NEEDED=

MMARK_VERSION=2.2.46
XML2RFC_VERSION=3.27.0
XSLTPROC_VERSION=1.1.0
XMLLINT_VERSION=20903

BOOTSTRAP_MAKE=bootstrap.mak
RUNTIMES_MAKE=runtimes.mak

check_version() {
    gotver=$2
    gotmajor=`echo $gotver|cut -d. -f1`
    gotminor=`echo $gotver|cut -d. -f2|cut -d+ -f1`
    gotmicro=`echo $gotver|cut -d. -f3|cut -d+ -f1`
    [ -z "$gotmicro" ] && gotmicro=0
    needmajor=`echo $3|cut -d. -f1`
    needminor=`echo $3|cut -d. -f2`
    needmicro=`echo $3|cut -d. -f3`
    [ -z "$needmicro" ] && needmicro=0
    if [ "$needmajor" -ne "$gotmajor" \
         -o "$needmajor" -eq "$gotmajor" -a "$needminor" -gt "$gotminor" \
         -o "$needmajor" -eq "$gotmajor" -a "$needminor" -eq "$gotminor" -a "$needmicro" -gt "$gotmicro" ]
    then
        echo "$1 too old (got $gotver, needed $3)"
        NEEDED="$NEEDED $1"
    else
        FOUND="$FOUND $(command -v $1)"
        echo "found $1 version $2 (needed $3)"
    fi
}

check() {
    if ! $1 --version >/dev/null 2>&1 && ! $1 -version >/dev/null 2>&1
    then
        echo "$1 not found"
        NEEDED="$NEEDED $1"
    else
        # found, need to check version ?
        if [ -z "$2" ]; then
            FOUND="$FOUND $(command -v $1)"
            echo "found $1"
        else
            gotver=`$1 --version | head -1 | sed s/'.* '//`
            check_version $1 $gotver $2
        fi
    fi
}

# check make
check mmark $MMARK_VERSION
check xml2rfc $XML2RFC_VERSION
# apt install xsltproc
check xsltproc $XSLTPROC_VERSION
# apt install libxml2-utils
check xmllint $XMLLINT_VERSION

cat > $BOOTSTRAP_MAKE << EOF
# Generated from boostrap

PREFIX=\$(abspath ./build)
EOF

echo > $RUNTIMES_MAKE << EOF
# Generated from boostrap

# calls the local or installed tool
EOF

for t in $FOUND; do
    FOUND_NAME=$(echo "$(basename $t)")
    echo ".$FOUND_NAME:" >> $BOOTSTRAP_MAKE
    VAR_NAME=$(echo "$(basename $t)" | awk '{ tool = sprintf("%s_CALL", toupper($0)); print tool }')
    echo "$VAR_NAME := $t" >> $RUNTIMES_MAKE
done

for t in $NEEDED; do
    echo .$t: .build$t >> $BOOTSTRAP_MAKE
    PACKAGES="$PACKAGES $t"
    TARGETS="$TARGETS .build$t"
    if [ $t = "xml2rfc" ]; then
        # installed in the Python local user dir
        PYTHON_USER_PATH=$(python3 -m site --user-base)
        echo PYTHON_USER_PATH=$PYTHON_USER_PATH >> $RUNTIMES_MAKE
        echo "$t" | awk '{ tool = sprintf("%s_CALL := $(PYTHON_USER_PATH)/bin/%s", toupper($0), $0); print tool }' >> $RUNTIMES_MAKE
    elif [ $t = "xsltproc" -o  $t = "xmllint" ]; then
        echo "$t" | awk '{ tool = sprintf("%s_CALL := %s", toupper($0), $0); print tool }' >> $RUNTIMES_MAKE
    else
        echo "$t" | awk '{ tool = sprintf("%s_CALL := ./%s", toupper($0), $0); print tool }' >> $RUNTIMES_MAKE
    fi
done

if [ ! -f ../ebml-specification/EBMLSchema.xsd ]; then
    echo out of tree DETECTED, getting a temporary EBMLSchema.xsd
    curl -f -L -o EBMLSchema.xsd https://raw.githubusercontent.com/cellar-wg/ebml-specification/master/EBMLSchema.xsd
    if [ ! -f EBMLSchema.xsd ]; then
        echo Missing EBML Schema
        exit 1
    fi
    echo EBML_SCHEMA_XSD=EBMLSchema.xsd >> $RUNTIMES_MAKE
fi

[ -n "$PACKAGES" ] && echo "Out of date packages: $PACKAGES"

case `uname` in
	Linux)
		MMARK_OS=linux
	;;
	Darwin)
		MMARK_OS=darwin
	;;
	MINGW32*|MINGW64*|*MSYS*)
		MMARK_OS=windows
	;;
	*)
		echo Unsupported build OS `uname`
		exit 1
	;;
esac
case `uname -m` in
	x86_64)
		MMARK_MACHINE=amd64
	;;
	arm64)
		MMARK_MACHINE=arm64
	;;
	arm)
		MMARK_MACHINE=arm
	;;
	*)
		echo Unsupported build CPU `uname -m`
		exit 1
	;;
esac

cat >> $BOOTSTRAP_MAKE << EOF
all: $TARGETS
	@echo "You are ready to build Matroska specifications"

MMARK_VERSION=$MMARK_VERSION
MMARK_OS=$MMARK_OS
MMARK_MACHINE=$MMARK_MACHINE
XML2RFC_VERSION=$XML2RFC_VERSION
XSLTPROC_VERSION=$XSLTPROC_VERSION
XMLLINT_VERSION=$XMLLINT_VERSION

include tools.mak
EOF

echo Getting necessary tools
make -f $BOOTSTRAP_MAKE
