#!/usr/bin/env bash set -e declare -r REPO_KEY_URL="https://debian.syshawk.net/repo.key" declare -r REPO_FILE_NAME="syshawk" declare -r REPO_DIST_URL="https://debian.syshawk.net/" declare -r SOURCE_LIST_DIR="/etc/apt/sources.list.d" declare -a temporaryInstalledPackage=() log_error() { local msg="$1" shift printf >&2 "$msg\n" "$@" } command_exists() { LAST_COMMAND_CHECK=$1 command -v "$@" &> /dev/null } apt_update() { force="$1" if [[ -n "$force" || -z "$cleanAptUpdate" ]]; then apt-get -qq update > /dev/null cleanAptUpdate=1 fi } apt_install() { local -a toInstall=() for package in "$@"; do if ! ( dpkg -l "$package" 2> /dev/null | grep -q '^ii' ); then toInstall+=("$package") temporaryInstalledPackage+=("$package") fi done if [[ ${#toInstall[@]} -gt 0 ]]; then apt_update DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "${toInstall[@]}" fi } download() { local URL="$1" local DEST="$2" if command_exists curl; then curl --fail --location --silent --show-error --output "$DEST" "$URL" elif command_exists wget; then wget --quiet --output-document="$DEST" "$URL" else apt_install curl download "$@" fi } get_repo_file() { local repo_file_name="$1" printf "%s/%s.sources" "$SOURCE_LIST_DIR" "$repo_file_name" } add_repo() { local REPO_ARCH="$1" local repo_file_name="$2" local repo_dist_url="$3" local REPO_SUITES="$4" local REPO_COMPONENT="$5" local REPO_KEY="$6" REPO_FILE=$(get_repo_file "$repo_file_name") if [[ -f "$REPO_FILE" ]]; then printf "\n" >> "$REPO_FILE" fi REPO_TYPE="deb" [[ -z "$ADD_SRC" ]] || REPO_TYPE="deb deb-src" cat <> $REPO_FILE Types: $REPO_TYPE URIs: $repo_dist_url Suites: $REPO_SUITES Components: $REPO_COMPONENT Architectures: $DEB_ARCH Signed-By: EOF sed 's/^$/./; s/^/ /' "$REPO_KEY" >> "$REPO_FILE" } trap_exit() { if [[ -n "$WORKING_DIR" && -d "$WORKING_DIR" ]]; then rm -rf "$WORKING_DIR" fi if [[ ${#temporaryInstalledPackage[@]} -gt 0 ]]; then apt-get remove --auto-remove --purge -y "${temporaryInstalledPackage[@]}" > /dev/null 2>&1 find /var/cache/apt/archives/ -xdev -type f -delete fi if [[ -n "$cleanAptUpdate" ]]; then find /var/lib/apt/lists/ -xdev -type f -delete fi } # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ trap trap_exit EXIT if [[ ! -r /etc/os-release ]]; then log_error "Distribution non reconnue" exit 1 fi . /etc/os-release if [[ "$ID" != "debian" ]]; then log_error "Distribution non supportée : %s" "$ID" exit 1 fi if ! command_exists dpkg \ || ! command_exists apt-get \ || ! command_exists grep \ || ! command_exists rm \ || ! command_exists find; then log_error "Commande vitale manquante : %s" "$LAST_COMMAND_CHECK" exit 1 fi DEB_ARCH=$(dpkg --print-architecture) case "$DEB_ARCH" in amd64|arm64) ;; *) log_error "Architecture Debian non supportée : %s" "$DEB_ARCH" exit 1 ;; esac if [[ $EUID -ne 0 ]]; then log_error "Cette commande doit être executée avec les droits root" exit 1 fi case "$VERSION_CODENAME" in bookworm|trixie|forky) ;; *) log_error "Distribution Debian non supportée : %s" "$VERSION_CODENAME" exit 1 ;; esac WORKING_DIR=$(mktemp -d) download "$REPO_KEY_URL" "$WORKING_DIR/repo.key" if [[ -f $(get_repo_file "$REPO_FILE_NAME") ]]; then log_error "Dépôt déjà configuré : %s présent" $(get_repo_file "$REPO_FILE_NAME") exit 1 fi add_repo "$DEB_ARCH" "$REPO_FILE_NAME" "$REPO_DIST_URL" "$VERSION_CODENAME" "main" "$WORKING_DIR/repo.key" if [[ -n "$DOCKER_IMAGE_DEV" ]]; then add_repo "$DEB_ARCH" "$REPO_FILE_NAME" "$REPO_DIST_URL" "$VERSION_CODENAME-dev" "main" "$WORKING_DIR/repo.key" fi