#!/bin/bash

#----------------------------------------------------------------#
# speedtest-gui version 0.4.8                                    #
# Simple gui to test the speed of your internet bandwidth with   #
# speedtest.net                                                  #
# This program is free software; you can redistribute it and/or  #
# modify it under the terms of the GNU General Public License as #
# published by the Free Software Foundation; either version 3 of #
# the License, or (at your option) any later version.            #  
#----------------------------------------------------------------#

TITLE="Speedtest 0.4.8"
ICON="/usr/share/icons/hicolor/48x48/apps/speedtest.svg"
MICON="/usr/share/speedtest/icons/speedtest16.svg"
OK="/usr/share/speedtest/icons/ok.svg"
CLOSE="/usr/share/speedtest/icons/cancel.svg"
DATA="$HOME/.config/speedtest"
yad --title="SpeedTest" --width=400 --height=125 --fixed --center --on-top \
--text="\nFor best speed test results...\nStop all web activities before proceeding." \
--window-icon=$ICON \
--image=$ICON \
--button=Cancel!$CLOSE:1 \
--button=Ok!$OK:0 
ret=$?
[[ $ret -eq 1 ]] &&
kill -9 $(ps -ef | grep speedtest-cli | awk '{print $2}') 2>/dev/null &
kill -9 $(ps -ef | grep yad | awk '{print $2}') 2>/dev/null &
sleep 1 
rm -rf $DATA 
if [[ $ret -eq 0 ]]; then 
# Create a test directory
 if ! test -d $DATA  ; then
       mkdir -p $DATA 
# Create a file for image results
`echo \
"#!/bin/bash
yad --title=SpeedTest --center --fixed --window-icon="$MICON" \
--image=$HOME/.config/speedtest/image.png --button=Close!$CLOSE" | awk '{print $0}' > $DATA/show_image`
chmod a+x $DATA/show_image
 fi 
# Running speedtest: 
pkill -f "Speedtest" 
# Testing for network connection:
wget -q --tries=3 --timeout=10 --delete-after "https://speedtest.net" 
  if [[ $? -eq 0 ]]; then
 echo "Testing..." 2>/dev/null
   else
 echo $"Test failure" 2>/dev/null
yad --title="SpeedTest Error" --width=450 --height=100 --fixed --center --on-top \
--text="\nNo network connection to speedtest server?" \
--window-icon=$ICON \
--image=$ICON \
--button=Ok!$OK
exit 0
fi 
pkill -f /usr/share/speedtest/notify/main_menu.py
python3 /usr/share/speedtest/notify/speedtest_notify.py &
speedtest-cli --accept-license --selection-details|tee > $DATA/.tmp 
sleep 1
grep -E 'Result URL:' $DATA/.tmp|sed 's/[(].*$//'|awk '{$1=$1}{ print }'|sed 's/^............//'|sed 's/$/.png/' > $DATA/.tmp3
sleep 1
grep -E 'Download:' $DATA/.tmp|sed 's/[(].*$//'|awk '{$1=$1}{ print }' > $DATA/.tmp1
sleep 1 
grep -E 'Upload:' $DATA/.tmp|sed 's/[(].*$//'|awk '{$1=$1}{ print }' >> $DATA/.tmp1
sleep 1
awk ' BEGIN { first = 2; last = 4 }
{ for (i = first; i < last; i++) { printf("%s ", $i) } print $last }' $DATA/.tmp1 > $DATA/.tmp2
IMAGE=(`cat $DATA/.tmp3`)
wget -P $DATA "$IMAGE"
mv  $DATA/*.png $DATA/image.png 
convert -scale 488x260 $DATA/image.png $DATA/image.png
python3 /usr/share/speedtest/notify/main_menu.py &
python3 /usr/share/speedtest/notify/speedtest_menu.py
    fi
rm -f /tmp/tmp*    
exit 0 
