#!/bin/bash
#############################################################
# Copyright 2009, PCLinuxOS
# Released under GPL licence (version 2 or later)
# Author: maik3531 (maik3531@gmail.com) with the support of the Linux community 
# (Special thanks to: travisn000, Neal and all the translators)
# PClinuxOS update-notifier
# Version: 1.1 (2009-05-03)
# Version: 1.5.7 (2018-04-01) developed by Daniel Meiss-Wilhelm
# 			aka Leiche
#############################################################
#
# to create a mo-file for his script have a look at:
# http://wiki.ubuntuusers.de/Skripte/Bash_Lokalisierung (german)
# http://wooledge.org:8000/BashFAQ/098
#
# - In order to use crontab service -
# newline="00 */2 * * * /usr/bin/update-notifier"
# (crontab -l; echo "$newline") | crontab -
# ln -s /usr/bin/update-notifier /etc/cron.hourly/update-notifier


VERSION="1.6.7"
RELEASE="2009-2021"

# verbose output for debugging
if [ "$1" == "--debug" ]; then set -xv; fi

# Check script requirements;
yad=$(which yad 2>/dev/null)
[ -z "$yad" ] && { echo $"Can't find yad, exiting"; exit 1; }

notify_send=$(which notify-send 2>/dev/null)
[ -z "$notify_send" ] && { echo $"Can't find notify-send, exiting"; exit 1; }

xterm=$(which xterm 2>/dev/null)
[ -z "$xterm" ] && { echo $"Can't find xterm, exiting"; exit 1; }

gtkdialog=$(which gtkdialog 2>/dev/null)
[ -z "$gtkdialog" ] && { echo $"Can't find gtkdialog, exiting"; exit 1; }

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

# root privilegs - rootrechte
if [ -f /usr/bin/gksu ]; then
   GSU="/usr/bin/gksu -l"
else
   GSU="/usr/bin/kdesu -c"
fi

LOGO=/usr/share/icons/synaptic_notify.png
LOGO0=/usr/share/pixmaps/synaptic.png
LOGO1=/usr/share/icons/synaptic_ok.png
LOGO2=/usr/share/icons/timemanagement_section.png
LOGO3=/usr/share/icons/networking_configuration_section.png
LOGO4=/usr/share/icons/synaptic_unknown.png
LOGO5=/usr/share/apps/update-notifier/u_n_plos.png

NOTIFY_TITLE=$"Update-Notifier"
TMP_DIR=$HOME/.update-notifier
CONF_DIR=$HOME/.update-notifier-launcher/SETTINGS
PID_DIR=$HOME/.update-notifier-launcher/PID
NUT_DIR=$HOME/.update-notifier-launcher/NUT
UPDATE_REFRESH="true"
CONECTION_FAILED=`cat $TMP_DIR/inet.log`



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

touch $PID_DIR/$$ && echo $"pid $$ has been logged for this instance of update-notifier"


function read_settings(){
	########### Read popup notification settings #####################
	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 [ "$POP_UPS" == "false" ]; then notify_send="echo"; fi

	############ Read synaptic settings ##############################
	if [ `cat $CONF_DIR/settings.cfg | grep -c synaptic_main_window_hide ` == "1" ]; then 
		SYNAPTIC_HIDE_MAIN=$(cat $CONF_DIR/settings.cfg | grep synaptic_main_window_hide | cut -d ":" -f2 ); fi
	if [ "$SYNAPTIC_HIDE_MAIN" == "true" ]; then
		SYNAPTIC_UPDATE="synaptic --hide-main-window --non-interactive -o Synaptic::AskRelated=true --update-at-startup --dist-upgrade-mode"
	else
		SYNAPTIC_UPDATE="synaptic --update-at-startup --dist-upgrade-mode"
	fi

	############ Read Next Update Time (NUT) settings ################
	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" == null ]] && [[ "$DAYS" == null ]] && HRS="8" #defualt to 8hrs if no time settings
	if [ "$HRS" == "" ]; then HRS=-0; fi
	if [ "$DAYS" == "" ]; then DAYS=0; fi
	DAYS_TO_HRS=$(( 24 * $DAYS ))
	timer=$(( $HRS+$DAYS_TO_HRS ))
	echo $timer
	if [[ $timer -eq 0 ]]; then 
		echo -e $"\n\ntimer set to zero.. manual update enabled"
		echo $"DISABLED" > $NUT_DIR/NUT
		NUT=$"DISABLED"
		UPDATE_REFRESH="false"
		if [ "$COUNT" == "" ]; then  # COUNT not defined
			NOTIFY_MSG=$"Automatic update checks are disabled, click icon to change settings or initiate check for updates."
			LOGO_X=$LOGO4 # unknown logo
		elif [ $COUNT -gt 0 ]; then
			LOGO_X=$LOGO # notify logo
			NOTIFY_MSG=$"$COUNT updates and $COUNT_NEW new packages available as of $COUNT_TIME."
		elif [ $COUNT -eq -100 ];then
			LOGO_X=$LOGO
			NOTIFY_MSG=$"
There are updates available; 
use synaptic to upgrade your system.
"
			echo -e "$NOTIFY_MSG\n\n"
		elif [ $COUNT -eq -10 ];then
			LOGO_X=$LOGO
			NOTIFY_MSG=$"An error occured verifying upgrade; please use synaptic or try again later. "
			echo -e "$NOTIFY_MSG\n\n"
		elif [ $COUNT -lt 0 ];then
			LOGO_X=$LOGO
			NOTIFY_MSG=$"An error occured verifying upgrade; please use synaptic or try again later. "
			echo -e "$NOTIFY_MSG\n\n"
		else
			NOTIFY_MSG=$"Automatic update checks are disabled, click icon to change settings or initiate check for updates."
			LOGO_X=$LOGO4 # unknown logo
		fi
	else
		NUT=$(cat $NUT_DIR/NUT) && echo $"Next update time is $NUT"
		UPDATE_REFRESH="true"
		LOGO1=/usr/share/icons/synaptic_ok.png
	fi
echo "update refresh status=$UPDATE_REFRESH"
}



function configure_settings() {
# read current settings 
if [ `cat $CONF_DIR/settings.cfg | grep start | cut -d ":" -f2` == "true" ]; then
	FORCE_ICON_STATUS=true
	else
	FORCE_ICON_STATUS=false
fi	
if [ `cat $CONF_DIR/settings.cfg | grep acces | cut -d ":" -f2` == "true" ]; then
	PKEXEC_STATUS=true
	else
	PKEXEC_STATUS=false
fi
if [ `cat $CONF_DIR/settings.cfg | grep pop-ups | cut -d ":" -f2` == "true" ]; then
	POP_UPS_STATUS=true
	else
	POP_UPS_STATUS=false
fi
if [ `cat $CONF_DIR/settings.cfg | grep synaptic_main_window_hide | cut -d ":" -f2` == "true" ]; then
	SYNAPTIC_HIDE_MAIN_STATUS=true
	else
	SYNAPTIC_HIDE_MAIN_STATUS=false
fi
if [ `cat $CONF_DIR/settings.cfg | grep days | cut -d ":" -f2` == "0" ]; then
	DAYS_STATUS=0
	else
	DAYS_STATUS=`cat $CONF_DIR/settings.cfg | grep days | cut -d ":" -f2`
fi
if [ `cat $CONF_DIR/settings.cfg | grep hours | cut -d ":" -f2` == "0" ]; then
	HOURS_STATUS=0
	else
	HOURS_STATUS=`cat $CONF_DIR/settings.cfg | grep hours | cut -d ":" -f2`
fi
if [ `cat $CONF_DIR/settings.cfg | grep delay | cut -d ":" -f2` == "0" ]; then
	DELAY_STATUS=10
	else
	DELAY_STATUS=`cat $CONF_DIR/settings.cfg | grep delay | cut -d ":" -f2`
fi
#
export MAIN_DIALOG='
<window title=" - Update-Notifier Settings - " resizable="false" icon-name="package_settings">
<vbox spacing="25">
	<frame '$" Display Icon always in notification area. "'>
		<checkbox active="'$FORCE_ICON_STATUS'">
		<label>"'$"Enable tray icon"'"</label>
		<variable>FORCE_ICON</variable>
		</checkbox>
	</frame>
	<frame '$" Standard application to use synaptic. "'>
		<checkbox active="'$PKEXEC_STATUS'">
		<label>"'$"Use pkexec as default to open synaptic."'"</label>
		<variable>PKEXEC</variable>
		</checkbox>
	</frame>
	<frame '$" Start delay "'>
	<hbox>
	 <text>
	  <label> "'$"Choose time to wait until connecting to repositories: "'"</label>
	 </text>
		<comboboxentry width-request="60" case-sensitive="false" value-in-list="false" active="0">
			<variable>DELAY</variable>
			<item>'$DELAY_STATUS'</item>
			<item>10</item>
			<item>15</item>
			<item>20</item>
			<item>25</item>
			<item>30</item>
			<item>35</item>
			<item>40</item>
			<item>45</item>
			<item>50</item>
			<item>55</item>
			<item>60</item>
		</comboboxentry>
	</hbox>
	</frame>
	<frame '$" Notifications "'>
		<checkbox active="'$POP_UPS_STATUS'">
		<label>"'$"Enable pop-up notifications"'"</label>
		<variable>POP_UPS</variable>
		</checkbox>
	</frame>
	<frame '$" Synaptic Update Settings "'>
		<checkbox active="'$SYNAPTIC_HIDE_MAIN_STATUS'">
		<label>"'$"Hide main window when updating with Synaptic "'"</label>
		<variable>SYNAPTIC_HIDE_MAIN</variable>
		</checkbox>
	</frame>
	<frame '$" Frequency of Update Checks "'>
		<hbox>
			<pixmap>
				<input file>"'$LOGO4'"</input>
			</pixmap>
			<text use-markup="true">
				<label>"'$"<b>Note:</b>  Time interval is calculated as a sum of days and hours. 

If you want update checks on an hourly basis, select zero (0) days. 
If you only want update checks to run when manually initiated set both hours and days to zero (0). "'"</label>
			</text>
		</hbox>
		<hbox>
			<text>
				<label>"'$"Days: "'"</label>
			</text>
			<comboboxentry active="0">
			<variable>CHK_DAYS</variable>
			<item>'$DAYS_STATUS'</item>
			<item>0</item>
			<item>1</item>
			<item>2</item>
			<item>3</item>
			<item>4</item>
			<item>5</item>
			<item>10</item>
			<item>15</item>
			<item>30</item>
			<item>40</item>
			</comboboxentry>
		</hbox>
		<hbox>
			<text>
				<label>"'$"Hours: "'"</label>
			</text>
			<comboboxentry active="0">
			<variable>CHK_HOURS</variable>
			<item>'$HOURS_STATUS'</item>
			<item>0</item>
			<item>4</item>
			<item>6</item>
			<item>8</item>
			<item>12</item>
			<item>15</item>
			<item>18</item>
			</comboboxentry>
		</hbox>
	</frame>
	<hbox start="true">
		<button>
			<label>"'$" Save "'"</label>
			<input file icon="document-save"></input>
			<action type="exit">save</action>
		</button>
		<button>
			<label>"'$" Cancel "'"</label>
			<input file icon="gtk-cancel"></input>
			<action type="exit">cancel</action>
		</button>
	</hbox>
</vbox>
</window>
'

I=$IFS; IFS=""
for STATEMENT in  $(gtkdialog --center --program=MAIN_DIALOG); do
	eval $STATEMENT
done
IFS=$I
	
	if [ -d $CONF_DIR ];then echo -e $"$CONF_DIR directory found\n\n"; 
	else 
		if [ ! -d $HOME/.update-notifier-launcher ]; then mkdir $HOME/.update-notifier-launcher; fi 
		mkdir $CONF_DIR && echo $"Creating directory $CONF_DIR " && if [ -d $CONF_DIR ];then echo $"Done" || echo -e $"$CONF_DIR directory could not be created\n\n"; fi
	fi
	
	
		# Process variables if dialog exit status == save
	if [ "$EXIT" == "save" ];then
		echo pop-ups:"$POP_UPS" > $CONF_DIR/settings.cfg
		echo synaptic_main_window_hide:"$SYNAPTIC_HIDE_MAIN" >> $CONF_DIR/settings.cfg
		echo days:"$CHK_DAYS" >> $CONF_DIR/settings.cfg
		echo hours:"$CHK_HOURS" >> $CONF_DIR/settings.cfg
		echo delay:"$DELAY" >> $CONF_DIR/settings.cfg
		echo start:"$FORCE_ICON" >> $CONF_DIR/settings.cfg
		echo acces:"$PKEXEC" >> $CONF_DIR/settings.cfg
		[[ "$HRS" == "" ]] && [[ "$DAYS" == "" ]] && HRS="8" #defualt to 8hrs if no time settings
		if [ "$HRS" == "" ]; then HRS=0; fi
		if [ "$DAYS" == "" ]; then DAYS=0; fi
		DAYS_TO_HRS=$(($DAYS*24))
		timer=$(( $HRS+$DAYS_TO_HRS ))
# Changing NUT from update notifier does not work, as launcher will not see the change of time until
# it's next iteration of the launcher's while loop.. (at the next scheduled update check)
# at that time the launcher will update NUT with the new update check interval automaticaly
		NUT=$(date --date "now + $timer hours" +%A\ $"the"\ %d.\ %b\ $"at"\ %l:%M%p)
		echo $NUT > $NUT_DIR/NUT
	
	else	#do nothing
		echo "Settings canceled"
	fi
	read_settings
	yad --button="gtk-ok:0" --window-icon=$LOGO --width=400 --height=150 --title="Update-Notifier" --timeout 15 --text=$"Changes to update interval will be applied after the next currently scheduled update check.\n\nExit and restart update-notifier to apply update interval now."
}

function choose() {
	UPDATE_REFRESH="false"
	if [[ $COUNT -gt 0 ]]; then
		RET=$(yad --button="gtk-ok:0" --button=$"Hide:1" --list --window-icon=$LOGO0 --width=500 --height=400 --title=$"Update-Notifier-$VERSION" --image=$LOGO5 \
		--text $"\n$COUNT new updates and $COUNT_NEW new packages are available for your system. \n(as of update check performed $COUNT_TIME)\n\nPlease select an option below:  " --column="" --hide-column 2 \
			$"Install updates using Synaptic"\
			$"Show package lists"\
 			$"Reload updates"\
			$"Change settings"\
			$"Read Software Announcements"\
			$"About update-notifier"\
			$"Exit update-notifier")
	elif [[ $COUNT_NEW -gt 0 ]]; then
		RET=$(yad --button="gtk-ok:0" --button=$"Hide:1" --list --window-icon=$LOGO0 --width=500 --height=375 --title=$"Update-Notifier-$VERSION" --image=$LOGO5 \
		--text $"\n$COUNT new updates and $COUNT_NEW new packages are available for your system. \n(as of update check performed $COUNT_TIME)\n\nPlease select an option below:  " --column="" --hide-column 2 \
 			$"Check for updates"\
			$"Show package lists"\
			$"Open Synaptic package manager"\
			$"Change settings"\
			$"Read Software Announcements"\
			$"About update-notifier"\
			$"Exit update-notifier")
	elif [[ $COUNT == "" ]]; then
		RET=$(yad --button="gtk-ok:0" --button=$"Hide:1" --list --window-icon=$LOGO0 --width=500 --height=325 --title=$"Update-Notifier-$VERSION" --image=$LOGO5 \
		--text $"\nUpdate check has not yet been performed.\n\nPlease select an option below:  " --column="" --hide-column 2 \
 			$"Check for updates"\
			$"Open Synaptic package manager"\
			$"Change settings"\
			$"Read Software Announcements"\
			$"About update-notifier"\
			$"Exit update-notifier")
	elif [[ $COUNT -eq 0 ]]; then
		RET=$(yad --button="gtk-ok:0" --button=$"Hide:1" --list --window-icon=$LOGO0 --width=500 --height=350 --title=$"Update-Notifier-$VERSION" --image=$LOGO5 \
		--text $"\n$COUNT new updates and $COUNT_NEW new packages are available for your system. \n(as of update check performed $COUNT_TIME)\n\nPlease select an option below:  " --column="" --hide-column 2 \
 			$"Check for updates"\
			$"Open Synaptic package manager"\
			$"Change settings"\
			$"Read Software Announcements"\
			$"About update-notifier"\
			$"Exit update-notifier")
	elif [ $COUNT -eq -100 ];then
		RET=$(yad --button="gtk-ok:0" --button=$"Hide:1" --list --window-icon=$LOGO0 --width=500 --height=330 --title=$"Update-Notifier-$VERSION" ---image=$LOGO5 \
		--text $"\nThere are updates available; \nuse synaptic to upgrade your system.  " \
		--column="" --hide-column 2 \
			$"Update System"\
			$"See log file for more information"\
			$"Check for updates"\
			$"Change settings"\
			$"Read Software Announcements"\
			$"About update-notifier"\
			$"Exit update-notifier")
	else  # COUNT is less than zero; error occured trying to update package lists
		RET=$(yad --button="gtk-ok:0" --button=$"Hide:1" --list --window-icon=$LOGO0 --width=500 --height=300 --title=$"Update-Notifier-$VERSION" --image=$LOGO5 \
		--text $"An error occured retrieving package lists\n\nPlease select an option below:  " --column="" --hide-column 2 \
			$"Open Synaptic package manager"\
			$"Change settings"\
			$"Read Software Announcements"\
			$"About update-notifier"\
			$"Exit update-notifier")
	fi
	RET_EXIT_STATUS="$?"
	
	if echo "$RET" | grep $"Update System"; then
		if [ `file /usr/bin/synaptic | grep consolehelper | wc -l` -eq 1 ];then
		$SYNAPTIC_UPDATE
		elif [ `cat $CONF_DIR/settings.cfg | grep acces | cut -d ":" -f2` == "true" ]; then
		$SYNAPTIC_UPDATE
		else
		$GSU "$SYNAPTIC_UPDATE"
		fi
	  UPDATE_REFRESH="true"	
	fi	
	
	if echo "$RET" | grep $"Open Synaptic package manager"; then
		if [ `file /usr/bin/synaptic | grep consolehelper | wc -l` -eq 1 ];then
		synaptic
		elif [ `cat $CONF_DIR/settings.cfg | grep acces | cut -d ":" -f2` == "true" ]; then
		synaptic
		else
		$GSU synaptic
		fi
	  UPDATE_REFRESH="false"	
	fi

	if echo "$RET" | grep $"Install updates using Synaptic"; then
		if [ `file /usr/bin/synaptic | grep consolehelper | wc -l` -eq 1 ];then
		$SYNAPTIC_UPDATE
		elif [ `cat $CONF_DIR/settings.cfg | grep acces | cut -d ":" -f2` == "true" ]; then
		$SYNAPTIC_UPDATE
		else
		$GSU "$SYNAPTIC_UPDATE"
		fi
      UPDATE_REFRESH="true"
    fi

	if echo "$RET" | grep $"Show package lists"; then
		if [ $COUNT -gt 0 ]; then
			yad --text-info --window-icon=$LOGO0 --width=400 --height=300\
			 --title=$"Package update list" --filename="$TMP_DIR/apt/package_update_list"
		fi
		if [ $COUNT_NEW -gt 0 ]; then
			yad --text-info --window-icon=$LOGO0 --width=400 --height=300\
			 --title=$"New package list" --filename="$TMP_DIR/apt/new-pkg-list"
		fi
		choose
	fi
	
	if echo "$RET" | grep $"See log file for more information"; then
		log_file
	fi
	
	if echo "$RET" | grep $"Read Software Announcements"; then
		xdg-open http://www.pclinuxos.com/forum/index.php/board,66.0.html
	fi

	if echo "$RET" | grep $"Change settings"; then
		configure_settings
		choose
	fi

	if echo "$RET" | grep $"About update-notifier"; then
		yad --button="gtk-ok:0" --image=$LOGO5 --window-icon=$LOGO0 \
		--title=$"About Update-Notifier " --timeout 30 --center --text=\
$"\n     <b>- Update-Notifier -</b>\n\t<b>Version:</b> $VERSION\n\t<b>Release:</b> $RELEASE\ \n\n\
<b>Original Author:</b> maik3531         \n\n\
<b>Updated by:</b> Daniel Meiss-Wilhelm (leiche)
\t\t\t rogaven
\t\t\t travisn000\n
<b>Translators are:</b> kikim (Polish)
\t\t\t\t turzin (French)
\nThe next check for updates\n \
<b>is $NUT</b> \n\
\n<b>Kernel Status:</b> $NOTIFY_KERN
<a href='http://www.pclosmag.com/html/Issues/200907/page09.html'>More about Update-Notifier</a>"
		choose
	fi

	if echo "$RET" | grep $"Reload updates"; then 
		UPDATE_REFRESH="true"
	fi

	if echo "$RET" | grep $"Check for updates"; then 
		UPDATE_REFRESH="true"
	fi

	if echo "$RET" | grep $"Exit update-notifier"; then
		exit_kill
	fi
}

function check_updates() {
	read_settings
	if [ -d $TMP_DIR ];then
		echo -e $"\n\n$TMP_DIR exists.. removing"
		rm -fR $TMP_DIR
		if [ -d $TMP_DIR ];then 
			echo -e $"ERROR: $TMP_DIR still exists.."
		else
			echo -e $"$TMP_DIR removed" 
		fi
	fi
	mkdir $TMP_DIR
	if [ -d $TMP_DIR ];then echo -e $"New $TMP_DIR directory created\n\n"; fi 
	echo -e $"Updating package sources.."
	if [ "$POP_UPS" == "true" ]; then notify-send -i $LOGO2 -t 12000 "$NOTIFY_TITLE" $"Please wait, checking for new updates"; fi

	if [ ! -d $TMP_DIR/apt/ ]; then mkdir $TMP_DIR/apt/; fi
	if [ ! -d $TMP_DIR/apt/lists ]; then mkdir $TMP_DIR/apt/lists/; fi
	if [ ! -d $TMP_DIR/apt/lists/partial ]; then mkdir $TMP_DIR/apt/lists/partial; fi

	if [ -f $CONF_DIR/all-pkg-list ]; then 
		mv -f $CONF_DIR/all-pkg-list $CONF_DIR/all-pkg-list.old; 
	else
		echo -e $"\n\nMaking all-pkg-list.old"
		apt-cache -o Debug::NoLocking=true -o dir::state=$TMP_DIR/apt pkgnames >$CONF_DIR/all-pkg-list.old
	fi

	apt-get -s -o Debug::NoLocking=true -o dir::state=$TMP_DIR/apt update
	apt_status=$?
	if [ $apt_status -ne 0 ]; then 
		echo; echo $"An error occured with apt-get update"
		COUNT=-1
	else
		# check number of packages to be updated
		# NoLocking=true allows non-root user to run  ..maybe not?
		# state=.. changes working directory to avoid permissions errors
		apt-get -s -o Debug::NoLocking=true -o dir::state=$TMP_DIR/apt dist-upgrade >> $TMP_DIR/apt/apt-get.log
		apt_status=$?
		if [ $apt_status -eq 100 ]; then 
			echo; echo $"
There are updates available; 
use synaptic to upgrade your system.
"
echo; echo
			COUNT=-100
			COUNT_NEW=-1
		elif [ $apt_status -ne 0 ]; then 
			echo; echo; echo $"An error occured verifying upgrade; please use synaptic or try again later. "
			COUNT=-10
			COUNT_NEW=-1
		else 
			COUNT_TIME=$(date --date "now + 0 hours" +%A\ %b\ %d\ $"at"\ %-l:%M%p )

			COUNT=`cat $TMP_DIR/apt/apt-get.log | grep -c "Inst"`

			# get names of packages to be updated
			if [ -f $TMP_DIR/apt/package_update_list ]; then rm -Rf $TMP_DIR/apt/package_update_lis*;fi
			PACKAGES=`cat $TMP_DIR/apt/apt-get.log | awk '/Inst/ {print $2}' | sort`
			if [ "$PACKAGES" ];then
				echo $PACKAGES  | tr " " "\n" >> $TMP_DIR/apt/package_update_list
			fi

			echo -e $"\n\nMaking all-pkg-list" 
			apt-cache -o Debug::NoLocking=true -o dir::state=$TMP_DIR/apt pkgnames >$CONF_DIR/all-pkg-list
			[[ -f $CONF_DIR/all-pkg-list.old ]] && [[ -f $CONF_DIR/all-pkg-list ]] && echo -e $"\n\nMaking new-pkg-list\n\n" && diff $CONF_DIR/all-pkg-list.old $CONF_DIR/all-pkg-list | grep "^>" | cut -d" " -f2 | sort >$TMP_DIR/apt/new-pkg-list

			COUNT_NEW=`cat $TMP_DIR/apt/new-pkg-list | grep -c "[0-9,a-Z]."`
		fi
	fi

	if [ $COUNT_NEW -gt 0 ];then COUNT_VALID=$(( $COUNT * $COUNT_NEW )); else COUNT_VALID=$COUNT; fi
	if [ $COUNT_VALID -gt 0 ];then
		LOGO_X=$LOGO # notify logo
		NOTIFY_MSG=$"$COUNT updates available; $COUNT_NEW new packages available."
		echo -e "$NOTIFY_MSG\n\n"
	elif [ $COUNT_VALID -eq -100 ];then
		LOGO_X=$LOGO
		NOTIFY_MSG=$"
There are updates available; 
use synaptic to upgrade your system.
"
		echo -e "$NOTIFY_MSG\n\n"
	elif [ $COUNT_VALID -eq -10 ];then
		LOGO_X=$LOGO
		NOTIFY_MSG=$"An error occured verifying upgrade; please use synaptic or try again later. "
		echo -e "$NOTIFY_MSG\n\n"
	elif [ $COUNT_VALID -lt 0 ];then
		LOGO_X=$LOGO
		NOTIFY_MSG=$"An error occured verifying upgrade; please use synaptic or try again later. "
		echo -e "$NOTIFY_MSG\n\n"
	else
		LOGO_X=$LOGO1 # ok logo
		NOTIFY_MSG=$"$COUNT updates available; $COUNT_NEW new packages available."
		echo -e "$NOTIFY_MSG\n\n"
	fi
	OLDKERNEL=$(uname -r | cut -d "-" -f1 | cut -d "." -f1-3)
	KERNEL=$(apt-cache search kernel-`uname -r` | grep -m "1" -s "kernel" | cut -d "-" -f2)
	if [ $KERNEL == $OLDKERNEL ]; then 
		echo no kernel update ; 
		NOTIFY_KERN=$"Your Kernel Version is $OLDKERNEL."
	else 
		echo kernel update available ;
		NOTIFY_KERN=$"There is a update for your Kernel"
	fi
}



function notify_net_connect_error () {
	echo -e $"\n\nERROR: Unable to connect to internet\n\n"
	if [ "$POP_UPS" == "true" ]; then notify-send -i $LOGO3 -t 4000 $"Connection Failed" $"Your internet connection does not appear to be working.  Please configure it and reload update-notifier"; fi

	yad --width=400 --title "Update-Notifier" --text=$"Would you like to configure your internet connection and check for updates?" --image=$LOGO0 --button="gtk-ok:0" --button="gtk-close:1" 
	if [ $? == 0 ]; then 
		/usr/bin/draknetcenter
		check_connected
	fi 

	LOGO_X=$LOGO3
	NOTIFY_MSG=$"Your internet connection does not appear to be working.  Please configure it and reload update-notifier"
}


function notify_repo_connect_error () {
	echo -e $"\n\nInternet connection working, but failed to contact software repository.  Please select a different repository.\n\n" && rm -Rf $TMP_DIR/index.htm*

	if [ "$POP_UPS" == "true" ]; then notify-send -i $LOGO3 -t 8000 $"Unable to contact repository" $"Your internet connection is working, but your default software repository cannot be contacted.  Please select a different repository using Synaptic."; fi

	yad --width=400 --title "Update-Notifier" --window-icon=$LOGO4 --text=$"Your internet connection is working, but your default software repository cannot be contacted.\n\nWould you like to open Synaptic and configure a different software repository now?\n\nSelecting Cancel will close Update Notifier. \n\n<b>$CONECTION_FAILED</b>" --image=$LOGO0 --button="gtk-ok:0" --button="gtk-close:1"
	if [ $? == 0 ]; then 
		synaptic -r	#launch synaptic with repository selection dialog open
		check_connected
	else
		exit_kill
	fi 
}



function check_connected() {

# check for working internet connection / connection to software repository
# =================================================

active_repo=$( awk '$1 ~ /rpm/ { print $2; exit }' /etc/apt/sources.list ) # URL of first line starting with 'rpm'
wget_arepo_file="${active_repo}pclinuxos/2007/base/release.main"
arepo_domain=$( awk '$1 ~ /rpm/ { print $2; exit }' /etc/apt/sources.list | cut -d/ -f3 | cut -d"@" -f2 | cut --complement -d":" -f2)
echo "Active repo: $arepo_domain"

ping -c 1 $arepo_domain
if [ $? -ne 0 ];then 	#ping to repo failed, try wget

	if [ -f $TMP_DIR/release.main ]; then rm -f $TMP_DIR/release.main; fi
	HAS_PASS=`echo $active_repo |grep @`
	if [ -n $HAS_PASS ];then
		( wget $arepo_domain -o $TMP_DIR/inet.log ) 2>&1
		if [ -n "`cat $TMP_DIR/inet.log |grep "403: Forbidden"`" ];then touch $TMP_DIR/release.main; fi
	else
		( wget --directory-prefix=$TMP_DIR $wget_arepo_file -o inet.log ) 2>&1
	fi
	
	if [ -f $TMP_DIR/release.main ]; then #wget to repo successful
		echo -e $"\n\nConnection to repository server is successful\n\n" && rm -f $TMP_DIR/release.main
		check_updates
	else		#wget to repo failed, try connecting to google
		echo -e $"\n\nNo connection to repository; trying google..\n\n"
		ping -c 1 64.233.161.99
		if [ $? -ne 0 ];then 	#ping to google failed, try wget to google
			if [ -d $TMP_DIR/index.html ]; then rm -Rf $TMP_DIR/index.htm*; fi
			( wget --directory-prefix=$TMP_DIR http://www.google.com/index.html -o inet.log ) 2>&1
			if [ -f $TMP_DIR/index.html ]; then 
				# no connection to repo, wget to google successful
				notify_repo_connect_error
			else	
				#wget to google failed.. no internet connection
				notify_net_connect_error
			fi
		else	
			#ping to google successful, but not wget to repo
			notify_repo_connect_error
		fi
	fi
else			#ping to repository server successful
	echo -e $"\n\nConnection to repository server is successful\n\n"
	check_updates
fi
}

function exit_kill() {
	echo -e $"\n\nStarting exit processes from PID $$.."
	echo -e $"\n\nThe following processes exist in the log:"
	for i in `ls $PID_DIR `; do echo -e "\t$i"; done
	rm -Rf $PID_DIR/$$ && echo -e $"\nRemoved this instance of update-notifier from the log (pid $$)"
	for i in `pgrep -x update-notifier`;  do echo -n $"Killing yad pid $i: "; kill -9 $i; done
	for i in `ls $PID_DIR `; do echo -n $"Killing logged pid $i: "; kill -9 $i; done #2>/dev/null #should kill all instances of launcher & notifier exept this one
	echo -e $"\nExiting.."
	exit
}

# open log file for more informations
#================================================================
function log_file () {
	LOGFILE='
<window window_position="1" title="Update-Notifier_'$VERSION'" icon-name="gtk-help">
	<vbox>
		<frame '$"Log file"'>
			<vbox scrollable="true" width="600" height="300">
				<text wrap="false" xalign="0">
				<input file>'$TMP_DIR'/apt/apt-get.log</input></text>
			</vbox>
		</frame>
		<hbox>
			<button>
				<label>'$"Exit"'</label>
				<input file icon="finish"></input>
				<action type="exit">LOGFILE</action>
			</button>
		</hbox>
	</vbox>
</window>
'
#
export GUI="`echo "$LOGFILE" | sed -e 's/##.*//'`" 
gtkdialog -p GUI
}
#=============================================================

read_settings
while (true); do
	if [ "$UPDATE_REFRESH" == "true" ]; then
		check_connected
		if [ "$POP_UPS" == "true" ]; then 
			notify-send -i $LOGO_X -t 8000 "$NOTIFY_TITLE" "$NOTIFY_MSG"; 
			notify-send -i info -t 8000 $"Next Update" $"The next scheduled check for updates is $NUT"
		fi
	fi
	read_settings
		pkill -f "yad --title=Update-Notifier" 
		yad --title="$NOTIFY_TITLE" --notification --image="$LOGO_X" --icon-size="128" --text="$NOTIFY_MSG
		$NOTIFY_KERN"
		choose
done



exit_kill
