#!/bin/sh
#
# We should use /usr/bin/env sh, but some systems are notoriously picky.
# In fact we could omit this line if some automations wouldn't rely on
# running this file via ./bootstrap.
#
# This file is in the public domain.
# SPDX-License-Identifier: 0BSD
#
# We can't set -eu because we encounter warnings which
# result in stops, whereas the warnings can for now be
# safely ignored.
# set -eu

cleanup()
{
    echo "Removing folder 'libltdl'..."
    rm -rf libltdl
}

errmsg=''

# Check if shell supports builtin 'type'.
if test -z "$errmsg"; then
    if ! (eval 'type type') >/dev/null 2>&1
    then
        errmsg='Shell does not support type builtin'
        exit 1
    fi
fi

# This is more portable than `which' but comes with
# the caveat of not(?) properly working on busybox's ash:
existence()
{
    type "$1" >/dev/null 2>&1
}

check_uncrustify()
{
    if existence uncrustify; then
        echo "Installing uncrustify hook and configuration"
        ln -fs $(pwd)/contrib/conf/uncrustify.cfg uncrustify.cfg 2> /dev/null
        ln -fs $(pwd)/contrib/conf/uncrustify_precommit .git/hooks/pre-commit 2> /dev/null
    else
        echo "Uncrustify not detected, hook not installed."
        echo "Please install uncrustify if you plan on doing development"
    fi
}

# yapf can be a suffixed binary, don't change the essential logic
# of this if you change it.
check_yapf()
{
    if existence yapf || \
       existence yapf3.0 || \
       existence yapf3.1 || \
       existence yapf3.2 || \
       existence yapf3.3 || \
       existence yapf3.4 || \
       existence yapf3.5 || \
       existence yapf3.6 || \
       existence yapf3.7 || \
       existence yapf3.8 || \
       existence yapf3.9 || \
       existence yapf4.0; then
        echo "Installing yapf symlink"
        ln -fs contrib/conf/.style.yapf .style.yapf 2> /dev/null
    else
        echo "yapf not detected, please install yapf if you plan on contributing python code"
    fi
}

check_libtool()
{
    echo "checking for libtoolize / libtool... "

    if existence libtool || \
       existence libtoolize || \
       existence glibtoolize || \
       existence slibtool; then
        autoreconf -if; ret=$?
        if [ $ret -ne 0 ]; then
          echo 'Error with `autoreconf -if`'
          return $ret
        fi
    elif ! existence meson; then
        echo "*** No libtoolize (libtool) or libtool or meson found, please install it ***" >&2;
        exit 1
    fi
}

submodules()
{
    # Try to update the submodule. Since bootstrap
    # is also invoked by distributors, we must
    # ignore any failing of this function as we
    # could have no outgoing network connection
    # in a restricted environment.
    if ! git --version >/dev/null; then
        echo "git not installed, skipping submodule update"
    else
        git submodule update --init --force --remote
    fi
}


install_hooks()
{
  ln -fs $(pwd)/contrib/conf/prepare-commit-msg .git/hooks/prepare-commit-msg 2> /dev/null
  ln -fs $(pwd)/contrib/conf/commit-msg .git/hooks/commit-msg 2> /dev/null
}

pogen()
{
    . "scripts/pogen.sh" || exit 1
}

update_gana()
{
    . "scripts/gana_update.sh" || exit 1
}

main()
{
    cleanup
    submodules
    check_uncrustify
    check_yapf
    echo "Please note that we have changed our default build system to meson"
    echo "If you want to build using autotools, pass 'autotools' to the bootstrap command"
    # update_gana
    if [ "$1" = "autotools" ]; then
      check_libtool; ret=$?
      if [ $ret -ne 0 ]; then
        echo 'Error with checking for libtool'
        return $ret
      fi
    fi
    #pogen
    if test -d .git
    then
        install_hooks
    fi
}

main "$@"
