#!/usr/bin/env bash
#
# Copyright (c) 2024 YunoHost Contributors
#
# This file is part of YunoHost (see https://yunohost.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"
    mkdir --parents "${pending_dir}/etc/apt/sources.list.d"

    # Remove renamed files
    touch "${pending_dir}/etc/apt/preferences.d/extra_php_version"
    touch "${pending_dir}/etc/apt/preferences.d/ban_packages"

    # .list files were replaced with deb822 .sources files
    touch "${pending_dir}/etc/apt/sources.list.d/extra_php_version.list"
    touch "${pending_dir}/etc/apt/sources.list.d/yarn.list"

    # Yarn repository was deleted
    touch "${pending_dir}/etc/apt/preferences.d/yarn"
    touch "${pending_dir}/etc/apt/sources.list.d/yarn.sources"

    # Copy the configuration files
    cp /usr/share/yunohost/conf/apt/preferences.d/* "${pending_dir}/etc/apt/preferences.d"
    cp /usr/share/yunohost/conf/apt/sources.list.d/* "${pending_dir}/etc/apt/sources.list.d"
}

_update_key() {
    url=$1
    target=$2

    if [[ ! -s "$target" ]]; then
        curl --connect-timeout 900 -s -o - "$url" | gpg --dearmor -o "$target"
    fi
}

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

    # Ensure the yunohost repo is not in sources.list
    if [ -f "/etc/apt/sources.list" ]; then
        sed '/yunohost.org/d' -i "/etc/apt/sources.list"
    fi

    # Ensure the legacy, unmanaged file was deleted
    if [ -f /etc/apt/sources.list.d/yunohost.list ]; then
        rm -rf /etc/apt/sources.list.d/yunohost.list
    fi

    # Download GPG keys
    # 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
    _update_key "https://packages.sury.org/php/apt.gpg" "/usr/share/keyrings/sury_php.gpg"
    _update_key "https://repo.yunohost.org/keys/yunohost_trixie.asc" "/usr/share/keyrings/yunohost-trixie.gpg"

    # Delete legacy repository keys
    rm -f "/etc/apt/trusted.gpg.d/yarn.gpg"
    # GPG keys were moved to /usr/share/keyrings
    rm -f "/etc/apt/trusted.gpg.d/extra_php_version.gpg"
    rm -f "/etc/apt/trusted.gpg.d/yunohost-trixie.gpg"

    # 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)"
