#!/bin/bash
#/usr/local/bin/slackupdr_checker
# Check for .new config files and then (k)eep, (o)verwrite or (r)emove

# Ensure the script is executed with sudo
if [ "$EUID" -ne 0 ]; then
    echo "This script must be run with sudo."
    exec sudo "$0" "$@"
fi

CONFIG_DIR="/etc"  # Modify this path if necessary
NEW_FILES=$(find "$CONFIG_DIR" -name "*.new")

for NEW_FILE in $NEW_FILES; do
    OLD_FILE="${NEW_FILE%.new}"

    echo -e "\nProcessing: $NEW_FILE"

    if [ -f "$OLD_FILE" ]; then
        echo "Diff between $OLD_FILE and $NEW_FILE:"
        diff -u "$OLD_FILE" "$NEW_FILE" | less -X  # Pipe to 'less' for better readability
    else
        echo "New config file found: $NEW_FILE (no existing counterpart)"
    fi

    echo -e "\nChoose an action:"
    echo "  (K)eep the old file and the new file"
    echo "  (O)verwrite old file with new (.orig backup)"
    echo "  (R)emove the .new file"
    echo -n "Your choice: "
    read -n 1 -r CHOICE # single keypress
    echo # newline after keypress

    case "$CHOICE" in
        K|k) echo "Keeping $OLD_FILE and $NEW_FILE" ;;
        O|o)
            mv "$OLD_FILE" "$OLD_FILE.orig"
            mv "$NEW_FILE" "$OLD_FILE"
            echo "Overwritten $OLD_FILE with $NEW_FILE (backup saved as .orig)"
            ;;
        R|r)
            rm "$NEW_FILE"
            echo "Removed $NEW_FILE"
            ;;
        *)
            echo "Invalid choice, skipping..."
            ;;
    esac
done

echo "Processing complete!"

