#!/usr/bin/env bash
# Time-stamp: <2018-08-13 16:47:15 kmodi>

# Usage: ./mybuild.sh # Installs using origin/master
#        ./mybuild.sh -r v2.16.2

set -euo pipefail # http://redsymbol.net/articles/unofficial-bash-strict-mode
IFS=$'\n\t'

if ! hash asciidoc 2>/dev/null
then
    echo "'asciidoc' was not found in PATH. It is needed for installing the git man pages."
    exit 1
fi

docbook2texi_bin="docbook2x-texi"
if ! hash "${docbook2texi_bin}" 2>/dev/null
then
    echo "'${docbook2texi_bin}' was not found in PATH. Checking if 'docbook2texi' is found instead .."
    docbook2texi_bin="docbook2texi"
    if ! hash "${docbook2texi_bin}" 2>/dev/null
    then
        echo "'${docbook2texi_bin}' was not found in PATH. It is needed for installing the git Info manual."
        exit 1
    fi
fi

git_rev="origin/master"

while [ $# -gt 0 ]
do
    case "$1" in
        "-r"|"--rev" ) shift
                       git_rev="$1";;
        * ) echo >&2 "Error: Invalid option: $*"
    esac
    shift # expose next argument
done

# If ${git_rev} is origin/master, ${git_rev_basename} = master
# If ${git_rev} is v2.16.2, ${git_rev_basename} = v2.16.2
git_rev_basename=$(basename "${git_rev}")

git fetch --all
git checkout "${git_rev_basename}"
git fetch --all
git reset --hard "${git_rev}"

echo ""

wait_time=5 # seconds
temp_cnt=${wait_time}
# https://www.cyberciti.biz/faq/csh-shell-scripting-loop-example/
while [[ ${temp_cnt} -gt 0 ]];
do
    printf "\\rWaiting for %2d second(s) .. Press Ctrl+C to cancel this installation." ${temp_cnt}
    sleep 1
    ((temp_cnt--))
done
echo ""

git_install_dir="${STOW_PKGS_ROOT}/git/${git_rev_basename}"

if [[ ! -f ./configure ]]
then
    autoreconf -vif
fi
libtoolize

CPPFLAGS="-fgnu89-inline" ./configure --prefix="${git_install_dir}"
# Mon May 01 12:29:48 EDT 2017 - kmodi
# Needed to install asciidoc ( https://sourceforge.net/projects/asciidoc ) in
# order for "make doc" to work, and docbook2X for "make info".
# Mon May 01 14:32:54 EDT 2017 - kmodi
# Note that the docbook2X needs to be built with this patch applied:
#   https://sourceforge.net/p/docbook2x/bugs/20/
# The latest release v0.8.8 as of today does not have this fix.
# make all man info DOCBOOK2X_TEXI="${docbook2texi_bin}"
# Fri Mar 16 12:49:42 EDT 2018 - kmodi
# make info is failing today with:
#   make[2]: Leaving directory '/tmp/kmodi/downloads_overflow/git'
#       DB2TEXI gitman.texi
#       MAKEINFO gitman.info
#   novalidate not a possible customization in Texinfo::Parser::parser
#   gitman.texi:1591: @ref reference to nonexistent node `ATTRIBUTES'
#   gitman.texi:3145: @pxref reference to nonexistent node `CO1-1'
#   gitman.texi:3161: @pxref reference to nonexistent node `CO2-1'
#   gitman.texi:3166: @pxref reference to nonexistent node `CO2-2'
#   gitman.texi:5041: @pxref reference to nonexistent node `CO1-1'
#   ..
#   gitman.texi:58299: @pxref reference to nonexistent node `[def_directory]'
#   gitman.texi:58301: @pxref reference to nonexistent node `[def_commit-ish]'
#   gitman.texi:58303: @pxref reference to nonexistent node `[def_tag_object]'
#   gitman.texi:58309: @pxref reference to nonexistent node `[def_index]'
#   gitman.texi:58310: @pxref reference to nonexistent node `[def_index_entry]'
#   gitman.texi:58313: @pxref reference to nonexistent node `[def_object]'
#   gitman.texi:58313: @pxref reference to nonexistent node `[def_reachable]'
#   gitman.texi:58314: @pxref reference to nonexistent node `[def_branch]'
#   gitman.texi:58314: @pxref reference to nonexistent node `[def_tag]'
#   gitman.texi:58317: @pxref reference to nonexistent node `[def_branch]'
#   gitman.texi:58324: @pxref reference to nonexistent node `[def_HEAD]'
#   gitman.texi:58607: @pxref reference to nonexistent node `update'
#   gitman.texi:58662: @pxref reference to nonexistent node `post-receive'
#   gitman.texi:58689: @pxref reference to nonexistent node `pre-receive'
#   gitman.texi:58695: @pxref reference to nonexistent node `post-update'
#   gitman.texi:58735: @pxref reference to nonexistent node `post-receive'
#   make[1]: *** [Makefile:404: gitman.info] Error 1
#   make[1]: Leaving directory '/tmp/kmodi/downloads_overflow/git/Documentation'
#   make: *** [Makefile:2244: info] Error 2

# So, for now, installing without building Info manual.
# Fri Mar 16 13:01:48 EDT 2018 - kmodi
# But even that did not help.. I still get the same error.
# So for now, I manually did the make install install-man, and the subtree building steps.
make all man

./configure --prefix="${git_install_dir}"
make install install-man
# make install-info

# Build "git subtree"
cd ./contrib/subtree
make install install-man prefix="${git_install_dir}"

echo ""
echo "Now update the '~/scripts/bash/stow_it.sh' script and rerun that script if needed."
echo "  That would be needed if you are installing 'git' for the first time, or changing the version."