#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later

# This script returns the list of configured "deb" sources in one-line-style
# source lists (/etc/apt/sources.list, /etc/apt/sources.list.d/*.list).
# See sources.list(5) for more information on the APT source list formats.
#
# This script takes no arguments.

set -eu

# grep(1) returns 1 if no lines were selected from the input files, which
# can happen if there are .list files, but they contain no "deb" entries.
# We don't want to fail in this case, hence the "|| true", which we put
# in a subshell because we want it to affect only the grep invocation.
{
    if [ -f /etc/apt/sources.list ]; then
        printf '%s\0' /etc/apt/sources.list
    fi
    if [ -d /etc/apt/sources.list.d ]; then
        find /etc/apt/sources.list.d -maxdepth 1 -type f -regex '.*/[a-zA-Z0-9_.-]+\.list' -print0 \
            | LC_ALL=C sort --zero-terminated
    fi
} | xargs -0 --no-run-if-empty -- sh -c 'grep -h "^deb " "$@" || true' "$(basename "$0")"
