#!/bin/bash
# -*- ENCODING: UTF-8 -*-

# pica-actualiza
#
# Check if the operating system is updated.
#
# Copyright (c) 2013-2026: Alexis Puente Montiel   < pica (a) picalibre.org >
#
# Licensed according to GNU AGPL version 3.0.
#
# It is libre/free software; you can use, redistribute and/or modify it according to the terms of GNU AGPL as published by GNU, version 3.0, 19 November 2007.
#
# It is distributed in the hope that it will be useful, but without any warranty. Read GNU AGPL version 3.0 for additional details.
#
# A copy of GNU AGPL version 3.0 is available at /usr/share/doc/<software-package-name>/agpl-3.0.txt (additionally on Internet as text at https://www.gnu.org/licenses/agpl-3.0.txt and as HTML at https://www.gnu.org/licenses/agpl-3.0-standalone.html ).
#
# Note: Additionally to the official e-mails, picalibre.org is strictly the only official site for this software project, please consider using it to download, report bugs and contribute.
#
# Depends: anacron, apt, at, bash, bc, coreutils, cron, dialog | yad | yad-pica | zenity, dpkg, gawk | mawk | original-awk, grep, login (<<1:4.5-1.1) | util-linux (>=2.32-0.2), lsof, menu, pica-apt | synaptic, sed
# Recommends: inetutils-ping | iputils-ping, lxterminal | sakura | xfce4-terminal, pica-skel, x11-xkb-utils, wget
# Replaces: unattended-upgrades


### SCRIPT VARIABLES ########################################

CNAME="pica-actualiza"
VERSION="1.3.1"
TITLE="Pica-Actualiza"
ICON="/usr/share/icons/pica-actualiza.png"

# Translations
if [ "$LANG" = "" ] ; then export $(cat /etc/default/locale | grep -a 'LANG=') ; fi
TEXTDOMAIN=pica-actualiza
TEXTDOMAINDIR=/usr/share/locale/

# Write errors to log
ERRORLOG="$HOME/.${CNAME}.log"
#if [ -e "$ERRORLOG" ] ; then
#	mv -f $ERRORLOG ${ERRORLOG}.ant
#fi
#if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi

for i in /etc/pica-global.dist /etc/pica-global.orig /etc/pica-global /etc/pica-global.local ~/.pica-global ~/pica-global ; do
	if [ -f "$i" ] ; then
	cat "$i"
	source "$i"
	source <(cat $i | sed -e "s/=\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)/=Y/g" -e "s/=\(No\|no\|n\)/=N/g" -e "s/=\"\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)\"/=Y/g" -e "s/=\"\(No\|no\|n\)\"/=N/g")
	fi
done
if [ "$DEBUG" = "Y" ] ; then
	set -xv
	DEBUG="Y"
else
	ERRORLOG="/tmp/.${CNAME}_$(id -nu).log"
	if [ -e "$ERRORLOG" ] ; then mv -f $ERRORLOG ${ERRORLOG}.ant ; fi
	if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi
fi
#if [ "$DEBUG" = "" ] ; then DEBUG="N" ; fi
if [ "$DEBUG" != "N" ] ; then
exec > >(tee -a "$ERRORLOG") 2>&1
echo "$0" "$*" >> "$ERRORLOG"
echo "$CNAME" "$VERSION" >> "$ERRORLOG"
echo $(date +%Y-%m-%d_%H:%M:%S) $"Start" >> "$ERRORLOG"
echo "env:" >> "$ERRORLOG"
env >> "$ERRORLOG"
echo "set:" >> "$ERRORLOG"
set >> "$ERRORLOG"
#else
#exec 2>>"$ERRORLOG"
fi

# Description:
BDESCRIP=$"Check if the operating system is updated."
LDESCRIP=$"$TITLE is a tool to check if the operating system is updated."

# Documentation:
docu_info () {
echo "$CNAME ($VERSION) - $BDESCRIP"
echo 
echo $"Usage:" $CNAME [$"OPTIONS"]
echo 
echo $"Options:"
echo -e "$ODESCRIP"
echo 
echo $"'man $CNAME' for more information."
echo 
}
ODESCRIP=" -s""\t"$"Do not show window.""\n"" -t""\t"$"Use terminal interface.""\n"" -k""\t"$"Use 'kdialog' if possible.""\n"" -y""\t"$"Use 'yad' if possible (by default in graphical sessions).""\n"" -z""\t"$"Use 'zenity' if possible.""\n"" -x""\t"$"Show help documentation."

while getopts stkyzx OPTION ; do
	case $OPTION in
		s )   SIL="Y"            ;;
		t )   T="Y" ; G="N"      ;;
		k )   SDIALOG="kdialog"  ;;
		y )   SDIALOG="yad"      ;;
		z )   SDIALOG="zenity"   ;;
		x )   docu_info ; exit 0 ;;
	esac
done


### DIALOG ########################################

#DEFAULTDIALOGLIST="yad zenity kdialog"
DEFAULTDIALOGLIST="yad zenity"

#if [ -t 0 ] ; then
if [ "$(setxkbmap 1>/dev/null 2>&1 ; echo $? )" -ne 0 ] ; then
	G="N" ; T="Y"
fi

if [ "$G" != "N" ] ; then
	
	if   [ "$SDIALOG" = "kdialog" ] ; then
		DIALOGLIST="kdialog yad zenity"
	elif [ "$SDIALOG" = "yad" ] ; then
		DIALOGLIST="yad zenity kdialog"
	elif [ "$SDIALOG" = "zenity" ] ; then
		DIALOGLIST="zenity yad kdialog"
	else
		DIALOGLIST="$DEFAULTDIALOGLIST"
	fi
	
	if [ "$DIALOGLIST" = "" ] ; then
		if [ "$DEFAULTDIALOGLIST" != "" ] ; then
			DIALOGLIST="$DEFAULTDIALOGLIST"
		else
			DIALOGLIST="yad zenity kdialog"
		fi
	fi
	
	if [ "$(command -v $DIALOGLIST)" = "" ] ; then
		G="N" ; T="Y"
	fi

fi


if [ "$G" = "N" ] ; then

	if [ "$(command -v dialog)" = "" ] ; then
		echo $"ERROR: No supported dialog is installed. $TITLE cannot continue."
		exit 1
	else
		
		DIALOG="dialog --cr-wrap --keep-window --output-fd 1"
		#DIALOG="dialog --cr-wrap --keep-window --output-fd 1 --no-lines --no-shadow"
		if [ "$TITLE" != "" ] ; then
			WICON="--backtitle $(echo $TITLE | sed 's| |-|g')"
		fi
		WTITLE="--title"
		WTEXT=""
		H="0"
		W="0"
		LH="0"
		CALENDAR="--calendar"
		DATEFORMAT="--date-format %Y-%m-%d"
		WTIME="--timebox"
		TIMEFORMAT="--time-format %H:%M:%S"
		DSEL="--dselect"
		FSEL="--fselect"
		WRITE="--inputbox"
		PREWRITE=""
		COLUMN=""
		PRINTCOLUMN1=""
		PRINTCOLUMN2=""
		HIDECOLUMN1=""
		HIDECOLUMN2=""
		#INPUTSEPARATOR="--column-separator"
		#OUTPUTSEPARATOR="--separate-output"
		MONOLIST="--menu"
		#MONOLIST="--radiolist"
		MULTIPLELIST="--separate-output --checklist"
		PRE=""
		POST="off"
		PRES=""
		POSTS="on"

		INFO="--msgbox"
		WAIT="--infobox"

		QYN="--yesno"
		QYNC="--yesno"
		WYN="--yesno"
		WCC="--yesno"
		WYNC="--yesno"
		WM="--msgbox"
		ERROR="--msgbox"

		FORM="--form"
		FORMT=""
		FORMSELPRE=""
		FORMSELPOST=""
		#FORMCALPRE=""
		#FORMCALPOST=""
		#FORMCLPRE=""
		#FORMCLPOST=""
		PRELAB1="1 1"
		POSTLAB1="1 10 999 0"
		PRELAB2="2 1"
		POSTLAB2="2 10 999 0"
		PRELAB3="3 1"
		POSTLAB3="3 10 999 0"

		YES="--yes-label"
		NO="--no-label"
		OK="--ok-label"
		CONTINUE="--ok-label"
		CANCEL="--cancel-label"
		EXTRABUTTON="--extra-button --extra-label"
	
	fi

else

	for i in $DIALOGLIST ; do

		if [ "$(command -v $i)" != "" ] ; then
	
			if [ "$i" = "yad" ] ; then
		
				if [ "$(yad --version | tail -n 1 | cut -d ' ' -f 1 | cut -d '.' -f 1)" -lt 2 ] ; then
					DIALOG="yad --center"  # --selectable-labels (bug in Yad < 2.0) https://github.com/v1cont/yad/blob/master/NEWS
				else
					DIALOG="yad --center --selectable-labels"  # --on-top
				fi
				if [ "$ICON" != "" ] ; then
					WICON="--window-icon $ICON"
				fi
				WTITLE="--title"
				WTEXT="--text"
				H=""
				if [ "$(yad --version | tail -n 1 | grep -aE ^'7.2')" != "" ] ; then
					W="--width=640"
				else
					W="--width=640 --text-width=640 --fixed"  # https://github.com/v1cont/yad/issues/107  https://github.com/v1cont/yad/issues/140
				fi
				LH=""
				CALENDAR="--calendar"
				DATEFORMAT="--date-format %Y-%m-%d"
				WTIME="--separator=\n --columns 3 --form"
				#TIMEFORMAT="--time-format %H:%M:%S"
				DSEL="--file --directory --filename"  # Deleted *-selection alias for --file in Yad 4.0
				FSEL="--file --filename"              # Deleted *-selection alias for --file in Yad 4.0
				WRITE="--entry"
				PREWRITE="--entry-text"
				COLUMN="--column="
				PRINTCOLUMN1="--print-column=01"
				PRINTCOLUMN2="--print-column=02"
				HIDECOLUMN1="--hide-column=01"
				HIDECOLUMN2="--hide-column=02"
				#INPUTSEPARATOR="--item-separator"
				#OUTPUTSEPARATOR="--separator="
				#MONOLIST="--entry"
				MONOLIST="--separator=\n --list --search-column=1 --no-headers"
				MONOLISTH="--separator=\n --list --search-column=1"
				MULTIPLELIST="--separator= --list --checklist --search-column=2 --no-headers"
				MULTIPLELISTH="--separator= --list --checklist --search-column=2"
				PRE="FALSE"
				POST=""
				PRES="TRUE"
				POSTS=""
				# standard gtk keybindings for next and previous search are ctrl+g and ctrl+shift+g while you can redefine them in gtk-keys.css for your theme  # https://github.com/v1cont/yad/issues/127
			
				INFO="--image=/usr/share/icons/picalibre/rm/play.png --button="$"OK"
				WAIT="--image=/usr/share/icons/picalibre/rm/play.png --no-buttons"
			
				QYN="--image=/usr/share/icons/picalibre/rm/question.png --button="$"Yes"":0 --button="$"No"":1"
				QYNC="--image=/usr/share/icons/picalibre/rm/question.png --button="$"Yes"":0 --button="$"No"":1 --button="$"Cancel"":1"
				WYN="--image=/usr/share/icons/picalibre/rm/warning.png --button="$"Yes"":0 --button="$"No"":1"
				WCC="--image=/usr/share/icons/picalibre/rm/warning.png --button="$"Continue"":0 --button="$"Cancel"":1"
				WYNC="--image=/usr/share/icons/picalibre/rm/warning.png --button="$"Yes"":0 --button="$"No"":1 --button="$"Cancel"":1"
				WM="--image=/usr/share/icons/picalibre/rm/warning.png --button="$"OK"
				ERROR="--image=/usr/share/icons/picalibre/rm/error.png --button="$"OK"
			
				FORM="--separator=\n --form"
				FORMT="--field"
				FORMSELPRE="--field"
				FORMSELPOST=":FL"
				FORMCALPRE="--field"
				FORMCALPOST=":DT"
				FORMCLPRE="--field"
				FORMCLPOST=":CB"
				PRELAB1=""
				POSTLAB1=""
				PRELAB2=""
				POSTLAB2=""
				PRELAB3=""
				POSTLAB3=""
			
				YES="--button"
				NO="--button"
				OK="--button"
				CONTINUE="--button"
				CANCEL="--button"
				EXTRABUTTON="--button"
		
			elif [ "$i" = "zenity" ] ; then
		
				DIALOG="zenity"
				if [ "$ICON" != "" ] ; then
					WICON="--window-icon $ICON"
				fi
				WTITLE="--title"
				WTEXT="--text"
				H=""
				W="--width=640"
				LH=""
				CALENDAR="--calendar"
				DATEFORMAT="--date-format %Y-%m-%d"
				WTIME="--separator : --forms"
				#TIMEFORMAT="--time-format %H:%M:%S"
				DSEL="--file-selection --directory --filename"
				FSEL="--file-selection --filename"
				WRITE="--entry"
				PREWRITE="--entry-text"
				COLUMN="--column="
				PRINTCOLUMN1="--print-column=01"
				PRINTCOLUMN2="--print-column=02"
				HIDECOLUMN1="--hide-column=01"
				HIDECOLUMN2="--hide-column=02"
				#INPUTSEPARATOR=""
				#OUTPUTSEPARATOR="--separator=\n"
				MONOLIST="--separator=\n --list --hide-header"
				MONOLISTH="--separator=\n --list"
				MULTIPLELIST="--separator=\n --list --checklist --hide-header"
				MULTIPLELISTH="--separator=\n --list --checklist"
				PRE="FALSE"
				POST=""
				PRES="TRUE"
				POSTS=""
			
				INFO="--info"
				WAIT="--info"
			
				QYN="--question"
				QYNC="--question"
				WYN="--question"
				WCC="--question"
				WYNC="--question"
				WM="--warning"
				ERROR="--error"
			
				FORM="--forms"
				FORMT="--add-entry"
				FORMSELPRE="--add-entry"
				FORMSELPOST=""
				FORMCALPRE="--add-calendar"
				FORMCALPOST=""
				if [ "$(zenity --version | cut -d '.' -f 1 )" -ge 3 ] || [ "$(zenity --version | sed 's|\.||g')" -ge 3102 ] ; then
					FORMCLPOST="--combo-values"
					FORMCLPRE="--add-combo"
				fi
				PRELAB1=""
				POSTLAB1=""
				PRELAB2=""
				POSTLAB2=""
				PRELAB3=""
				POSTLAB3=""
			
				# Zenity <3 does not support custom buttons.
				if [ "$(zenity --version | cut -d '.' -f 1 )" -ge 3 ] ; then
					YES="--ok-label"
					NO="--cancel-label"
					OK="--ok-label"
					CONTINUE="--ok-label"
					CANCEL="--cancel-label"
					#EXTRABUTTON=""
				fi
		
#			elif [ "$i" = "kdialog" ] ; then
#		
#				# CAUTION: All 'kdialog' (TDE, KDE Plasma 4, KDE Plasma 5, KDE Plasma 6,...) are included here!
#			
#				DIALOG="kdialog"
#				if [ "$ICON" != "" ] ; then
#					WICON="--icon $ICON"
#				fi
#				WTITLE="--title"
#				WTEXT=""
#				H=""
#				W=""
#				LH=""
#				# No "--calendar" in TDE-kdialog + No "--date-format" both TDE and Plasma = There is no usable version of kdialog for calendars
#				#CALENDAR="--calendar"
#				#DATEFORMAT="--date-format %Y-%m-%d"
#				#WTIME="--timebox"
#				#TIMEFORMAT="--time-format %H:%M:%S"
#				DSEL="--getexistingdirectory"
#				FSEL="--getsavefilename"
#				#FSEL="--getopenfilename"
#				WRITE="--inputbox"
#				PREWRITE=""
#				COLUMN=""
#				PRINTCOLUMN1=""
#				PRINTCOLUMN2=""
#				HIDECOLUMN1=""
#				HIDECOLUMN2=""
#				#INPUTSEPARATOR=""
#				#OUTPUTSEPARATOR="--separate-output"
#				MONOLIST="--menu"
#				#MONOLIST="--combobox"
#				#MONOLIST="--radiolist"
#				MULTIPLELIST="--separate-output --checklist"
#				PRE=""
#				POST="off"
#				PRES=""
#				POSTS="on"
#			
#				INFO="--msgbox"
#				WAIT="--msgbox"
#			
#				QYN="--yesno"
#				QYNC="--yesnocancel"
#				WYN="--warningyesno"
#				WCC="--warningcontinuecancel"
#				WYNC="--warningyesnocancel"
#				WM="--sorry"
#				ERROR="--error"
#			
#				# No "--form" in any kdialog version
#				#FORM="--form"
#				#FORMT=""
#				#FORMSELPRE=""
#				#FORMSELPOST=""
#				#FORMCALPRE=""
#				#FORMCALPOST=""
#				#FORMCLPRE=""
#				#FORMCLPOST=""
#				#PRELAB1=""
#				#POSTLAB1=""
#				#PRELAB2=""
#				#POSTLAB2=""
#				#PRELAB3=""
#				#POSTLAB3=""
#			
#				# TDE-kdialog does not support custom buttons.
#				if [ "$(kdialog --version | grep -a Qt | grep -aEo "[0-9]" | head -n 1)" -ge 4 ] || [ "$(kdialog --version | grep -aEi kdialog | grep -aEo "[0-9]" | head -n 1)" -gt 1 ] ; then
#					YES="--yes-label"
#					NO="--no-label"
#					OK="--continue-label"
#					CONTINUE="--continue-label"
#					CANCEL="--cancel-label"
#					EXTRABUTTON="--cancel-label"
#				fi
		
			else
		
				true
		
			fi
		
		if [ "$DIALOG" != "" ] ; then break ; fi
		
		fi

	done

fi

if [ "$DIALOG" = "" ] ; then
	echo $"ERROR: No supported dialog is installed. $TITLE cannot continue."
	exit 1
fi


### TERMINAL SELECTION ########################################

if [ "$G" != "N" ] && [ "$(command -v lxterminal sakura xfce4-terminal)" = "" ] ; then $DIALOG $WICON $WTITLE "$TITLE" $WM    $WTEXT $"WARNING: Neither 'lxterminal', nor 'sakura' nor 'xfce4-terminal' are installed, $TITLE might fail." $H $W ; fi

TERMINAL () {
if [ "$G" = "N" ] ; then
	if [ "$TT" != "$TITLE" ] ; then echo "$TT" ; sleep 1 ; fi
	$COM
else
	if [ "$(command -v sakura)" != "" ] ; then
#		sakura -t "$TT" -e "$COM"  # https://bugs.debian.org/782396
		sakura -t "$TT" -x "$COM"  # sakura '-e' bug en Debian 13 "Trixie": Segment violation
	elif [ "$(command -v lxterminal)" != "" ] ; then
		lxterminal -t "$TT" -e "$COM"
	elif [ "$(command -v xfce4-terminal)" != "" ] ; then
		xfce4-terminal -T "$TT" -e "$COM"
	else
		x-terminal-emulator -t "$TT" -e "$COM"
	fi
fi
}


### BASICFUN ########################################

check_exit () {

TAILERRORLOG=$(cat "$ERRORLOG" | grep -aE "[a-zA-Z0-9\=\[]" | tail -n 10 )

echo $(date +%Y-%m-%d_%H:%M:%S)

$DIALOG $WICON $WTITLE "$TITLE" $ERROR $WTEXT $"Stop due to error.\n\n$TAILERRORLOG\n" $H $W

exit 1

}

# "try again or exit" dialog window
input_error () {

$DIALOG $WICON $WTITLE "$TITLE" $WYN   $WTEXT $"The update has failed.\n\nDo you want to try again?" $H $W

if [ "$?" -ne 0 ] ; then
	exit 1
else
	$REFUN
fi

}


pica-actualiza_script () {

### INTERNET TEST ########################################

ping -c 3 ftp.fau.de || ping -c 3 mirrorservice.org || ping -c 3 ftp.ch.debian.org

if [ "$?" -eq 0 ] ; then
	TESTINTERNET="Y"
else
	echo "No PING (no ICMP). Pass-through test..."
	wget http://ftp.fau.de -O /dev/null || wget http://mirrorservice.org -O /dev/null || wget http://ftp.ch.debian.org -O /dev/null
	if [ "$?" -eq 0 ] ; then
		echo "WGET (TCP connections) works. Internet OK."
		TESTINTERNET="Y"
	else
		echo $"No Internet connection."
		TESTINTERNET="N"
	fi
fi


### READ-ONLY CHECK ########################################

# If the system is operating in read-only mode (for example a live session without persistence), then it is not worth spending time updating the operating system.

if [ "$(cat /etc/mtab | grep -aEi ^/ | grep -aEiv /dtmp/ | awk '{print $4}' | sed 's|,|\n|g' | grep -aE "^rw$")" = "" ] ; then

	ROTEXT=$"\n\nThe system seems to be operating in read-only mode (perhaps a live session without persistence), if so then it is not worth spending time updating the operating system."

fi


### CLOCK CHECK ########################################

YMD="$(date +%Y-%m-%d)"
if [ "$(date +%Y)" -lt 2025 ] ; then
	CLOCKTEXT=$"\n\nThe clock seems to be in the past: $YMD \nIf it is a computer with a button battery to keep time, then the button battery probably needs to be replaced (if this is not the first time this computer is turned on), to prevent the clock reverting to the past every time the computer loses power."
fi


### RELEASE EOL CHECK ########################################

if [ -e /etc/picalibre_version ] ; then cat /etc/picalibre_version >> "$ERRORLOG" ; PRE_PICAVERSION=$(cat /etc/picalibre_version | cut -d '.' -f 1 ) ; fi
if [ -e /etc/debian_version ] ; then cat /etc/debian_version >> "$ERRORLOG" ; PRE_DEBIANVERSION=$(cat /etc/debian_version | cut -d '.' -f 1 ) ; fi
if [ -e /etc/devuan_version ] ; then cat /etc/devuan_version >> "$ERRORLOG" ; PRE_DEVUANVERSION=$(expr $(cat /etc/devuan_version | sed -e 's|jessie|1|g' -e 's|ascii|2|g' -e 's|beowulf|3|g' -e 's|chimaera|4|g' -e 's|daedalus|5|g' -e 's|excalibur|6|g' | cut -d '.' -f 1 | sed 's|/ceres||g') + 7 ) ; fi
DEBIANVERSION=$(printf "$PRE_PICAVERSION\n$PRE_DEBIANVERSION\n$PRE_DEVUANVERSION" | sort -n -r | head -n 1 )
POSTDEBIANVERSION=$(expr -$DEBIANVERSION - 1)

if [ "$(dpkg -l | grep -aEi "^ii|^hi" | awk '{print $2}' | sed "s|:.*||g" | grep -aEi "^falkon" )" != "" ] ; then
NVER=$(echo "1999+($DEBIANVERSION*2)+2"  | bc -l)12
EOL=$(echo "1999+($DEBIANVERSION*2)+3"   | bc -l)06
else
NVER=$(echo "1999+($DEBIANVERSION*2)+3"  | bc -l)06
EOL=$(echo "1999+($DEBIANVERSION*2)+3+2" | bc -l)06
fi

if [ "$(date +%Y%m)" -lt "$NVER" ] || [ "$PRE_PICAVERSION" = "x" ] ; then
	EOLTEXT=""
elif [ "$(date +%Y%m)" -le "$EOL" ] ; then
	EOLTEXT=$"\n\nIt is recommended that you install or upgrade to the new version of PicaLibre. (In order to install the new version of PicaLibre then download from www.picalibre.org, while in order to upgrade then install pica-eleva$POSTDEBIANVERSION using Synaptic or 'app'.)"
elif [ "$(date +%Y%m)" -gt "$EOL" ] ; then
	EOLTEXT=$"\n\nWARNING: Your version of PicaLibre is obsolete and it has no security updates, it is critical that you install or upgrade to a newer version of PicaLibre immediately. (In order to install the new version of PicaLibre then download from www.picalibre.org, while in order to upgrade then install pica-eleva$POSTDEBIANVERSION using Synaptic or 'app'.)"
else
	echo $"ERROR:" $(date +%Y%m) DEBIANVERSION=$DEBIANVERSION PRE_DEBIANVERSION=$PRE_DEBIANVERSION PRE_DEVUANVERSION=$PRE_DEVUANVERSION
	check_exit
fi


### SCRIPT ########################################

if [ "$G" != "N" ] ; then
	PipePicaActualiza="/tmp/pica-actualiza"
	mkfifo $PipePicaActualiza
	# Attach a (sole) file descriptor (0-1023) to the named pipe (bidirectional to kept open the stream):
	if [ "$DEBUG" = "Y" ] ; then find /proc/*/fd ! -type d 2>$ERRORLOG | awk -F '/' '{print $NF}' | sort -nu >> "$ERRORLOG" ; fi
	exec 777<> $PipePicaActualiza
	if [ ! -e "/tmp/pica-actualiza.png" ] ; then cp /usr/share/icons/pica-actualiza.png /tmp/pica-actualiza.png ; chmod 666 /tmp/pica-actualiza.png ; fi
	$DIALOG --notification --listen --image="/tmp/pica-actualiza.png" $WTITLE "$TITLE" $WTEXT "$TITLE" --command="pica-actualiza" <$PipePicaActualiza &
	lsof $PipePicaActualiza | grep -aEi "^$(echo $DIALOG | cut -d ' ' -f 1)" | awk '{if ( $4 == "0r" ) print $2}' | sort -n -r | sed "1d" | while read i ; do kill $i ; done
fi

if [ "$SIL" != "Y" ] ; then

if [ -e /tmp/pica-upgrade-necessary ] ; then

	OPT=$($DIALOG $WICON $WTITLE "$TITLE" $MONOLIST $WTEXT $"There are updates pending to install.${CLOCKTEXT}${EOLTEXT}${ROTEXT}\n\nPlease select an option.\n\nNote: It is recommended (but not essential) to close the windows of the operating applications (such as web browsers, text editors, spreadsheets, media players,...) during the update." $H $W $LH $COLUMN $COLUMN \
	$"Launch 'app u' in a terminal (recommended)" " " \
	$"Open Synaptic to install the updates" " " \
	$"Remember within 10 minutes" " " \
	$"Remember within 30 minutes" " " \
	$"Remember within 1 hour" " " \
	$"Remember within 3 hours" " " \
	$"No more notifications for the rest of the day" " " \
	$"Fix an accidentally interrupted update" " " \
	| head -n 1 )

	if [ "$?" -ne 0 ] || [ "$OPT" = "" ] ; then
		exit 0
	fi

	case $OPT in

	$"No more notifications for the rest of the day" )
		exit 0
	;;

	$"Open Synaptic to install the updates" )
	
		LOCALREPO=$(cat /etc/apt/sources.list | grep -aEi "^deb " | grep -aEiv '//' )
	
		if [ "$TESTINTERNET" = "N" ] && [ "$LOCALREPO" = "" ] ; then
			echo $"ERROR: No Internet connection, no local copy of packages."
			$DIALOG $WICON $WTITLE "$TITLE" $ERROR $WTEXT $"ERROR: No Internet connection, no local copy of packages." $H $W
			exit 1
		fi
	
		echo "synaptic" >> "$ERRORLOG"
		echo "synaptic ; pica-actualiza-check" >> /tmp/pica-actualiza.sh
		chmod +x /tmp/pica-actualiza.sh
		TT="$TITLE" ; COM="su-to-root -c /tmp/pica-actualiza.sh" ; TERMINAL #su-to-root -X -c /tmp/pica-actualiza.sh
		rm -f /tmp/pica-actualiza.sh
	
		if [ -e /tmp/pica-upgrade-necessary ] ; then
			REFUN=pica-actualiza_script
			input_error ; return
		else
			
			# False positives from 'checkrestart' (debian-goodies)
			#if [ "$(dpkg -l | awk '{print $2}' | sed "s|:.*||g" | grep -aEi linux-image-[0-9] | grep -a '\.' | sort -Vr | head -n 1 | sed 's|[a-z.+-]||g')" -ne "$(uname -r | sed 's|[a-z.+-]||g')" ] || [ "$(expr $(checkrestart | grep -a -c ^) - 1)" -gt 0 ] || [ "$(lsof 2>>$ERRORLOG | grep -a lib | grep -a ' DEL ' | grep -a -c ^)" -ne 0 ] ; then
			if [ "$(dpkg -l | awk '{print $2}' | sed "s|:.*||g" | grep -aEi linux-image-[0-9] | grep -a '\.' | sort -Vr | head -n 1 | sed 's|[a-z.+-]||g')" -ne "$(uname -r | sed 's|[a-z.+-]||g')" ] || [ "$(lsof 2>>$ERRORLOG | grep -a lib | grep -a ' DEL ' | grep -a -c ^)" -ne 0 ] ; then
				$DIALOG $WICON $WTITLE "$TITLE" $INFO  $WTEXT $"The update will be completed when the computer is rebooted or the next time it is turned on." $H $W
			fi
			
			exit 0
		
		fi
	
	;;

	$"Launch 'app u' in a terminal (recommended)" )
	
		LOCALREPO=$(cat /etc/apt/sources.list | grep -aEi "^deb " | grep -aEiv '//' )
	
		if [ "$TESTINTERNET" = "N" ] && [ "$LOCALREPO" = "" ] ; then
			echo $"ERROR: No Internet connection, no local copy of packages."
			$DIALOG $WICON $WTITLE "$TITLE" $ERROR $WTEXT $"ERROR: No Internet connection, no local copy of packages." $H $W
			exit 1
		fi
	
		echo "terminal" >> "$ERRORLOG"
		echo "apt-actualiza ; pica-actualiza-check" >> /tmp/pica-actualiza.sh
		chmod +x /tmp/pica-actualiza.sh
		TT="$TITLE" ; COM="su-to-root -c /tmp/pica-actualiza.sh" ; TERMINAL
		rm -f /tmp/pica-actualiza.sh
	
		if [ -e /tmp/pica-upgrade-necessary ] ; then
			REFUN=pica-actualiza_script
			input_error ; return
		else
			
			# False positives from 'checkrestart' (debian-goodies)
			#if [ "$(dpkg -l | awk '{print $2}' | sed "s|:.*||g" | grep -aEi linux-image-[0-9] | grep -a '\.' | sort -Vr | head -n 1 | sed 's|[a-z.+-]||g')" -ne "$(uname -r | sed 's|[a-z.+-]||g')" ] || [ "$(expr $(checkrestart | grep -a -c ^) - 1)" -gt 0 ] || [ "$(lsof 2>>$ERRORLOG | grep -a lib | grep -a ' DEL ' | grep -a -c ^)" -ne 0 ] ; then
			if [ "$(dpkg -l | awk '{print $2}' | sed "s|:.*||g" | grep -aEi linux-image-[0-9] | grep -a '\.' | sort -Vr | head -n 1 | sed 's|[a-z.+-]||g')" -ne "$(uname -r | sed 's|[a-z.+-]||g')" ] || [ "$(lsof 2>>$ERRORLOG | grep -a lib | grep -a ' DEL ' | grep -a -c ^)" -ne 0 ] ; then
				$DIALOG $WICON $WTITLE "$TITLE" $INFO  $WTEXT $"The update will be completed when the computer is rebooted or the next time it is turned on." $H $W
			fi
			
			exit 0
		
		fi
	
	;;

	$"Fix an accidentally interrupted update"  )
	
		echo "apt-problem" >> "$ERRORLOG"
		echo "apt-problem ; pica-actualiza-check" >> /tmp/pica-actualiza.sh
		chmod +x /tmp/pica-actualiza.sh
		TT="$TITLE" ; COM="su-to-root -c /tmp/pica-actualiza.sh" ; TERMINAL
		rm -f /tmp/pica-actualiza.sh
	
		# False positives from 'checkrestart' (debian-goodies)
		#if [ "$(dpkg -l | awk '{print $2}' | sed "s|:.*||g" | grep -aEi linux-image-[0-9] | grep -a '\.' | sort -Vr | head -n 1 | sed 's|[a-z.+-]||g')" -ne "$(uname -r | sed 's|[a-z.+-]||g')" ] || [ "$(expr $(checkrestart | grep -a -c ^) - 1)" -gt 0 ] || [ "$(lsof 2>>$ERRORLOG | grep -a lib | grep -a ' DEL ' | grep -a -c ^)" -ne 0 ] ; then
		if [ "$(dpkg -l | awk '{print $2}' | sed "s|:.*||g" | grep -aEi linux-image-[0-9] | grep -a '\.' | sort -Vr | head -n 1 | sed 's|[a-z.+-]||g')" -ne "$(uname -r | sed 's|[a-z.+-]||g')" ] || [ "$(lsof 2>>$ERRORLOG | grep -a lib | grep -a ' DEL ' | grep -a -c ^)" -ne 0 ] ; then
			$DIALOG $WICON $WTITLE "$TITLE" $INFO  $WTEXT $"The update will be completed when the computer is rebooted or the next time it is turned on." $H $W
		fi
	
		exit 0
	
	;;

	$"Remember within 10 minutes" )
		echo $(date +%Y-%m-%d_%H:%M:%S) "10 m"
		(sleep 600 && pica-actualiza)&

		exit 0
	;;

	$"Remember within 30 minutes" )
		echo $(date +%Y-%m-%d_%H:%M:%S) "30 m"
		(sleep 1800 && pica-actualiza)&

		exit 0
	;;

	$"Remember within 1 hour" )
		echo $(date +%Y-%m-%d_%H:%M:%S) "1 h"
		(sleep 3600 && pica-actualiza)&

		exit 0
	;;

	$"Remember within 3 hours" )
		echo $(date +%Y-%m-%d_%H:%M:%S) "3 h"
		(sleep 10800 && pica-actualiza)&

		exit 0
	;;

	* )
		exit 1
	;;

	esac


elif [ -e /tmp/pica-upgrade-not-necessary ] ; then

	$DIALOG $WICON $WTITLE "$TITLE" $INFO  $WTEXT $"There seem to be no updates pending to install.\n\nSelect 'Yes/OK' if you want to launch 'app u' in a terminal, otherwise just close this window.\n\nIf in doubt, just close this window.${CLOCKTEXT}${EOLTEXT}" $H $W

	if [ "$?" -eq 0 ] ; then
		
		echo "terminal" >> "$ERRORLOG"
		echo "apt-actualiza ; pica-actualiza-check" >> /tmp/pica-actualiza.sh
		chmod +x /tmp/pica-actualiza.sh
		TT="$TITLE" ; COM="su-to-root -c /tmp/pica-actualiza.sh" ; TERMINAL
		rm -f /tmp/pica-actualiza.sh
		
	fi
	
	exit 0

else

	echo $(date +%Y-%m-%d_%H:%M:%S) "WARNING: No tmp mark" >> "$ERRORLOG"
	ls -FAl --full-time /tmp >> "$ERRORLOG"

	$DIALOG $WICON $WTITLE "$TITLE" $WYN   $WTEXT $"No information about system update status.\n\nDo you want to get this information now?${CLOCKTEXT}${EOLTEXT}${ROTEXT}" $H $W

	if [ "$?" -eq 0 ] ; then
	
		LOCALREPO=$(cat /etc/apt/sources.list | grep -aEi "^deb " | grep -aEiv '//' )
	
		if [ "$TESTINTERNET" = "N" ] && [ "$LOCALREPO" = "" ] ; then
			echo $"ERROR: No Internet connection, no local copy of packages."
			$DIALOG $WICON $WTITLE "$TITLE" $ERROR $WTEXT $"ERROR: No Internet connection, no local copy of packages." $H $W
			exit 1
		fi
	
		if [ "$G" = "N" ] ; then
			su-to-root -c pica-actualiza-check
		else
			su-to-root -X -c pica-actualiza-check
		fi
	
		pica-actualiza_script
		return
	
	fi

	exit 0

fi

fi # End: if [ "$SIL" != "Y" ]

}

if [ "$SIL" = "Y" ] ; then
	if [ -e /tmp/pica-upgrade-not-necessary ] || [ -e /tmp/pica-upgrade-necessary ] ; then
		pica-actualiza_script
	else
		exit 0
	fi
else
	pica-actualiza_script
fi

exit 0
