#! /bin/sh
set -e

# Record certain build tool versions for use as a cache key.
autotools_ver=

# Log the identities and versions of the build tools.
for tool in \
    "${CC-cc}" \
    "${NM-nm}" \
    "${AUTOCONF-autoconf}" \
    "${AUTOMAKE-automake}" \
    "${LIBTOOLIZE-libtoolize}" \
    "${PKG_CONFIG-pkg-config}" \
    "${PERL-perl}" \
    "${CPANM-cpanm}" \
    "${PERLCRITIC-perlcritic}" \
    "${PERLTIDY-perltidy}" \
    "${PYTHON-python3}" \
    "${GCOV-gcov}" \
    "${LCOV-lcov}" \
    "${LLVM_COV-llvm-cov}" \
    "${VALGRIND-valgrind}"
do
    # $tool might include mandatory command-line arguments.
    # Interpret it the same way Make would.
    set fnord $tool; shift # word-split $tool and load it into "$@"

    echo ::group::"$1"
    if command -V "$1"; then
        echo + "$@" --version
        "$@" --version
    fi
    echo ::endgroup::

    case "$1" in
        (*autoconf*)
            autotools_ver="${autotools_ver}ac$("$@" --version 2>&1 |
              sed -Ene '1{s/^[^\(]+\([^\)]+\) //;p}'
            )"
            ;;

        (*automake*)
            autotools_ver="${autotools_ver}am$("$@" --version |
              sed -Ene '1{s/^[^\(]+\([^\)]+\) //;p}'
            )"
            ;;

        (*libtoolize*)
            autotools_ver="${autotools_ver}lt$("$@" --version |
              sed -Ene '1{s/^[^\(]+\([^\)]+\) //;p}'
            )"
            ;;

        (*pkg-config*)
            autotools_ver="${autotools_ver}pk$("$@" --version |
              sed -Ene '1{s/^[^\(]+\([^\)]+\) //;p}'
            )"
            ;;

        (*python*)
            echo ::group::passlib
            "$@" -c '
try:
  import passlib
  import os.path
  print("passlib is " + os.path.dirname(passlib.__file__))
  print("passlib: version " + passlib.__version__)
except ModuleNotFoundError:
  print("passlib is not installed")
'
            echo ::endgroup::
            ;;
    esac
done

set fnord; shift  # clear $@
echo "::set-output name=autotools-ver::$autotools_ver"
exit 0
