#!/bin/bash
# testing-packages.sh
#
# Testing package list + standalone reporter.
#
# Sourced (by build_world.sh):  declares TESTING_LIST, no side effects.
# Executed directly:            reports official packages in $PKG_DIR/<series>/
#                               that are missing from $CUSTOM_OUTPUT and not
#                               named in TESTING_LIST.
#
# Examples:
#   ./testing-packages.sh --series a
#   ./testing-packages.sh --series a --series ap --series d
#   ./testing-packages.sh                     # all series
#   ./testing-packages.sh --series a --include-testing

TESTING_LIST=(
#sway testing (can move to above series later)
wayland
wayland-protocols
wayland-utils
xorg-server-xwayland
libdrm
libinput
pixman
cairo
mesa
harfbuzz
pango
gtkmm3
glibmm
cairomm
libsigc++
vulkan-sdk
fmt
gtk-layer-shell
libva
libvdpau
libva-utils
pipewire
wireplumber
libdisplay-info
xdg-desktop-portal
xdg-desktop-portal-gtk
alsa-lib
upower
spdlog
libevdev
mtdev
libepoxy
libxcvt
xkeyboard-config
wlroots
utf8proc
libX11
libxcb
libXext
libXrender
libXt
libXtst
libXau
libXdmcp
libXfont2

#fcft tllist should be built and available before building sway, waybar, or foot

#

# adding rest of the a series
acpi
bcachefs-tools
dbus
devs
dracut
ed
elvis
exfatprogs
gptfdisk
grub
haveged
iniparser
inotify-tools
jfsutils
kmscon
lbzip2
lhasa
libblockdev
libbytesize
libcgroup
libgudev
libtsm
lrzip
mcelog
minicom
mlocate
moreutils
mt-st
mtx
ncompress
ndctl
ntfs-3g
nut
nvi
os-prober
pcmciautils
quota
sdparm
splitvt
sysfsutils
sysvinit-functions
tcsh
thin-provisioning-tools
time
udisks2
unarj
usb_modeswitch
utempter
volume_key
zerofree
zoo

# l series
GConf
Imath
LibRaw
Mako
PyQt-builder
PyQt6
PyQt6_sip
SDL2
SDL2_gfx
SDL2_image
SDL2_mixer
SDL2_net
SDL2_ttf
SDL3
SDL3_ttf
a52dec
aalib
abseil-cpp
accountsservice
adwaita-icon-theme
adwaita-icon-theme-legacy
alsa-oss
alsa-plugins
aom
appstream
apr
apr-util
aspell
aspell-en
at-spi2-core
atkmm
atkmm2
audiofile
avahi
babl
blake3
bstring
cairomm1
ccid
cfitsio
chmlib
clucene
cmark
cmark-gfm
colord
cryfs
cryptopp
cxxopts
db48
dbus-glib
dbus-python
dconf
dconf-editor
ddcutil
desktop-file-utils
djvulibre
dotconf
dtc
duktape
dvdauthor
ebook-tools
editorconfig-core-c
eigen
enchant
esound
espeak-ng
exiv2
farstream
fast_float
ffmpeg
fluidsynth
fprintd
freecell-solver
frei0r-plugins
fuse
gcr
gcr4
gdk-pixbuf2-xlib
gegl
gexiv2
gi-docgen
gjs
glade
glib
glib-networking
glibmm2
gmime
gmm
gnome-keyring
gnome-themes-extra
gnu-efi
gobject-introspection
graphene
graphviz
gsettings-desktop-schemas
gsl
gspell
gst-plugins-bad-free
gst-plugins-base
gst-plugins-good
gst-plugins-libav
gstreamer
gtk+
gtk+2
gtk+3
gtk4
gtkmm2
gtkmm4
gtksourceview4
gtkspell
gvfs
hicolor-icon-theme
highway
hunspell
hyphen
icon-naming-utils
id3lib
immer
iso-codes
jansson
jasper
jemalloc
jmtpfs
json-glib
kdsoap
keybinder3
lager
lame
lcms
lensfun
leptonica
libQuotient
libadwaita
libao
libappindicator
libass
libasyncns
libatasmart
libbluray
libburn
libcaca
libcacard
libcanberra
libcbor
libcddb
libcdio
libcdio-paranoia
libcec
libclc
libcue
libcupsfilters
libdaemon
libdbusmenu
libdbusmenu-qt
libdiscid
libdmtx
libdvdnav
libdvdread
libev
libexif
libfakekey
libfido2
libfprint
libfyaml
libgit2
libglade
libgnome-keyring
libgnt
libgphoto2
libgpod
libgsf
libgtop
libgusb
libhandy
libical
libid3tag
libidl
libidn
libieee1284
libimobiledevice
libimobiledevice-glue
libindicator
libiodbc
libjxl
libkarma
libmad
libmaxminddb
libmcrypt
libmng
libmtp
libnice
libnih
libnjb
libnl
libnotify
libnsl
libnss_nis
libnvme
libodfgen
libogg
liboggz
libopusenc
libpaper
libplacebo
libplist
libppd
libproxy
libptytty
libqalculate
libraw1394
librevenge
librsvg
libsamplerate
libsass
libsigc++3
libsigsegv
libsndfile
libsoup
libsoup3
libspectre
libtatsu
libtheora
libtraceevent
libtracefs
libunibreak
libusb-compat
libusbmuxd
libvips
libvisio
libvisual
libvisual-plugins
libvncserver
libvorbis
libvpx
libwnck
libwnck3
libwpd
libwpg
libxkbcommon
libxklavier
libxmlb
lmdb
log4cplus
loudmouth
luajit-lgi
media-player-info
meson-python
mhash
mlt
mm
movit
mozilla-nss
mujs
neon
netpbm
newt
nlohmann-json
olm
openal-soft
openblas
opencv
openexr
openjph
opensc
opentimelineio
opus
opusfile
orc
p8-platform
pangomm
pangomm2
pcaudiolib
pcre
pcsc-lite
pdfio
phonon
phonon-backend-gstreamer
phonon-backend-mpv
pilot-link
polkit
polkit-gnome
polkit-qt-1
poppler
poppler-data
potrace
protobuf
pulseaudio
pycairo
pycups
pycurl
pygobject3
pyparsing
python-PyYAML
python-accessible-pygments
python-alabaster
python-appdirs
python-attrs
python-babel
python-calver
python-certifi
python-cffi
python-chardet
python-charset-normalizer
python-distro
python-dnspython
python-docutils
python-doxypypy
python-doxyqml
python-editables
python-future
python-glad2
python-hatch-fancy-pypi-readme
python-hatch-vcs
python-html5lib
python-idna
python-imagesize
python-importlib_metadata
python-lxml
python-markdown
python-notify2
python-numpy
python-pbr
python-pillow
python-ply
python-psutil
python-pybind11
python-pycparser
python-pygments
python-pyproject_metadata
python-pysol_cards
python-random2
python-requests
python-roman-numerals
python-sane
python-scikit-build-core
python-six
python-smartypants
python-snowballstemmer
python-sphinx
python-sphinx_rtd_theme
python-tomli-w
python-typogrify
python-urllib3
python-versioneer
python-webencodings
python-zipp
qrencode
qt6
qtkeychain
quazip
range-v3
readstat
rnnoise
rpcsvc-proto
rttr
rubygem-asciidoctor
sbc
sdl
seatd
serf
sg3_utils
shared-mime-info
simdutf
sip
slang
slang1
sof-firmware
sound-theme-freedesktop
speech-dispatcher
speex
speexdsp
spirv-llvm-translator
startup-notification
suitesparse
svgalib
system-config-printer
t1lib
taglib
talloc
tango-icon-theme
tango-icon-theme-extras
tdb
tesseract
tevent
tidy-html5
unicode-ucd
usbredir
v4l-utils
vde2
vid.stab
vte
wavpack
woff2
xapian-core
zint
zug
zxing-cpp

# d series

# the end


)
# ----------------------------------------------------------------------
# When sourced, stop here. Only the TESTING_LIST declaration above is
# exposed to the caller. Do not add anything below that runs at source
# time.
# ----------------------------------------------------------------------
if [ "${BASH_SOURCE[0]}" != "${0}" ]; then
    return 0
fi

# ----------------------------------------------------------------------
# Standalone reporter
# ----------------------------------------------------------------------

set -uo pipefail
BUILD_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
PKG_DIR="$(cd -- "$BUILD_DIR/../../../slackware-current/repo/slackware64" && pwd -P)"
CUSTOM_OUTPUT="$(cd -- "$BUILD_DIR/../amos64" && pwd -P)"

usage() {
    cat <<EOF
Usage: $(basename "$0") [--series SERIES]... [--include-testing] [-h]

Reports official packages that exist in \$PKG_DIR/<series>/ but are
not in \$CUSTOM_OUTPUT (any series) and not named in TESTING_LIST.

  --series SERIES     Series to inspect (repeatable). Default: all.
  --include-testing   Also report packages named in TESTING_LIST,
                      tagged [testing] on the output line.
  -h, --help          This help.

Output: one "series/filename.txz" per line on stdout; summaries on stderr.
EOF
}

SERIES=()
INCLUDE_TESTING=0

while [ $# -gt 0 ]; do
    case "$1" in
        --series)
            [ -n "${2:-}" ] || { echo "ERROR: --series requires an argument" >&2; exit 1; }
            SERIES+=("$2"); shift 2 ;;
        --include-testing)
            INCLUDE_TESTING=1; shift ;;
        -h|--help)
            usage; exit 0 ;;
        *)
            echo "ERROR: unknown option: $1" >&2
            usage >&2
            exit 1 ;;
    esac
done

[ -d "$PKG_DIR" ]       || { echo "ERROR: PKG_DIR not found: $PKG_DIR" >&2; exit 1; }
[ -d "$CUSTOM_OUTPUT" ] || { echo "ERROR: CUSTOM_OUTPUT not found: $CUSTOM_OUTPUT" >&2; exit 1; }

if [ ${#SERIES[@]} -eq 0 ]; then
    mapfile -t SERIES < <(find "$PKG_DIR" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort)
fi

for s in "${SERIES[@]}"; do
    [ -d "$PKG_DIR/$s" ] || { echo "ERROR: series not in PKG_DIR: $s" >&2; exit 1; }
done

# Identical to build_world.sh's pkg_basename — must stay in sync.
pkg_basename() {
    echo "$1" | sed -E 's/-[^-]+-[^-]+-[^-]+\.t[a-z]z$//'
}

declare -A TESTING_SET=()
for p in "${TESTING_LIST[@]}"; do
    TESTING_SET["$p"]=1
done

# Filenames present anywhere in $CUSTOM_OUTPUT (across all series).
# Matches build_world.sh's reverse-check semantics: a package is
# "present" iff its exact filename exists somewhere in the tree.
declare -A CUSTOM_FILES=()
while IFS= read -r -d '' f; do
    CUSTOM_FILES["$(basename "$f")"]=1
done < <(find "$CUSTOM_OUTPUT" -name '*.t?z' -type f -print0 2>/dev/null)

total_missing=0
total_testing=0

for s in "${SERIES[@]}"; do
    missing_in_series=0
    testing_in_series=0

    for pkgfile in "$PKG_DIR/$s"/*.t?z; do
        [ -f "$pkgfile" ] || continue
        filename=$(basename "$pkgfile")

        # Already built somewhere in custom output.
        [ -n "${CUSTOM_FILES[$filename]:-}" ] && continue

        base=$(pkg_basename "$filename")

        if [ -n "${TESTING_SET[$base]:-}" ]; then
            if [ "$INCLUDE_TESTING" -eq 1 ]; then
                printf '%s\t[testing]\n' "$base"
                testing_in_series=$((testing_in_series + 1))
            fi
            continue
        fi

        printf '%s\n' "$base"
        missing_in_series=$((missing_in_series + 1))
    done

    total_missing=$((total_missing + missing_in_series))
    total_testing=$((total_testing + testing_in_series))

    if [ "$missing_in_series" -gt 0 ] || [ "$testing_in_series" -gt 0 ]; then
        printf '%s: %d missing' "$s" "$missing_in_series" >&2
        [ "$INCLUDE_TESTING" -eq 1 ] && printf ', %d in testing list' "$testing_in_series" >&2
        printf '\n' >&2
    fi
done

echo "" >&2
if [ "$INCLUDE_TESTING" -eq 1 ]; then
    echo "Total: $total_missing missing (excl. testing), $total_testing in testing list" >&2
else
    echo "Total: $total_missing missing" >&2
fi