#!/bin/bash

# ======================================================================
# Archivo:       removelist
# Ruta:          /usr/local/bin/
# Autor:         Charlie Martínez® <cmartinez@quirinux.org>
# Licencia:      https://www.gnu.org/licenses/gpl-2.0.txt
# Propósito:     Crea lista de paquetes a remover de una distro base.
# Distribución:  Quirinux 2.x
# ======================================================================

#
# Copyright (c) 2019-2025 Charlie Martínez, derechos reservados.
# Licencia: https://www.gnu.org/licenses/gpl-2.0.txt
# Usos autorizados y no autorizados de la marca Quirinux:
# Ver https://www.quirinux.org/aviso-legal
#

# removelist - generate packages_remove.txt from two package lists.
#
# Usage:
#   ./removelist [keep_list] [base_list]
#
# keep_list : packages that must REMAIN (your Quirinux list). First parameter.
# base_list : packages to filter (base distro list, e.g. Devuan). Second parameter.
#
# Result: packages_remove.txt = packages present in base_list but absent
# from keep_list, in YAML list format.
# If the parameters are omitted, the script asks for the paths interactively.

echo "=== Package Remove List Generator ==="
echo ""

if [ -z "$1" ]; then
    echo "Enter the path to the KEEP list (packages that should remain):"
    read -r KEEP_LIST
else
    KEEP_LIST="$1"
fi

if [ ! -f "$KEEP_LIST" ]; then
    echo "ERROR: file '$KEEP_LIST' not found."
    exit 1
fi

if [ -z "$2" ]; then
    echo "Enter the path to the BASE list (packages to filter):"
    read -r BASE_LIST
else
    BASE_LIST="$2"
fi

if [ ! -f "$BASE_LIST" ]; then
    echo "ERROR: file '$BASE_LIST' not found."
    exit 1
fi

OUTPUT_FILE="packages_remove.txt"

echo "Generating remove list..."
echo "  Keep list : $KEEP_LIST"
echo "  Base list : $BASE_LIST"
echo "  Output    : $OUTPUT_FILE"
echo ""

# Extract bare package names from any of the supported formats:
#   "    - package"  (YAML style, as produced by current-list.sh)
#   "package"        (plain)
#   "ii package ver arch description"  (dpkg -l style)
extract_names() {
    sed 's/^ *//; s/ *$//' "$1" \
        | grep -v '^#' | grep -v '^$' \
        | sed 's/^- *//' \
        | awk '{ if ($1 == "ii" || $1 == "rc" || $1 == "un") print $2; else print $1 }' \
        | sed 's/:.*//' \
        | sort -u
}

extract_names "$KEEP_LIST" > /tmp/keep_packages.txt
extract_names "$BASE_LIST" > /tmp/base_packages.txt

# Packages in base that are NOT in keep
comm -23 /tmp/base_packages.txt /tmp/keep_packages.txt > /tmp/remove_packages.txt

{
    echo "# Packages to remove (present in base but not in keep list)"
    sed 's/^/    - /' /tmp/remove_packages.txt
} > "$OUTPUT_FILE"

REMOVE_COUNT=$(wc -l < /tmp/remove_packages.txt)

echo "Remove list generated: $OUTPUT_FILE"
echo "Packages to remove: $REMOVE_COUNT"
echo ""
echo "First 10 entries:"
head -11 "$OUTPUT_FILE"
if [ "$REMOVE_COUNT" -gt 10 ]; then
    echo "..."
fi

rm -f /tmp/keep_packages.txt /tmp/base_packages.txt /tmp/remove_packages.txt

echo ""
echo "Next steps:"
echo "  1. Copy $OUTPUT_FILE into your costume directory"
echo "  2. Make sure index.yaml has: packages_remove_file: packages_remove.txt"
