#!/bin/sh
set -e

ALT_COMMON_REPO_PATH="/etc/apt/sources.list.d/alt-common-els.list"
ALT_COMMON_ROLLOUT_REPO_PATH="/etc/apt/sources.list.d/alt-common-els-rollout.list"
ROLLOUT_SLOTS="1 2 3 4 5 6"
GPG_KEY_PATH_SOURCE="/usr/share/alt-common-release/keyrings/tuxcare-keyring.gpg"
GPG_KEY_PATH_DEST="/etc/apt/trusted.gpg.d/tuxcare-keyring.gpg"

install_gpg_key() {
    if [ ! -f "$GPG_KEY_PATH_SOURCE" ]; then
        echo "Error: Source GPG key file not found: $GPG_KEY_PATH_SOURCE" >&2
        return 1
    fi

    mkdir -p "$(dirname "$GPG_KEY_PATH_DEST")"

    if cp "$GPG_KEY_PATH_SOURCE" "$GPG_KEY_PATH_DEST"; then
        chmod 644 "$GPG_KEY_PATH_DEST"
        echo "GPG key installed successfully"
    else
        echo "Error: Failed to copy GPG key from $GPG_KEY_PATH_SOURCE to $GPG_KEY_PATH_DEST" >&2
        return 1
    fi
}

create_alt_common_repo() {
    OS_CODENAME=$(grep '^VERSION_CODENAME=' /etc/os-release | cut -d'=' -f2 | sed -e 's@"@@g')
    OS_VER=$(grep '^VERSION_ID=' /etc/os-release | cut -d'=' -f2 | sed -e 's@"@@g')
    OS_NAME=$(grep '^ID=' /etc/os-release | cut -d'=' -f2 | sed -e 's@"@@g')
    _arch=$(dpkg --print-architecture)

    if [ -z "$_arch" ]; then
        echo "Can't create repository: unknown arch"
        return 0
    fi

    mkdir -p "$(dirname "$ALT_COMMON_REPO_PATH")"

    cat <<EOF > "$ALT_COMMON_REPO_PATH"
### THIS FILE IS AUTOMATICALLY CONFIGURED ###
# You may comment out this entry, but any other modifications may be lost.
deb [arch=${_arch}] https://repo.alt.tuxcare.com/alt-common/deb/${OS_NAME}/${OS_VER}/stable ${OS_CODENAME} main
EOF

    chmod 644 "$ALT_COMMON_REPO_PATH"
    echo "Repository configuration created successfully"
}

# Gradual-rollout slots els-alt-1..6, mirroring the rpm rollout repos (ELS-2641).
# Layout from build-system-configs/repositories/slots/els_alt_common.json: the
# rollout host carries no channel segment (unlike the stable URL above) and
# takes no credentials, so no /etc/apt/auth.conf.d entry is needed.
# All six slot lines are active: the host serves only the slot this client is
# assigned to. The slot-N-bypass entries are the opt-in immediate update --
# uncomment one, then run apt update.
create_alt_common_rollout_repo() {
    OS_CODENAME=$(grep '^VERSION_CODENAME=' /etc/os-release | cut -d'=' -f2 | sed -e 's@"@@g')
    OS_VER=$(grep '^VERSION_ID=' /etc/os-release | cut -d'=' -f2 | sed -e 's@"@@g')
    OS_NAME=$(grep '^ID=' /etc/os-release | cut -d'=' -f2 | sed -e 's@"@@g')
    _arch=$(dpkg --print-architecture)

    if [ -z "$_arch" ]; then
        echo "Can't create rollout repository: unknown arch"
        return 0
    fi

    mkdir -p "$(dirname "$ALT_COMMON_ROLLOUT_REPO_PATH")"

    {
        echo "### THIS FILE IS AUTOMATICALLY CONFIGURED ###"
        echo "# You may comment out these entries, but any other modifications may be lost."
        for slot in $ROLLOUT_SLOTS; do
            echo "deb [arch=${_arch}] https://rollout.alt.tuxcare.com/alt-common/slot-${slot}/deb/${OS_NAME}/${OS_VER} ${OS_CODENAME} main"
        done
        echo "# Immediate update, bypassing the gradual rollout: uncomment one line below."
        for slot in $ROLLOUT_SLOTS; do
            echo "# deb [arch=${_arch}] https://rollout.alt.tuxcare.com/alt-common/slot-${slot}-bypass/deb/${OS_NAME}/${OS_VER} ${OS_CODENAME} main"
        done
    } > "$ALT_COMMON_ROLLOUT_REPO_PATH"

    chmod 644 "$ALT_COMMON_ROLLOUT_REPO_PATH"
    echo "Rollout repository configuration created successfully"
}

install_gpg_key
create_alt_common_repo
create_alt_common_rollout_repo
