#!/bin/bash

INFO=$(yad --form --width=340 --title="HamClock Setup" \
	--separator="|" --item-separator="|" \
	--text="\r\nDo you want to use the Quadra as a <b>dedicated, full-screen HamClock</b> on a TV or Monitor?" \
	--image="/usr/share/pixmaps/hamclock-screenshot-small.png" \
	--button="Ask Me Later":2 \
	--button="No":0 \
        --button="Yes":1 "" "" "")
BUT=$(echo $?)

#echo "button is $BUT"

# treat "No" and "close box" as "No" and clear autostart.  Use desktop icon (or menu) to re-run
if [ ${BUT} -eq 0 ] || [ ${BUT} -eq 252 ]; then
#	echo "clearing hamclock and hamclock-setup desktop files from autostart"
	if [ -f "/home/quadra/.config/autostart/hamclock.desktop" ]; then
		rm /home/quadra/.config/autostart/hamclock.desktop
	fi
	if [ -f "/home/quadra/.config/autostart/hamclock-setup.desktop" ]; then
		rm /home/quadra/.config/autostart/hamclock-setup.desktop
	fi
	sync
	exit

# Ask Me Later, just exit without clearing anything - will continue to run on startup
elif [ ${BUT} -eq 2 ]; then
#	echo "just exiting"
  	exit
fi

# make sure internet is setup 
cat < /dev/null > /dev/tcp/www.inovato.com/80
res=$?
if [ $res -eq 1 ]; then
	INFO=$(yad --form --width=500 --text-align=center --center --title="Connect to internet" \
	--separator="|" --item-separator="|" \
	--text="\r\nMake sure the Quadra is <b>connected to the internet</b> before continuing.\r\n\r\nClick the <b>up/down arrow symbol</b> in the upper right of the screen.\r\n\r\nOnce connected, click 'Continue'" \
	--button="Exit":0 \
        --button="Continue":1 "" "" "")
	
	BUT=$(echo $?)

	if [ ${BUT} = 0 ]; then
		sync
  		exit
	fi
fi

INFO=$(yad --form --width=420 --text-align=center --bottom --title="Adjust Screen Resolution" \
--separator="|" --item-separator="|" \
--image="/usr/share/pixmaps/hamclock-borders-compare.png" \
--button="Yes":0 \
--button="No":1 "" "" "")

BUT=$(echo $?)

INFO=$(yad --form --width=500 --text-align=center --center --title="HamClock Setup" \
	--separator="|" --item-separator="|" \
	--text="\r\nAfter you click 'Done', HamClock will run and automatically on boot.\r\n\r\nWhen run for the first time, be sure to enter your callsign and location.\r\n\r\nTo exit full-screen mode, press ALT-F11 (Fn-Alt-F9 on mini-keyboards)" \
	--fixed \
	--button="Done":0 "" "" "")

mv ~/.config/autostart/hamclock-setup.desktop ~/.config/autostart/hamclock-setup.desktop.save

if [ ${BUT} = 1 ]; then
	cp /usr/local/share/applications/hamclock.desktop ~/.config/autostart
	sync
	hamclock
else
	cp /usr/local/share/applications/hamclock-no-borders.desktop ~/.config/autostart/hamclock.desktop
	sync
	hamclock-xrandr-wrapper
fi




