#!/bin/bash

## Copyright (C) 2020 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

if [ -o xtrace ]; then
   export GENMKFILE_DEBUG=1
fi

MYDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

pushd "$MYDIR" >/dev/null
cd ..
cd ..

if [ "$GENMKFILE_PATH" = "" ] ; then
   if [ -d "./packages/kicksecure/genmkfile/usr/share/genmkfile" ]; then
      GENMKFILE_PATH="./packages/kicksecure/genmkfile/usr/share/genmkfile"
   elif [ -d "./usr/share/genmkfile" ]; then
      GENMKFILE_PATH="./usr/share/genmkfile"
   elif [ -d "/usr/share/genmkfile" ]; then
      GENMKFILE_PATH="/usr/share/genmkfile"
   else
      echo "$0: ERROR: Auto detection of GENMKFILE_PATH failed!"
   fi
fi

GENMKFILE_PATH="$(realpath "$GENMKFILE_PATH")"
export GENMKFILE_PATH

if [ ! -r "${GENMKFILE_PATH}/makefile-full" ]; then
   echo "$0: ERROR: Folder '$GENMKFILE_PATH' exists but file '${GENMKFILE_PATH}/makefile-full' does not readable!"
   echo "$0: ERROR: Running 'ls -la $GENMKFILE_PATH' for debugging..."
   ls -la "$GENMKFILE_PATH"
   echo "$0: ERROR: Permission denied error? Try running:"
   echo ""
   echo "sudo chmod --recursive o+r '$GENMKFILE_PATH'"
   echo "sudo chmod o+x '$GENMKFILE_PATH'"
   echo ""
fi

popd >/dev/null

"${GENMKFILE_PATH}/make-helper.bsh" "$@"
