#!/bin/bash
#
# i18n - Internationalization - Internationalisierung
export TEXTDOMAIN=unl
export TEXTDOMAINDIR="/usr/share/locale"
# Variable
NUT=`cat $HOME/.update-notifier-launcher/NUT/NUT`
PID_DIR=$HOME/.update-notifier-launcher/PID
if [ ! -d $HOME/.update-notifier-launcher/SETTINGS ]; then
mkdir $HOME/.update-notifier-launcher/SETTINGS
echo "start:true
pop-ups:true
apt_force_yes:false
synaptic_main_window_hide:false
days:0
hours:8
delay:15" > $HOME/.update-notifier-launcher/SETTINGS/settings.cfg
fi
CONF_DIR=$HOME/.update-notifier-launcher/SETTINGS
counter=`cat $CONF_DIR/counter`
echo $"Counter is $counter"
#check of running update-notifier
QTSERVICE='qt-update-notifier'
SERVICE='update-notifier'
# Check for QT-Update-Notifier 
if ps -ef | grep -v grep | grep $QTSERVICE > /dev/null
then
	yad --button="gtk-ok:0" --title="Update-Notifier" --window-icon="/usr/share/icons/synaptic_notify.png" \
		--text $"<b>QT-</b>Update Notifier is already running.\n Exiting Update-Notifier!" --image="/usr/share/icons/synaptic_notify.png" --timeout 5
	exit 2
fi	
if ps -ef | grep -v grep | grep $SERVICE > /dev/null
then
	echo $"The update-notifier is already running. Exiting..."
	yad --button="gtk-ok:0" --title="Update-Notifier" --window-icon="/usr/share/icons/synaptic_notify.png" \
		--text $"Update Notifier is already running." --image="/usr/share/icons/synaptic_notify.png" --timeout 5
	exit 1
else
	if [ `cat $CONF_DIR/settings.cfg | grep start | cut -d ":" -f2` == "true" ]; then
		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:"
		update-notifier-launcher
		exit 4
	fi	

	if [ `cat $CONF_DIR/sleep` == `ps -efa | grep sleep -m 1 | cut -d ":" -f4 | cut -d " " -f3` ]; then
		yad --button="gtk-ok:0" --title="Update-Notifier" --window-icon="/usr/share/icons/synaptic_notify.png" \
		--text $"The next scheduled check for updates is $NUT" --image="/usr/share/icons/synaptic_notify.png" --timeout 10
		exit 3
	fi	
	if [ `date +%s` -lt $counter ]; then
		echo $"The next scheduled check for updates is $NUT"
		seconds=`date +%s`
		countdown=$(( $counter - $seconds ))
		echo $"Waiting $countdown seconds to check for updates."
		yad --button="gtk-ok:0" --title="Update-Notifier" --window-icon="/usr/share/icons/synaptic_notify.png" \
		--text $"The next scheduled check for updates is $NUT" --image="/usr/share/icons/synaptic_notify.png" --timeout 10
		echo $countdown > $CONF_DIR/sleep
		sleep $countdown
		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:"
		update-notifier-launcher
	fi
	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:"	
	update-notifier-launcher
fi
exit 0
