#!/bin/bash

version="3.0.6"
reldate="2026/09/14"

scriptname=$(basename ${0})

if (( EUID != 0 )); then
    echo "${scriptname} must be run as root"
    exit 1
fi

source /usr/share/nanolx/${scriptname}.conf

[[ -d "${key_base}" ]] || mkdir -p "${key_base}"

manage_repos_int () {
    if [[ "${mode}" = "add" ]]; then
        case "${1}" in
            *.asc | *.gpg | *.key )
                wget "${1}" -O "${2}"
                if ! echo "${3}" "${2}" | sha256sum -c &>/dev/null; then
                    echo "SHA256 checksum for ${2} is wrong"
                    rm "${2}"
                else
                    echo "enabling repo: ${repo}"
                    cp "${sources_d}/${repo}.sources" "/etc/apt/sources.list.d/${repo}.sources"
                fi
            ;;
            *.deb )
                wget "${1}" -O "/var/cache/nanolx/${2}"
                if ! echo "${3}" "/var/cache/nanolx/${2}" | sha256sum -c &>/dev/null; then
                    echo "SHA256 checksum for ${2} is wrong"
                    rm "/var/cache/nanolx/${2}"
                else
                    dpkg -i "/var/cache/nanolx/${2}"
                    echo "enabling repo: ${repo}"
                    cp "${sources_d}/${repo}.sources" "/etc/apt/sources.list.d/${repo}.sources"
                fi
            ;;
            0x* )
                gpg --keyserver "${3}" --recv-keys "${1}"
                gpg --export "${1}" > "${2}"
                if [ -f "${2}" ]; then
                    echo "enabling repo: ${repo}"
                    cp "${sources_d}/${repo}.sources" "/etc/apt/sources.list.d/${repo}.sources"
                else
                    echo "failed to grab key ${1} from ${3}"
                fi
            ;;
            "" )
                cp "${sources_d}/${repo}.sources" "/etc/apt/sources.list.d/${repo}.sources"
            ;;
        esac
    elif [[ "${mode}" = "check" ]]; then
        if [ -f "/etc/apt/sources.list.d/${repo}.sources" ]; then
            echo "repo ${repo}:       enabled"
        else
            echo "repo ${repo}:       disabled"
        fi
    else
        echo "disabling repo: ${repo}"
        rm "/etc/apt/sources.list.d/${repo}.sources"
    fi
}

manage_repos () {
    mode="${1}"
    shift

    for repo in "${@}"; do
        case ${repo} in
            debian ) manage_repos_int ;;
            nanolx ) manage_repos_int "${nanolx}" "${nanolx_store}" "${nanolx_sha}" ;;
            liquorix ) manage_repos_int "${liquorix}" "${liquorix_store}" "${liquorix_sha}" ;;
            winehq ) manage_repos_int "${winehq}" "${winehq_store}" "${winehq_sha}" ;;
            i2p ) manage_repos_int "${i2p}" "${i2p_store}" "${ip2_sha}" ;;
            deb-multimedia ) manage_repos_int "${debmultimedia_url}" \
                "${debmultimedia_pkg}" "${debmultimedia_sha}" ;;
            mozilla ) manage_repos_int "${mozilla}" "${mozilla_store}" "${mozilla_sha}" ;;
            * ) echo "repository ${repo} unknown." ;;
        esac
    done

    [[ ! "${mode}" = "check" ]] && apt update -q
}

is_enabled () {
    case ${1} in
        *.sources )
            [ -e /etc/apt/sources.list.d/${1} ] && echo "enabled" || echo "disabled"
        ;;
        99* )
            [ -e /etc/apt/apt.conf.d/${1} ] && echo "enabled" || echo "disabled"
        ;;
        pinning )
            [ -e /etc/apt/preferences.d/${1} ] && echo "enabled" || echo "disabled"
        ;;
    esac
}

user_opt="${1}"
shift

case ${user_opt} in
    list )
        echo "${scriptname} supports the following repos:"
        echo "
Debian:         + [$(is_enabled debian.sources)]            + https://debian.org (full suite: stable->experimental)
nanolx:         + [$(is_enabled nanolx.sources)]            + https://nanolx.org/photonic/
liquorix:       + [$(is_enabled liquorix.sources)]          + https://liquorix.net/
i2p:            + [$(is_enabled i2p.sources)]               + https://deb.i2p.net/
winehq:         + [$(is_enabled winehq.sources)]            + https://gitlab.winehq.org/wine/wine/-/wikis/Debian-Ubuntu
debmultimedia:  + [$(is_enabled deb-multimedia.sources)]    + https://deb-multimedia.org/
mozillavpn:     + [$(is_enabled mozilla.sources)]           + https://support.mozilla.org/en-US/kb/how-install-mozilla-vpn-linux-computer" | column -t -s"+"

        echo "
additional apt configuration status:"
        echo "
99-nanolx      + [$(is_enabled 99-nanolx)]        + Nanolx apt configuration
99-keep-refind + [$(is_enabled 99-keep-refind)]   + Keep refind as default bootmanager when grub/shim are updated
pinning        + [$(is_enabled pinning)]          + Nanolx apt pinning configuration" | column -t -s"+"
    ;;
    add ) manage_repos "add" "${@}" ;;
    remove ) manage_repos "remove" "${@}" ;;
    check ) manage_repos "check" ${all_repos} ;;
    enable-config )
        if [ -f /etc/apt/apt.conf.d/99nanolx ]; then
            # remove file with old name
            rm /etc/apt/apt.conf.d/99nanolx
        fi

        if [ ! -f /etc/apt/apt.conf.d/99-nanolx ]; then
            echo "enabling nanolx apt configuration"
            cp /usr/share/nanolx/apt.d/99-nanolx /etc/apt/apt.conf.d/99-nanolx
        else
            echo "nanolx apt configuration already enabled"
        fi
    ;;
    disable-config )
        if [ -f /etc/apt/apt.conf.d/99nanolx ]; then
            # remove file with old name
            rm /etc/apt/apt.conf.d/99nanolx
        fi

        if [ -f /etc/apt/apt.conf.d/99-nanolx ]; then
            echo "disabling nanolx apt configuration"
            rm /etc/apt/apt.conf.d/99-nanolx
        else
            echo "nanolx apt configuration not enabled"
        fi
    ;;
    enable-pinning )
        if [ -f /etc/apt/preferences.d/pinning ]; then
            echo "backing up previous apt pinning to /etc/apt/preferences.d/pinning.nanolx_old"
            cp /etc/apt/preferences.d/pinning{,.nanolx_old}
        fi
        echo "enabling nanolx apt pinning configuration"
        cp /usr/share/nanolx/apt.d/pinning /etc/apt/preferences.d/pinning
    ;;
    disable-pinning )
        if [ -f /etc/apt/preferences.d/pinning ]; then
            echo "disabling nanolx apt pinning configuration"
            rm /etc/apt/preferences.d/pinning
        else
            echo "nanolx apt pinning not in use"
        fi
        if [ -f /etc/apt/preferences.d/pinning.nanolx_old ]; then
            echo "resotoring previous apt pinning from /etc/apt/preferences.d/pinning.nanolx_old"
            mv /etc/apt/preferences.d/pinning{.nanolx_old,}
        fi
    ;;
    version)
        echo "${scriptname} ${version} (${reldate})"
    ;;
    * )
        echo -e "${scriptname} v${version} (${reldate})
© 2014-2026 Christopher Roy Bratusek <nano@jpberlin.de>

licensed under the GNU General Public License v3 (or newer)

Helper script for apt related tasks

usage: ${scriptname} <flag>

version          print version

** Repository management
list             list available repositories and status
add              [repo1...repo2]
remove           [repo1...repo2]

check            check which repos are enabled

** Apt configuration

Nanolx features an optional, custom apt configuration, see

    /usr/share/nanolx/apt.d/99-nanolx

for details.

enable-config    enable 99-nanolx apt config
disable-config   disable 99-nanolx apt config

** Apt pinning

Nanolx features an optional custom apt pinning file, see

    /usr/share/nanolx/apt.d/pinning

for details, basically it ensures that extra repositories are prefered over
Debian repos, for Debian unstable > testing > stable > experimental.

enable-pinning   enable nanolx apt pinning
disable-pinning  disable nanolx apt pinning
"
esac
