#!/bin/bash
# Post installation script

TIME_OUT=25

# makes sure that there is a connection, especially for kernel install wifi restarts
connection-test()
{
	local repeat='' connection='' options='' opt=''
	local UserAgent='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1'

	echo "${S}Testing internet connection....${N}"
	# ping is a problem when there is no connection, takes too long to time out
	#ping -c 1 -W 2 google.com &>/dev/null && connection=1 || connection=0
	wget -T 7 -t 1 -q --spider http://www.google.com/ && connection=1 || connection=0

	# some US ISPs block ping, so do backup tests if that's the case
	if [ "$connection" -eq 0 ];then
		echo "${E}test 1 failed, trying test 2....${N}"
		wget -T 12 -t 1 -q --spider http://www.google.com/ && connection=1 || connection=0
		if [ "$connection" -eq 0 ];then
			echo "${E}test 2 failed, trying test 3....${N}"
			wget -T $TIME_OUT -t 1 -q --spider http://www.ebay.com/ && connection=1 || connection=0
		fi
	fi

	if [ "$connection" -eq 0 ];then
		# restart connection just in case
		if [ "$B_CONNECTION_RETRY" != 'true' ];then
			restart_networking
			repeat='true'
		else
			repeat=''
			echo "${W}No connection to the internet seems to be available.${N}"
			exit 1
		fi
	else
		echo "${S}Internet connection is present and working.${N}"
	fi
	
	if [ "$repeat" == 'true' ];then
		connection-test
	fi
}

restart_networking()
{
	local RestartNetworking='/etc/init.d/networking restart' response=''

	echo $WLINE
	echo "${W}Connection failed, sorry."
}

createFlag() {
	if [ ! -d "/home/$USER/.config/diy" ]; then
		mkdir /home/$USER/.config/diy
	fi
	if [ ! -f "/home/$USER/.config/diy/diy-welcome" ]; then
		touch /home/$USER/.config/diy/diy-welcome
	fi
}

runInTerminal() {
    x-terminal-emulator -e 'diy-welcome'
    exit 0
}

# First run
if [[ $1 = '--firstrun' ]]; then

#    if [[ -d /live/overlay || -e /home/$USER/.config/diy/diy-welcome || ! $(groups) =~ ( |^)sudo( |$) ]]; then
    if [[ -d /lib/live/mount/overlay || -e /home/$USER/.config/diy/diy-welcome || ! $(groups) =~ ( |^)sudo( |$) ]]; then
        exit 1
    fi
    runInTerminal
fi

# Open in terminal
if [[ $1 = '--open' ]]; then
    runInTerminal
fi

createFlag

if ! [[ $(groups) =~ ( |^)sudo( |$) ]]; then
  echo 'Error: must be a member of the sudo group to run this script.' # TODO 'root' user is not a member of the sudo group. Is it meant to restrict root from executing this script?
  exit 1
fi

LIBDIR='/usr/lib/lib-diy-welcome'

# Import prompt
if [[ -f ${LIBDIR}/prompt ]]; then
	source "${LIBDIR}/prompt"
else
	echo 'Error: Failed to locate prompt'
	exit 1
fi

# Run through steps
STEPS_BASIC=('apt-update' 'apt-dist-upgrade' 'install-printer-packages' 'install-java-packages' 'install-gimp' 'install-libreoffice' 'devel')
STEPS_DEVEL=('devel-install-version-control-tools' 'devel-install-ssh-server' 'devel-install-lamp-stack' 'devel-install-packaging-tools')
STEP=1
(( STEPS = ${#STEPS_BASIC[@]} + ${#STEPS_DEVEL[@]} + 2 )) # 2 is intro and fini

. "$LIBDIR/intro"
connection-test

for curStep in "${STEPS_BASIC[@]}"; do
    ((STEP++))
    . "$LIBDIR/$curStep"
done

if [[ $DEVEL ]]; then
    for curStep in "${STEPS_DEVEL[@]}"; do
        ((STEP++))
        . "$LIBDIR/$curStep"
    done
fi

. "$LIBDIR/fini"

exit 0
