#!/bin/bash

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

#set -x

if [ "$GENMKFILE_DEBUG" = "1" ]; then
   set -x
fi

set -e

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

true "$0: 1: $1"
true "$0: make_cross_build_platform_list: $make_cross_build_platform_list"

if [ -f /usr/libexec/helper-scripts/pre.bsh ]; then
   ## 'pre.bsh' would `source` the following folders if they exist:
   ## /etc/make-helper-one.bsh_pre.d/*.conf
   ## /usr/local/etc/make-helper-one.bsh_pre.d/*.conf
   source /usr/libexec/helper-scripts/pre.bsh
   true
fi

true "$0: GENMKFILE_PATH: $GENMKFILE_PATH"

## For manual invocation.
## Should be set by makefile already.
if [ "$GENMKFILE_PATH" = "" ] ; then
   if [ -f "./packages/kicksecure/genmkfile/usr/share/genmkfile/makefile-full" ]; then
      GENMKFILE_PATH="./packages/kicksecure/genmkfile/usr/share/genmkfile"
   elif [ -f "./usr/share/genmkfile/makefile-full" ]; then
      GENMKFILE_PATH="./usr/share/genmkfile"
   elif [ -f "/usr/share/genmkfile/makefile-full" ]; then
      GENMKFILE_PATH="/usr/share/genmkfile"
   else
      echo "$0: ERROR: Auto detection of GENMKFILE_PATH failed!"
   fi
fi

export GENMKFILE_PATH

## sets: architecture_of_source_package
## sets: make_debian_control_file_relative_path
## sets: make_debian_control_file_absolute_path
## sets: dpkg_print_architecture
source "$GENMKFILE_PATH/shared.bsh"
make_shared_function

export architecture_of_source_package

## If $make_cross_build_platform_list is unset, just build same platform as
## output of 'dpkg --print-architecture'.
if [ "$make_cross_build_platform_list" = "" ]; then
   make_cross_build_platform_list="$dpkg_print_architecture"
fi

## Just in case?
if [ "$architecture_of_source_package" = "" ]; then
   architecture_of_source_package="$dpkg_print_architecture"
fi

for arch in $make_cross_build_platform_list ; do
   target_architecture="$arch"
   export target_architecture
done

if [ "$1" = "deb-pkg" ]; then
   "$GENMKFILE_PATH/make-helper-one.bsh" deb-cleanup
   "$GENMKFILE_PATH/make-helper-one.bsh" dist
   "$GENMKFILE_PATH/make-helper-one.bsh" debdist
   "$GENMKFILE_PATH/make-helper-one.bsh" debdsc
   "$GENMKFILE_PATH/make-helper-one.bsh" deb-pkg-build
   exit $?
fi

if [ "$1" = "deb-icup" ]; then
   "$GENMKFILE_PATH/make-helper-one.bsh" deb-cleanup
   "$GENMKFILE_PATH/make-helper-one.bsh" dist
   "$GENMKFILE_PATH/make-helper-one.bsh" debdist
   "$GENMKFILE_PATH/make-helper-one.bsh" debdsc
   "$GENMKFILE_PATH/make-helper-one.bsh" deb-pkg-build
   "$GENMKFILE_PATH/make-helper-one.bsh" deb-install
   "$GENMKFILE_PATH/make-helper-one.bsh" deb-cleanup
   exit $?
fi

for arch in $make_cross_build_platform_list ; do
   target_architecture="$arch"
   export target_architecture
   "$GENMKFILE_PATH/make-helper-one.bsh" "$@"

   ## If $architecture_of_source_package is set to 'all', in other words if it is
   ## an uncompiled package, there is no reason to iterate over
   ## $make_cross_build_platform_list.
   break
done
