#!/bin/bash
########################################################
# Find the fastest PCLinuxOS repository                #      
#                                                      #
#  © 2024 WTFPL lxgator@gmail.com                      #
########################################################
ICON="/usr/share/fastrepo/icons/fastrepo.png"

# Need a network connection
wget -q --tries=3 --timeout=10 --delete-after "http://www.google.com/" 
if [[ $? -eq 0 ]]; then
  exec
else
 echo "Network failed"
ECON="/usr/share/fastrepo/icons/error.svg"
notify-send -t 15000 -i $ECON "No network Connection 
Check for network failure?"
exit 0
fi
rm -rf /tmp/fastrepo 2>/dev/null
if ! test -d /tmp/fastrepo  ; then
 mkdir -p /tmp/fastrepo
fi
while true; do 
sleep 0.1; 
echo "#Checking the repos d/load speeds. This may take time... Wait!" 2>/dev/null;
done|$(yad --progress --pulsate \
--title='Fastest Repository' \
--text="\n\Finding the fastest available repository" \
--width=500 \
--height=125 \
--center --no-escape \
--image="$ICON" \
--window-icon="$ICON" \
--button="Cancel"!/usr/share/fastrepo/icons/cancel.svg:/tmp/fastrepo/fastrepo) & 
touch /tmp/fastrepo/fastrepo
touch /tmp/fastrepo/tmp
touch /tmp/fastrepo/fail
while true; do
if grep -E "END" /tmp/fastrepo/tmp; then
paste <(echo -e "1. \n2. \n3. \n4. \n5. \n6. \n7. \n8. \n9. \n10. \n11. \n12. ") <(echo -e "`sort -Vr /tmp/fastrepo/fastrepo`") 2>&1 > /tmp/fastrepo/results
sleep 0.75
/usr/share/fastrepo/results &
pgrep -n yad > /tmp/pid
pkill -f /usr/bin/fastrepo &
PID=`cat /tmp/pid | awk '{print $0}'`
kill -9 $PID > /dev/null 2>&1 &
if [[ -n "$pid" ]] ; then
	kill "$pid"
fi
 fi
A=(insecure.nluug.nl/pub/os/Linux/distr/pclinuxos/pclinuxos/apt/pclinuxos/64bit/base/pkglist.x86_64.bz2)
URL=$(wget -O /dev/null --spider -S "$A" 2>&1)
echo "$URL" > /tmp/fastrepo/fail
FAIL=$(sed -n '3p' /tmp/fastrepo/fail | grep -o '[^ ]*' | cut -d' ' -f1|head -n2|tail -n1)
wget -q --tries=3 --timeout=10 --delete-after "$A" 
if [[ $? -eq 0 ]]; then
  exec $@
else
 echo "failed"
notify-send -t 15000 -i "/usr/share/fastrepo/icons/error.svg" "Repository failed!" "Repository https://$FAIL" &
fi
# END------------------------------------------------------------------------
B=(ftp.fau.de/pclinuxos/pclinuxos/apt/pclinuxos/64bit/base/pkglist.x86_64.bz2)
URL=$(wget -O /dev/null --spider -S "$B" 2>&1)
echo "$URL" > /tmp/fastrepo/fail
FAIL=$(sed -n '3p' /tmp/fastrepo/fail | grep -o '[^ ]*' | cut -d' ' -f1|head -n2|tail -n1)
wget -q --tries=3 --timeout=10 --delete-after "$B" 
if [[ $? -eq 0 ]]; then
  exec $@
else
 echo "failed"
notify-send -t 15000 -i "/usr/share/fastrepo/icons/error.svg" "Repository failed!" "Repository http://$FAIL" &
fi
# END------------------------------------------------------------------------
C=(mirror.math.princeton.edu/pub/pclinuxos/pclinuxos/apt/pclinuxos/64bit/base/pkglist.x86_64.bz2)
URL=$(wget -O /dev/null --spider -S "$C" 2>&1)
echo "$URL" > /tmp/fastrepo/fail
FAIL=$(sed -n '3p' /tmp/fastrepo/fail | grep -o '[^ ]*' | cut -d' ' -f1|head -n2|tail -n1)
wget -q --tries=3 --timeout=10 --delete-after "$C" 
if [[ $? -eq 0 ]]; then
  exec $@
else
 echo "failed"
notify-send -t 15000 -i "/usr/share/fastrepo/icons/error.svg" "Repository failed!" "Repository http://$FAIL" &
fi
# END------------------------------------------------------------------------
D=(pclinuxos.c3sl.ufpr.br/pclinuxos/apt/pclinuxos/64bit/base/pkglist.x86_64.bz2)
URL=$(wget -O /dev/null --spider -S "$D" 2>&1)
echo "$URL" > /tmp/fastrepo/fail
FAIL=$(sed -n '3p' /tmp/fastrepo/fail | grep -o '[^ ]*' | cut -d' ' -f1|head -n2|tail -n1)
wget -q --tries=3 --timeout=10 --delete-after "$D" 
if [[ $? -eq 0 ]]; then
  exec $@
else
 echo "failed"
notify-send -t 15000 -i "/usr/share/fastrepo/icons/error.svg" "Repository failed!" "Repository http://$FAIL" &
fi
# END------------------------------------------------------------------------
E=(ftp.cc.uoc.gr/mirrors/linux/pclinuxos/pclinuxos/apt/pclinuxos/64bit/base/pkglist.x86_64.bz2)
URL=$(wget -O /dev/null --spider -S "$E" 2>&1)
echo "$URL" > /tmp/fastrepo/fail
FAIL=$(sed -n '3p' /tmp/fastrepo/fail | grep -o '[^ ]*' | cut -d' ' -f1|head -n2|tail -n1)
wget -q --tries=3 --timeout=10 --delete-after "$E" 
if [[ $? -eq 0 ]]; then
  exec $@
else
 echo "failed"
notify-send -t 15000 -i "/usr/share/fastrepo/icons/error.svg" "Repository failed!" "Repository http://$FAIL" &
fi
# END------------------------------------------------------------------------
F=(distrib-coffee.ipsl.jussieu.fr/pub/linux/pclinuxos/pclinuxos/apt/pclinuxos/64bit/base/pkglist.x86_64.bz2)
URL=$(wget -O /dev/null --spider -S "$F" 2>&1)
echo "$URL" > /tmp/fastrepo/fail
FAIL=$(sed -n '3p' /tmp/fastrepo/fail | grep -o '[^ ]*' | cut -d' ' -f1|head -n2|tail -n1)
wget -q --tries=3 --timeout=10 --delete-after "$F" 
if [[ $? -eq 0 ]]; then
  exec $@
else
 echo "failed"
notify-send -t 15000 -i "/usr/share/fastrepo/icons/error.svg" "Repository failed!" "Repository http://$FAIL" &
fi
# END------------------------------------------------------------------------
G=(mirror.internode.on.net/pub/pclinuxos/pclinuxos/apt/pclinuxos/64bit/base/pkglist.x86_64.bz2)
URL=$(wget -O /dev/null --spider -S "$G" 2>&1)
echo "$URL" > /tmp/fastrepo/fail
FAIL=$(sed -n '3p' /tmp/fastrepo/fail | grep -o '[^ ]*' | cut -d' ' -f1|head -n2|tail -n1)
wget -q --tries=3 --timeout=10 --delete-after "$G" 
if [[ $? -eq 0 ]]; then
  exec $@
else
 echo "failed"
notify-send -t 15000 -i "/usr/share/fastrepo/icons/error.svg" "Repository failed!" "Repository http://$FAIL" &
fi
# END------------------------------------------------------------------------
H=(mirror.aarnet.edu.au/pub/pclinuxos/apt/pclinuxos/64bit/base/pkglist.x86_64.bz2)
URL=$(wget -O /dev/null --spider -S "$H" 2>&1)
echo "$URL" > /tmp/fastrepo/fail
FAIL=$(sed -n '3p' /tmp/fastrepo/fail | grep -o '[^ ]*' | cut -d' ' -f1|head -n2|tail -n1)
wget -q --tries=3 --timeout=10 --delete-after "$H" 
if [[ $? -eq 0 ]]; then
  exec $@
else
 echo "failed"
notify-send -t 15000 -i "/usr/share/fastrepo/icons/error.svg" "Repository failed!" "Repository http://$FAIL" &
fi
# END------------------------------------------------------------------------
I=(ftp.jaist.ac.jp/pub/Linux/PCLinuxOS/pclinuxos/apt/pclinuxos/64bit/base/pkglist.x86_64.bz2)
URL=$(wget -O /dev/null --spider -S "$I" 2>&1)
echo "$URL" > /tmp/fastrepo/fail
FAIL=$(sed -n '3p' /tmp/fastrepo/fail | grep -o '[^ ]*' | cut -d' ' -f1|head -n2|tail -n1)
wget -q --tries=3 --timeout=10 --delete-after "$I" 
if [[ $? -eq 0 ]]; then
  exec $@
else
 echo "failed"
notify-send -t 15000 -i "/usr/share/fastrepo/icons/error.svg" "Repository failed!" "Repository http://$FAIL" &
fi
# END------------------------------------------------------------------------
J=(mirrors.uni-ruse.bg/pclinuxos/pclinuxos/apt/pclinuxos/64bit/base/pkglist.x86_64.bz2)
URL=$(wget -O /dev/null --spider -S "$J" 2>&1)
echo "$URL" > /tmp/fastrepo/fail
FAIL=$(sed -n '3p' /tmp/fastrepo/fail | grep -o '[^ ]*' | cut -d' ' -f1|head -n2|tail -n1)
wget -q --tries=3 --timeout=10 --delete-after "$J" 
if [[ $? -eq 0 ]]; then
  exec $@
else
 echo "failed"
notify-send -t 15000 -i "/usr/share/fastrepo/icons/error.svg" "Repository failed!" "Repository http://$FAIL" &
fi
# END------------------------------------------------------------------------
K=(pclinuxos.mirror.wearetriple.com/pclinuxos/apt/pclinuxos/64bit/base/pkglist.x86_64.bz2)
URL=$(wget -O /dev/null --spider -S "$K" 2>&1)
echo "$URL" > /tmp/fastrepo/fail
FAIL=$(sed -n '3p' /tmp/fastrepo/fail | grep -o '[^ ]*' | cut -d' ' -f1|head -n2|tail -n1)
wget -q --tries=3 --timeout=10 --delete-after "$K" 
if [[ $? -eq 0 ]]; then
  exec $@
else
 echo "failed"
notify-send -t 15000 -i "/usr/share/fastrepo/icons/error.svg" "Repository failed!" "Repository http://$FAIL" &
fi
# END------------------------------------------------------------------------
L=(mirrors.cicku.me/pclinuxos/pclinuxos/apt/pclinuxos/64bit/base/pkglist.x86_64.bz2)
URL=$(wget -O /dev/null --spider -S "$L" 2>&1)
echo "$URL" > /tmp/fastrepo/fail
FAIL=$(sed -n '3p' /tmp/fastrepo/fail | grep -o '[^ ]*' | cut -d' ' -f1|head -n2|tail -n1)
wget -q --tries=3 --timeout=10 --delete-after "$L" 
if [[ $? -eq 0 ]]; then
  exec $@
else
 echo "failed"
notify-send -t 15000 -i "/usr/share/fastrepo/icons/error.svg" "Repository failed!" "Repository http://$FAIL" &
fi
# END------------------------------------------------------------------------
echo "END" >> /tmp/fastrepo/tmp

for i in $A $B $C $D $E $F $G $H $I $J $K $L ; do
TEST=$(echo http://$i);
RESULTS=$(wget -O /dev/null --report-speed=bits $i 2>&1 | grep -o "[0-9.,]\+ [KM]*[Bb]/s")
echo "$RESULTS $TEST" >> /tmp/fastrepo/fastrepo
sleep 1
done  
done




