#!/usr/bin/bash
set -e

# speed up sort and comm
export LC_ALL=C

source /etc/os-release
if [[ ! "$1" =~ ^[0-9]*$ ]]
then
    echo "usage: $(basename "$0") [fedora linux version number as digits]."
    exit 1
fi
UPGRADE_FROM="$1"
if [ -z "$UPGRADE_FROM" ]; then
        UPGRADE_FROM=$(("$VERSION_ID"-1))
fi
UPGRADE_TO="$VERSION_ID"
echo "Looking for retired packages between Fedora Linux $UPGRADE_FROM and Fedora Linux $UPGRADE_TO"
echo "Retired packages are no longer maintained. Answer N to the following questions to keep them,"
echo "but these packages will not get any updates. Not even security updates."

TDIR=$(mktemp --directory --tmpdir remove-retired.XXXXXXXXX)
TO_REMOVE="$TDIR/retired"
OLD_LIST="$TDIR/old"
NEW_LIST="$TDIR/new"
INST_LIST="$TDIR/installed"
RM_LIST="$TDIR/toberemoved"

function pause() {
   # clear the stdin buffer and pause with question
   read -t 1 -n 10000 discard || [ $? -gt 128 ]
   read -p "Hit Enter to continue with other package or Ctrl + C to interrupt."
}


#--repo={fedora,fedora-modular,updates,updates-modular}-source


echo "Gathering package list for Fedora Linux $UPGRADE_FROM"
# in case the metadata are signed and GPG key is not yet imported
dnf repoquery -q --releasever "$UPGRADE_FROM" --disableplugin=local XXXXXXXXXXXX
# sed from the repoquery can be remove when dnf4 will be a history
dnf repoquery -q --releasever "$UPGRADE_FROM" --disableplugin=local --qf="%{name}\n" | sort | sed -r '/^\s*$/d' > "$OLD_LIST"
#repoquery --releasever "$UPGRADE_FROM" --arch src -a | pkgname | sort | uniq > "$OLD_LIST"
echo "Gathering package list for Fedora Linux $UPGRADE_TO"
# in case the metadata are signed and GPG key is not yet imported
dnf repoquery -q --releasever "$UPGRADE_TO" --disableplugin=local XXXXXXXXXXXX
dnf repoquery -q --releasever "$UPGRADE_TO" --disableplugin=local --qf="%{name}\n" | sort | sed -r '/^\s*$/d' > "$NEW_LIST"
comm -23 "$OLD_LIST" "$NEW_LIST" > "$TO_REMOVE"

echo "Asking for super user access:"
sudo true Executed from remove-retired-packages

echo "These packages have been retired:"
rpm -qa --qf '%{name}\n' | sort > "$INST_LIST"
comm -12  "$TO_REMOVE" "$INST_LIST" > "$RM_LIST"
while read PACKAGE; do
        SUMMARY=$(rpm -q --qf "%{summary}" "$PACKAGE" | head -n1 )
        echo "$PACKAGE: $SUMMARY"
done < "$RM_LIST"

while read -u 7 PACKAGE; do
        #skip if this package is not installed
        rpm -q "$PACKAGE" 2>/dev/null >&2 || continue

        SUMMARY=$(rpm -q --qf "%{summary}" "$PACKAGE" | head -n1 )
        echo "Removing $PACKAGE: $SUMMARY"
        SRPMFILENAME=$(rpm -q --qf "%{sourcerpm}" "$PACKAGE")
        SRPM=$(rpm-print-name-from-filename "$SRPMFILENAME")
        REASON=$(curl --fail https://src.fedoraproject.org/rpms/$SRPM/raw/rawhide/f/dead.package 2>/dev/null || echo "unknown reason")
        if [ "$REASON" = "unknown reason" ]; then
                if curl --fail https://src.fedoraproject.org/rpms/$SRPM/raw/rawhide/f/${SRPM}.spec 2>/dev/null >/dev/null; then
                        REASON="$PACKAGE was subpackage of $SRPM, but this subpackages was removed"
                fi
        fi
        echo "Reason of retirement: $REASON"
        sudo dnf remove "$PACKAGE" || pause
done 7< "$RM_LIST"

# cleanup
rm -rf "$TDIR"
