#!/bin/bash

version="3.1.1"
reldate="2026/10/13"

scriptname=$(basename ${0})

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

confd="/usr/share/nanolx/"
source ${confd}${scriptname}.conf

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

apt_confd="/etc/apt/apt.conf.d/"
apt_prefd="/etc/apt/preferences.d/"
apt_srcd="/etc/apt/sources.list.d/"

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" "${apt_srcd}${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" "${apt_srcd}${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" "${apt_srcd}${repo}.sources"
                else
                    echo "failed to grab key ${1} from ${3}"
                fi
            ;;
            "" )
                cp "${sources_d}/${repo}.sources" "${apt_srcd}${repo}.sources"
            ;;
        esac
    else
        echo "disabling repo: ${repo}"
        rm "${apt_srcd}${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 ${apt_srcd}${1} ] && echo "enabled" || echo "disabled"
        ;;
        99* )
            [ -e ${apt_confd}${1} ] && echo "enabled" || echo "disabled"
        ;;
        pinning )
            [ -e ${apt_prefd}${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/
mozilla:        + [$(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
99-list-new    + [$(is_enabled 99-list-new)]      + List newly added packages after 'apt update'
pinning        + [$(is_enabled pinning)]          + Nanolx apt pinning configuration" | column -t -s"+"
    ;;
    add ) manage_repos "add" "${@}" ;;
    remove ) manage_repos "remove" "${@}" ;;
    enable-config )
        if [ -f ${apt_confd}99nanolx ]; then
            # remove file with old name
            rm ${apt_confd}99nanolx
        fi

        if [ ! -f ${apt_confd}99-nanolx ]; then
            echo "enabling nanolx apt configuration"
            cp ${confd}apt.d/99-nanolx ${apt_confd}99-nanolx
        else
            echo "nanolx apt configuration already enabled"
        fi
    ;;
    disable-config )
        if [ -f ${apt_confd}99nanolx ]; then
            # remove file with old name
            rm ${apt_confd}99nanolx
        fi

        if [ -f ${apt_confd}99-nanolx ]; then
            echo "disabling nanolx apt configuration"
            rm ${apt_confd}99-nanolx
        else
            echo "nanolx apt configuration not enabled"
        fi
    ;;
    enable-pinning )
        if [ -f ${apt_prefd}pinning ]; then
            echo "backing up previous apt pinning to ${apt_prefd}pinning.nanolx_old"
            cp ${apt_prefd}pinning{,.nanolx_old}
        fi
        echo "enabling nanolx apt pinning configuration"
        cp ${confd}apt.d/pinning ${apt_prefd}pinning
    ;;
    disable-pinning )
        if [ -f ${apt_prefd}pinning ]; then
            echo "disabling nanolx apt pinning configuration"
            rm ${apt_prefd}pinning
        else
            echo "nanolx apt pinning not in use"
        fi
        if [ -f ${apt_prefd}pinning.nanolx_old ]; then
            echo "resotoring previous apt pinning from ${apt_prefd}pinning.nanolx_old"
            mv ${apt_prefd}pinning{.nanolx_old,}
        fi
    ;;
    enable-listnew )
        if [ ! -f ${apt_confd}99-list-new ]; then
            echo "enabling listing new packages after 'apt update'"
            cp ${confd}apt.d/99-list-new ${apt_confd}99-list-new
        else
            echo "listing new packages after 'apt update' already enabled"
        fi
    ;;
    disable-listnew )
        if [ -f ${apt_confd}99-list-new ]; then
            echo "disabling listing new packages after 'apt update'"
            rm ${apt_confd}99-list-new
        else
            echo "listing new packages after 'apt update' not enabled"
        fi
    ;;
    show-new )
        if [ -f ${apt_confd}99-list-new ]; then
            nanolx-apt-hook show
        else
            echo "listing new packages after 'apt update' not enabled"
            echo "use 'nanolx-apt enable-listnew' to enable that feature"
        fi
    ;;
    clear-new )
        nanolx-apt-hook clear
    ;;
    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]

** Apt configuration

Nanolx features an optional, custom apt configuration, see

    ${confd}apt.d/99-nanolx

for details.

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

** Apt hook

Nanolx allows adding an apt hook listing new packages after 'apt update'

enable-listnew   enabling listing new packages after 'apt update'
disable-listnew  disabling listing new packages after 'apt update'
show-new         list newly added packages from 'apt update'
clear-new        clear history of newly added packages from 'apt update'

** Apt pinning

Nanolx features an optional custom apt pinning file, see

    ${confd}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
