#!/bin/bash
#
# i18n - Internationalization - Internationalisierung
export TEXTDOMAIN=update-notifier-launcher
export TEXTDOMAINDIR="/usr/share/locale"

#
#add time for waiting until connect repositories
if [ `cat $HOME/.update-notifier-launcher/SETTINGS/settings.cfg | grep delay | cut -d ":" -f2` -gt "1" ]; then
DELAY=`cat $HOME/.update-notifier-launcher/SETTINGS//settings.cfg | grep delay | cut -d ":" -f2` 
else
DELAY=10
fi
echo $"$DELAY seconds wait to connect repositories"
if [ `cat $HOME/.update-notifier-launcher/SETTINGS/settings.cfg | grep pop-ups | cut -d ":" -f2` == "true" ]; then
notify-send -i info -t 8000 $"$DELAY seconds wait to connect repositories"
fi
sleep $DELAY
#

PID_DIR=$HOME/.update-notifier-launcher/PID
NUT_DIR=$HOME/.update-notifier-launcher/NUT # Next Update Time
CONF_DIR=$HOME/.update-notifier-launcher/SETTINGS


if [ -d $NUT_DIR ];then echo -e $"$NUT_DIR directory found\n\n"; 
else 
	if [ ! -d $HOME/.update-notifier-launcher ]; then mkdir $HOME/.update-notifier-launcher; fi 
	mkdir $NUT_DIR && echo $"Creating directory $NUT_DIR " && if [ -d $NUT_DIR ];then echo $"Done" || echo -e $"$NUT_DIR directory could not be created\n\n"; fi
fi 


function kill_existing() {
	rm -Rf $PID_DIR/$$ && echo -e $"\nRemoved this instance of update-notifier from the log (pid $$)"
	echo -e $"\n\nThe following notifier processes will be killed and removed from the log:"
	for i in `ls $PID_DIR `; do 
		echo -e -n "\t$i "
		kill -9 $i
		rm -f $PID_DIR/$i
		echo -e  "\n\n"
	done

	#for i in `ps -ef | grep "apt-get" | awk '{print $2}'`; do kill -9 $i; done 2>/dev/null
	for i in `ps -efa|grep update|grep yad|awk '{print $2}'`; do kill -9 $i; done
	echo -e $"\nAll pre-existing notifier processes have been killed and removed from the log\n\n"

	touch $PID_DIR/$$ && echo -e $"\nThis instance of update-notifier-launcher has been logged (pid $$)"
}

kill_existing
echo $"Starting update-notifier.."
update-notifier&
if [ "$POP_UPS" == "true" ]; then
	sleep 15 #Allow time for the nag screens to die before sending another one
fi

FIRST_LOOP="true"

while (true); do

	if [ `cat $CONF_DIR/settings.cfg | grep -c pop-ups ` == "1" ]; then POP_UPS=$(cat $CONF_DIR/settings.cfg | grep pop-ups | cut -d ":" -f2); fi
	[[ "$POP_UPS" == "true" ]] || [[ "$POP_UPS" == "false" ]] && echo "pop-ups=$POP_UPS"; export POP_UPS=$POP_UPS || export POP_UPS="true"
	if [ `cat $CONF_DIR/settings.cfg | grep -c hours ` == "1" ]; then HRS=$(cat $CONF_DIR/settings.cfg | grep hours | cut -d ":" -f2); fi
	if [ `cat $CONF_DIR/settings.cfg | grep -c days ` == "1" ]; then DAYS=$(cat $CONF_DIR/settings.cfg | grep days | cut -d ":" -f2); fi
	[[ "$HRS" == "" ]] && [[ "$DAYS" == "" ]] && HRS="1" #defualt to 1hrs if no time settings, recommend to set time
	if [ "$HRS" == "" ]; then HRS=; fi
	if [ "$DAYS" == "" ]; then DAYS=0; fi
	DAYS_TO_HRS=$(( 24 * $DAYS ))
	timer=$(( $HRS + $DAYS_TO_HRS ))
	COUNTER=$(( $timer * 3600 ))
	if [[ $timer -eq 0 ]]; then 
		echo -e $"\n\ntimer set to zero.. manual update enabled"
		echo $"<auto-check disabled>" > $NUT_DIR/NUT
		echo -e $"update-notifier-launcher is sleeping..\n\n"
		wait
	else
		echo "$timer"
		NEXT_UPDATE_TIME=$(date --date "now + $timer hours" +%A\ $"the"\ %d.\ %b\ $"at"\ %l:%M%p)
		echo $NEXT_UPDATE_TIME > $NUT_DIR/NUT
		COUNTDOWN=$((`date +%s` + $COUNTER))
		echo $COUNTDOWN > $CONF_DIR/counter 
		if [ "$FIRST_LOOP" == "false" ]; then
			if [ "$POP_UPS" == "true" ]; then
				notify-send -i info -t 8000 $"Next Update" $"The next scheduled check for updates is on $NEXT_UPDATE_TIME"
			fi
			echo -e $"Next update in $timer hours.. on $NEXT_UPDATE_TIME"
		fi
		#sleep "$timer"h
		sleep $COUNTER
		for i in `ls $PID_DIR `; do 
			if [ $i -ne $$ ]; then
				kill -9 $i
			fi
		done #2>/dev/null
		kill_existing
		update-notifier&
		if [ "$POP_UPS" == "true" ]; then
			sleep 15 #Allow time for the nag screens to die before sending another one
		fi
	fi
	FIRST_LOOP="false"
done
exit
