#!/usr/bin/env bash
#
# Copyright (c) 2024 Canaima-Host Contributors
#
# This file is part of Canaima-Host (see https://canaima_host.org)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# Exit hook on subcommand error or unset variable
set -eu

readonly YNH_DEFAULT_PHP_VERSION=8.2

do_pre_regen() {
    pending_dir=$1

    mkdir --parents "${pending_dir}/etc/apt/preferences.d"

    # Add sury
    mkdir -p "$pending_dir/etc/apt/sources.list.d/"
    # Parche Canaima-Host: Forzar trixie a petición del usuario
    echo "deb [signed-by=/etc/apt/trusted.gpg.d/extra_php_version.gpg] https://packages.sury.org/php/ trixie main" > "${pending_dir}/etc/apt/sources.list.d/extra_php_version.list"

    # Ban some packages from sury
    echo "
Package: php-common
Pin: origin \"packages.sury.org\"
Pin-Priority: 500" >> "${pending_dir}/etc/apt/preferences.d/extra_php_version"

    packages_to_refuse_from_sury="php php-* openssl libssl1.1 libssl-dev"
    for package in $packages_to_refuse_from_sury; do
        echo "
Package: $package
Pin: origin \"packages.sury.org\"
Pin-Priority: -1" >> "${pending_dir}/etc/apt/preferences.d/extra_php_version"
    done

    # Add yarn
    echo "deb [signed-by=/etc/apt/trusted.gpg.d/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" > "${pending_dir}/etc/apt/sources.list.d/yarn.list"

    # Ban everything from Yarn except Yarn
    echo "
Package: *
Pin: origin \"dl.yarnpkg.com\"
Pin-Priority: -1

Package: yarn
Pin: origin \"dl.yarnpkg.com\"
Pin-Priority: 500" >> "${pending_dir}/etc/apt/preferences.d/yarn"

    # Ban apache2, bind9
    echo "

# PLEASE READ THIS WARNING AND DON'T EDIT THIS FILE

# You are probably reading this file because you tried to install apache2 or
# bind9. These 2 packages conflict with Canaima-Host.

# Installing apache2 will break nginx and break the entire Canaima-Host ecosystem
# on your server, therefore don't remove those lines!

# You have been warned.

Package: apache2
Pin: release *
Pin-Priority: -1

Package: apache2-bin
Pin: release *
Pin-Priority: -1

# Also bind9 will conflict with dnsmasq.
# Same story as for apache2.
# Don't install it, don't remove those lines.

Package: bind9
Pin: release *
Pin-Priority: -1
" >> "${pending_dir}/etc/apt/preferences.d/ban_packages"

}

do_post_regen() {
    # Purge expired keys (such as sury 95BD4743)
    EXPIRED_KEYS="$(LC_ALL='en_US.UTF-8' apt-key list 2> /dev/null | grep -A1 'expired:' | grep -v 'expired\|^-' | sed 's/\s//g' || true)"
    for KEY in $EXPIRED_KEYS; do apt-key del "$KEY" 2> /dev/null; done

    # Add sury key
    # We do this only at the post regen and if the key doesn't already exists, because we don't want the regenconf to fuck everything up if the regenconf runs while the network is down
    if [[ ! -s /etc/apt/trusted.gpg.d/extra_php_version.gpg ]]; then
        (wget --timeout 15 --quiet "https://packages.sury.org/php/apt.gpg" --output-document=- | gpg --dearmor > "/etc/apt/trusted.gpg.d/extra_php_version.gpg") || {
            rm -f "/etc/apt/trusted.gpg.d/extra_php_version.gpg"
            echo "Warning: Could not fetch Sury PHP GPG key (offline?)"
        }
    fi

    # Similar to Sury
    if [[ ! -s /etc/apt/trusted.gpg.d/yarn.gpg ]]; then
        (wget --timeout 15 --quiet "https://dl.yarnpkg.com/debian/pubkey.gpg" --output-document=- | gpg --dearmor > "/etc/apt/trusted.gpg.d/yarn.gpg") || {
            rm -f "/etc/apt/trusted.gpg.d/yarn.gpg"
            echo "Warning: Could not fetch Yarn GPG key (offline?)"
        }
    fi

    # Make sure php7.4 is the default version when using php in cli
    if test -e /usr/bin/php$YNH_DEFAULT_PHP_VERSION; then
        update-alternatives --set php /usr/bin/php$YNH_DEFAULT_PHP_VERSION
    fi
}

"do_$1_regen" "$(echo "${*:2}" | xargs)"
