#!/usr/bin/env bash
#
# GitHub Actions Scripts
# Copyright (C) 2021-2024 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


# ====== Check arguments ====================================================
USE_PACKAGING=0
while [ $# -gt 0 ] ; do
   if [ "$1" == "package" ] ; then
      USE_PACKAGING=1
   fi
   shift
done


# ====== Check/set environment variables ====================================
if [ ! -e /etc/os-release ] ; then
   echo >&2 "ERROR: /etc/os-release does not exist!"
   exit 1
fi
. /etc/os-release


# ====== Ubuntu/Deban =======================================================
if [ "${ID}" == "ubuntu" ] || [ "${ID}" == "debian" ] ; then
   PACKAGES="python3 python3-distro"
   if [ -v CC ] ; then
      if [[ "${CC}" =~ .*gcc.* ]] ; then
         PACKAGES="${PACKAGES} gcc"
      elif [[ "${CC}" =~ .*clang.* ]] ; then
         PACKAGES="${PACKAGES} clang"
      fi
   fi
   if [ ${USE_PACKAGING} -eq 1 ] ; then
      # Need to install pbuilder as well:
      PACKAGES="${PACKAGES} build-essential debian-archive-keyring debian-ports-archive-keyring devscripts distro-info eatmydata fakeroot pbuilder qemu-user-static sudo"
   fi

   apt-get update -qq
   # DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade -qy
   # shellcheck disable=SC2086
   DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::=--force-confold -o Dpkg::Options::=--force-confdef --no-install-recommends \
      ${PACKAGES}

   if [ "${ID}" == "ubuntu" ] ; then
      # Add PPA dreibh/ppa for Ubuntu:
      DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::=--force-confold -o Dpkg::Options::=--force-confdef --no-install-recommends \
         software-properties-common
      apt-add-repository -y ppa:dreibh/ppa || true
      apt-get update -q
   fi

   if [ ${USE_PACKAGING} -eq 1 ] ; then
      # ====== pbuilder environment =========================================
      # Example for GitHub Actions:
      # https://github.com/jrl-umi3218/github-actions/tree/master/setup-pbuilder

      if [ ! -v OS ] || [ "${OS}" == "" ] ; then
         OS="${ID}"
      fi
      if [ ! -v DIST ] ||  [ "${DIST}" == "" ] ; then
         DIST="${VERSION_CODENAME}"
      fi
      if [ ! -v ARCH ] || [ "${ARCH}" == "" ] ; then
         ARCH="$(dpkg --print-architecture)"
      fi

      if [ "${OS}" == "ubuntu" ] ; then
         COMPONENTS="main universe"
         MIRRORSITE=http://dk.archive.ubuntu.com/ubuntu/
         KEYRING="/usr/share/keyrings/ubuntu-archive-keyring.gpg"
      elif [ "${OS}" == "debian" ] ; then
         COMPONENTS="main"
         if [ "${ARCH}" == "m68k" ] || [ "${ARCH}" == "riscv64" ] ; then
            # Debian Ports (special architectures)
            MIRRORSITE="http://ftp.ports.debian.org/debian-ports/"
            KEYRING="/usr/share/keyrings/debian-ports-archive-keyring.gpg"
         else
            # Debian (supported architectures)
            MIRRORSITE="http://ftp.dk.debian.org/debian/"
            KEYRING="/usr/share/keyrings/debian-archive-keyring.gpg"
         fi
      else
         echo >&2 "ERROR: Unknown distribution ${ID}!"
         exit 1
      fi

      cores=$(getconf _NPROCESSORS_ONLN 2>/dev/null || true)
      cat >/etc/pbuilderrc <<EOF
#!/bin/bash -e

# ====== Settings ===========================================================
DISTRIBUTION="${DIST}"
COMPONENTS="${COMPONENTS}"
MIRRORSITE="${MIRRORSITE}"
DEBOOTSTRAPOPTS=("\${DEBOOTSTRAPOPTS[@]}" "--keyring=${KEYRING}")
DEBOOTSTRAPOPTS=("\${DEBOOTSTRAPOPTS[@]}" "--variant=buildd")
ARCHITECTURE="${ARCH}"

APTCACHEHARDLINK=no

USECOLORS=yes
COMPRESSPROG=cat
EXTRAPACKAGES=eatmydata
EATMYDATA=yes

# Multi-core: set concurrency level. The packaging scripts will handle it properly:
export CONCURRENCY_LEVEL=${cores}
export DEB_BUILD_OPTIONS="parallel=${cores}"

# ====== Directories ========================================================
NAME="\${OS}-\${DISTRIBUTION}-\${ARCHITECTURE}"
BASETGZ="/var/cache/pbuilder/\${NAME}-base.tgz"
APTCACHE="/var/cache/pbuilder/aptcache/\${NAME}/"
BUILDPLACE="/var/cache/pbuilder/build/\${NAME}/"
BUILDRESULT="/var/cache/pbuilder/result/\${NAME}/"
HOOKDIR="/var/cache/pbuilder/hook.d/"

mkdir -p \${APTCACHE}
mkdir -p \${BUILDRESULT}
mkdir -p \${HOOKDIR}
EOF
      echo "----- /etc/pbuilderrc: ------------------------------------------------------"
      cat /etc/pbuilderrc
      echo "-----------------------------------------------------------------------------"

      OS="${OS}" DISTRIBUTION="${DIST}" ARCHITECTURE="${ARCH}" pbuilder create \
         --debootstrapopts --variant=buildd \
         | grep -v "^I: Retrieving\|^I: Validating\|^I: Unpacking\|^I: Extracting\|^I: Configuring\|^I: new cache content"
         # --debootstrapopts --keyring=/etc/apt/trusted.gpg

      # Speed up pbuilder:
      echo "echo \"force-unsafe-io\" > /etc/dpkg/dpkg.cfg.d/02apt-speedup" | \
         OS="${OS}" DISTRIBUTION="${DIST}" ARCHITECTURE="${ARCH}" pbuilder login --save-after-exec

      # ====== Add ppa:dreibh/ppa, updates and backports =================
      if [ "${OS}" == "ubuntu" ] ; then
         # Add PPA dreibh/ppa for Ubuntu:
         OS="${OS}" DISTRIBUTION="${DIST}" ARCHITECTURE="${ARCH}" pbuilder login --save-after-login <<EOF
DEBIAN_FRONTEND=noninteractive apt-get install -qqy -o Dpkg::Options::=--force-confold -o Dpkg::Options::=--force-confdef --no-install-recommends \
software-properties-common
apt-add-repository -y ppa:dreibh/ppa
apt-get update -q
EOF
      fi
   fi


# ====== Fedora =============================================================
elif [ "${ID}" == "fedora" ] ; then
   PACKAGES="clang make python3 python3-distro"
   if [ ${USE_PACKAGING} -eq 1 ] ; then
      # Need to install mock as well:
      PACKAGES="${PACKAGES} fedora-release findutils mock nosync rpmdevtools   which strace sudo"
   fi

   # dnf upgrade -qy
   # shellcheck disable=SC2086
   dnf install -qy ${PACKAGES}

   if [ ${USE_PACKAGING} -eq 1 ] ; then
      # ====== Mock environment =============================================
      groupadd -f mock

      if [ ! -v OS ] || [ "${OS}" == "" ] ; then
         OS="${ID}"
      fi
      if [ ! -v DIST ] ||  [ "${DIST}" == "" ] ; then
         DIST="${VERSION_ID}"
      fi
      if [ ! -v ARCH ] || [ "${ARCH}" == "" ] ; then
         ARCH="$(uname -m)"
      fi

      if ! grep "^\[copr-dreibh-ppa\]" "/etc/mock/${OS}-${DIST}-${ARCH}.cfg" ; then
         shopt -s extglob
         ppa="config_opts['dnf.conf'] += \"\"\"\n[copr-dreibh-ppa]\nname=Copr repo for ppa owned by dreibh\nbaseurl=https://copr-be.cloud.fedoraproject.org/results/dreibh/ppa/fedora-\$releasever-\$basearch/\ntype=rpm-md\nskip_if_unavailable=True\ngpgcheck=1\ngpgkey=https://copr-be.cloud.fedoraproject.org/results/dreibh/ppa/pubkey.gpg\nrepo_gpgcheck=0\nenabled=1\n\"\"\""
         ppa="${ppa//+( )$/\\n}"
         echo -e "${ppa}" | tee -a "/etc/mock/${OS}-${DIST}-${ARCH}.cfg"
         if ! grep "^\[copr-dreibh-ppa\]" "/etc/mock/${OS}-${DIST}-${ARCH}.cfg" ; then
            echo >&2 "ERROR: Unable to inject PPA configuration into Mock configuration file /etc/mock/${OS}-${DIST}-${ARCH}.cfg!"
            exit 1
         fi
      fi
   fi


# ====== FreeBSD ============================================================
elif [ "${ID}" == "freebsd" ] ; then
   PACKAGES="autoconf automake bash gcc libtool git python3 py311-distro"

   # shellcheck disable=SC2086
   ASSUME_ALWAYS_YES=yes pkg install -y ${PACKAGES}

   if [ ! -e /usr/ports/.git ] ; then
      rm -rf /usr/ports/* /usr/ports/.??* || true
      mkdir -p /usr/ports
      cd /usr/ports
      git init -b main
      git remote add freebsd https://git.freebsd.org/ports.git
      git remote set-url --push freebsd ssh://git@gitrepo.freebsd.org/ports.git
      git config --add remote.freebsd.fetch "+refs/notes/*:refs/notes/*"
      git config pull.rebase true
      git pull --depth=1 freebsd main
      git branch --set-upstream-to=freebsd/main main
   fi

# ====== Unknown ============================================================
else

   echo >&2 "ERROR: Unknown distribution ${ID}!"
   exit 1

fi
