#! /bin/bash -eu

# My custom gbp hook:
# - invoked in the Build directory
# and has $GBP_GIT_DIR pointing at git sources
#-----------------------------------------

# - so this updates the Version?
# - keeps my list of Git repositories from which I build ~/.deb-gits

# - ?? this used to generate the debian/control and hence install dependencies ???
# - invoke local  ./auto


# to-be-generated
# function debian_control_automatic() {
#     count1=$(git ls-files debian/control.in|wc -c)

#     count2=$(git ls-files debian/control|wc -c)
#     test \( $count1 != 0 -a $count2 = 0 \)
# }

copy-snapshot-changelog()
{
    # So, git-xxx checks out from Git.

    # But, before that, git-dch is run, and possibly debian/changelog
    # is `not' commited to git. -- I need to keep it there, to have increasing
    # version number!
    # So, git-xxx would retrieve previous version.

    # This works together with  ./snaprel which creates the changelog!

    # cwd is the build/working copy
    # GBP_GIT_DIR is the .git directory.
    # what if it's a worktree ?
    cp --target-directory=debian $GBP_GIT_DIR/../debian/changelog
    # this is useless:
    # DISTRIBUTION=focal
    if false
    then
        sed --regexp-extended -e "1s/([[:alnum:]]+) (\([[:graph:]]+\)) [[:alnum:]]+(;.*)$/\1 \2 $DISTRIBUTION\3/" \
            $GBP_GIT_DIR/../debian/changelog > $local_dir/debian/changelog
    fi
}

echo "------------------- pre-build-hook invoked ----------" >&2
arch=""
# this should be obsolete/deprecated .. use dpkg-checkbuilddeps (to avoid root password if possible)
skip_build_depends=n
while getopts :a:s OPT; do
    case $OPT in
        a|+a)
            arch="$OPTARG"
            ;;
        s)
            skip_build_depends=y
            ;;
        *)
            echo "usage: ${0##*/} [+-a ARG} [--] ARGS..."
            exit 2
    esac
done
shift $(( OPTIND - 1 ))
OPTIND=1

set -x
if [ $GBP_GIT_DIR = $(pwd) ]; then
    echo "Your export-dir of git-buildpackage is unset" >&2
    echo "So your build directory is the same as (git) Source dir" >&2
else
    copy-snapshot-changelog
fi

# if [ ! -e debian/control ]
# then
#     generate-debian-control
# fi


which add-deb-git && add-deb-git $GBP_GIT_DIR/..

# Generate stuff for  ./configure
if [ -e preauto ]
then
    ./preauto
fi

if [[ $skip_build_depends != y ]]
then
    DISTRO=sid
    if [ -n "$arch" ]
    then
        echo GBP_TMP_DIR = $GBP_TMP_DIR
        enter-container --distro $DISTRO --arch $arch \
                        --bind $(realpath $GBP_TMP_DIR/..) \
                        --root --chdir $GBP_TMP_DIR \
                        --verbose -- \
                        /usr/bin/mk-build-deps --install --remove \
                        "--tool=\"apt -o Debug::pkgProblemResolver=yes --no-install-recommends -y\""
    else
        # why not debi --with-depends --tool ?
        # install
        (
            # One publishes to hardcoded /tmp and then?
            # or is it permission issue?
            unset TMPDIR;
            set -x
            /usr/bin/mk-build-deps --install --remove --root-cmd=sudo --tool="apt --no-install-recommends -y -f"
         )
        # debpkg -i   -> conflict with -f option
    fi
else
    : not_needed
    # (set -x; dpkg-checkbuilddeps || :)
fi

# Generate stuff for  ./configure
if [ -e auto ]
then
    ./auto
fi


echo "------------------- pre-build-hook finished ----------" >&2
