#!/bin/bash

# See https://www.q4os.org/forum/viewtopic.php?pid=29988#p29988
# Usage: 
# - make this script executable: 
#   $ chmod +x test-apt-lists.sh
# - run it with sudo: 
#   $ sudo ./test-apt-lists.sh

# Save working directory path and cd to apt directory.
mydir=$(pwd)
cd /etc/apt/sources.list.d/

echo -n "Renaming apt .list and .sources files to .bak files... "
for f in *.list *.sources; do
	[ -f "$f" ] && mv "$f" "$f".bak
done
echo "Done."

echo -n "Test apt update while its .list and .sources files are 'hidden'... "
apt update 1> /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "ERROR even without any enabled list."
	echo "Restoring original names and exiting..."
	for f in *.bak; do
		original=$(basename -s.bak "$f")
		[ -f "$f" ] && mv "$f" "$original"
	done
	exit 1
fi
echo "Successful."

echo "Testing apt update for each .list and .sources file, separately..."
for f in *.bak; do
	original=$(basename -s.bak "$f")
	[ -f "$f" ] && mv "$f" "$original"
	apt update 1> /dev/null 2>&1
	[ $? -eq 0 ] && echo "- OK for $original" || echo "- ERROR with $original"
	# Rename again as .bak after the test to avoid any later update.
	mv "$original" "$f"
done

echo "Restoring all .list and .sources original names... "
for f in *.bak; do
	original=$(basename -s.bak "$f")
	[ -f "$f" ] && mv "$f" "$original"
done

# cd back to initial directory and exit with no error.
cd $mydir
exit 0
