#!/usr/bin/env bash
# ==========================================================================
# ____ _ _ _ _____ _
# | __ ) _ _(_) | __| | |_ _|__ ___ | |___
# | _ \| | | | | |/ _` |_____| |/ _ \ / _ \| / __|
# | |_) | |_| | | | (_| |_____| | (_) | (_) | \__ \
# |____/ \__,_|_|_|\__,_| |_|\___/ \___/|_|___/
#
# --- Build-Tools ---
# https://www.nntb.no/~dreibh/system-tools/
# ==========================================================================
#
# GitHub Actions Scripts
# Copyright (C) 2021-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 .
#
# Contact: thomas.dreibholz@gmail.com
# Bash options:
set -euo pipefail
# ====== Check arguments ====================================================
if [ $# -lt 1 ] ; then
echo >&2 "Usage: $0 compile|package ..."
exit 1
fi
# ====== Check arguments ====================================================
USE_PACKAGING=0
while [ $# -gt 0 ] ; do
TOOL="$1"
if [ "${TOOL}" == "compile" ] ; then
USE_PACKAGING=0
elif [ "$1" == "package" ] ; then
USE_PACKAGING=1
else
echo >&2 "ERROR: Invalid setting of TOOL=${TOOL}!"
exit 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 python3-packaging python3-urllib3"
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 qemu-user-binfmt 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}
# ====== Add COPR PPA dreibh/ppa =========================================
# NOTE: This is only available for Ubuntu!
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 < /etc/dpkg/dpkg.cfg.d/02apt-speedup" | \
OS="${OS}" DISTRIBUTION="${DIST}" ARCHITECTURE="${ARCH}" pbuilder login --save-after-exec
# ====== Add PPA dreibh/ppa ===========================================
if [ "${OS}" == "ubuntu" ] ; then
# Add PPA dreibh/ppa for Ubuntu:
OS="${OS}" DISTRIBUTION="${DIST}" ARCHITECTURE="${ARCH}" pbuilder login --save-after-login <&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 py311-packaging py311-urllib3"
# 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