#!/usr/bin/env bash
# ==========================================================================
#              ____        _ _     _     _____           _
#             | __ ) _   _(_) | __| |   |_   _|__   ___ | |___
#             |  _ \| | | | | |/ _` |_____| |/ _ \ / _ \| / __|
#             | |_) | |_| | | | (_| |_____| | (_) | (_) | \__ \
#             |____/ \__,_|_|_|\__,_|     |_|\___/ \___/|_|___/
#
#                           --- Build-Tools ---
#                https://www.nntb.no/~dreibh/system-tools/
# ==========================================================================
#
# Packaging Scripts
# Copyright (C) 2017-2026 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: thomas.dreibholz@gmail.com

# Bash options:
set -eu


TOOLS_DIRECTORY="$(dirname "$0")/../../build-tools"
WORKFLOWS_DIRECTORY="$(dirname "$0")/../../build-tools-template"

TOOLS="
   autogen.sh
"

CI_TOOLS="
   build-tool
   ci-build
   ci-install
   ci-setup
   ci-test
   get-dependencies
"

WORKFLOWS="
   freebsd.yml
   linux-debian.yml
   linux-fedora.yml
   linux-ubuntu.yml
"


# ====== Check options ======================================================
GIT=0
while [ $# -gt 0 ] ; do
   if [[ "$1" =~ ^(-|--)git$ ]] ; then
      GIT=1
   else
      echo >&2 "Usage: update-tools [--git]"
      exit 1
   fi
   shift
done


# ====== Copy tools =========================================================
for tool in ${TOOLS} ; do
   echo "Copying ${tool} ..."
   cp "${TOOLS_DIRECTORY}/${tool}" .
done
if [ -d ci/ ] ; then
   for tool in ${CI_TOOLS} ; do
      echo "Copying ci/${tool} ..."
      cp "${TOOLS_DIRECTORY}/ci/${tool}" ci/
   done
fi
if [ -d .github/workflows ] ; then
   for workflow in ${WORKFLOWS} ; do
      echo "Copying ci/${workflow} ..."
      if [ -e ".github/workflows/${workflow}" ] ; then
         cp "${WORKFLOWS_DIRECTORY}/.github/workflows/${workflow}" ".github/workflows/${workflow}"
      else
         echo "SKIPPING workflow ${workflow}!"
      fi
   done
fi


# ====== Git commits ========================================================
if [ $GIT -ne 0 ] ; then
   echo "Committing tools ..."
   toolsToCommit=""
   for tool in ${TOOLS} ; do
      if [ -e "${tool}" ] ; then
         toolsToCommit="${toolsToCommit} ${tool}"
      fi
   done
   git commit -m "Updated build scripts" "${toolsToCommit}" || true

   if [ -d ci/ ] ; then
      pushd ci/ >/dev/null

      echo "Committing CI tools ..."
      # shellcheck disable=SC2086
      git add ${CI_TOOLS}
      # shellcheck disable=SC2086
      git commit -m "Updated CI scripts" ${CI_TOOLS} || true

      popd >/dev/null
   fi
   if [ -e .github/workflows ] ; then
      echo "Committing CI configuration ..."
      git commit -m "Updated CI configuration" .github/workflows || true
   fi
fi

echo "Done!"
