#!/bin/bash
# ======================================================================
#
# HELPER :: GO TO A BUILD DIRECTORY
#
# Jump to a build directory to debug build errors.
#
# ----------------------------------------------------------------------
# 2024-01-19  v1.0  <axel.hahn@iml.unibe.ch>  first lines
# 2024-07-25  v1.1  <axel.hahn@iml.unibe.ch>  deny root access; shell fixes
# ======================================================================

cd "$( dirname "$0" )/.." || exit 1
CI_APPROOT=$( pwd )

# ----------------------------------------------------------------------
# header
# ----------------------------------------------------------------------

cat <<HEADEREND
+----------------------------------------------------------------------
| CI SERVER * helper script
| Jump to a build directory to debug build errors
:

HEADEREND

# ----------------------------------------------------------------------
# deny start as root
# ----------------------------------------------------------------------
if [ "$( id -u )" -eq 0 ]; then
  echo "ERROR: do not start this script as root."
  exit 1
fi

# ----------------------------------------------------------------------
# detect workdir
# ----------------------------------------------------------------------

datadefault=$( grep "'workDir'.*=>" config/config_defaults.php | cut -f 2 -d '>' | cut -f 2 -d "'" )
datacustom=$(  grep "'workDir'.*=>" config/config_custom.php   | cut -f 2 -d '>' | cut -f 2 -d "'" )
CI_WORKDIR=${datacustom:-$datadefault}
if [ -z "$CI_WORKDIR" ]; then
    echo "ERROR: Unable to parse working dir from config/config_*.php."
    echo "       Sorry ... it is Bash here :-/"
    exit 1
fi
builddir="$CI_WORKDIR/build/"

# ----------------------------------------------------------------------
# detect builds on error
# ----------------------------------------------------------------------

ls  "$builddir" >/dev/null || exit 1
echo "Searching for builds im $builddir..."
founddirs=$( find "$builddir" -maxdepth 2 -type d | grep '2[0-1][0-9][0-9][0-1][0-9][0-3][0-9]' | sort )

if [ -z "$founddirs" ]; then
    echo "Good news: No build dirs (builds on error) were found. Aborting."
    echo
    exit 0
fi

# ----------------------------------------------------------------------
# enter my directory
# ----------------------------------------------------------------------

cat <<ENDINFO
Found error builds: $( echo "$founddirs" | wc -l )

The latest error is the last entry per project.
Select a directory to jump in or press just RETURN to abort:
$( echo "$founddirs" | sed 's,^,    ,g')

ENDINFO

echo -n "I want to jump to > "
read -r DIR_APPROOT

test -z "$DIR_APPROOT" && exit 1
ls "$DIR_APPROOT" >/dev/null || exit 2

# ----------------------------------------------------------------------
# prepare environment an GO!
# ----------------------------------------------------------------------

export DIR_APPROOT
export GIT_SSH="$CI_APPROOT/shellscripts/gitsshwrapper.sh"
export DIR_SSH_KEYS="$CI_WORKDIR/data/sshkeys"
export NVMINIT="$CI_APPROOT/shellscripts/nvm_init.sh";
export RVMSCRIPT="/usr/local/rvm/scripts/rvm";

cd "$DIR_APPROOT" || exit 1
cat <<EOF

* Switching now into build dir
  $( pwd )
* The environment for the CI server is set
  You can go to the ./hooks/ directory to execute the command of your onbuild actions.

EOF
echo
bash --rcfile <(cat ~/.bashrc 2>/dev/null; echo 'PS1="CI|$PS1"')

# ----------------------------------------------------------------------
