#!/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: apt, bash, bc, coreutils, gawk | mawk | original-awk, grep, inetutils-ping | iputils-ping, login (<<1:4.5-1.1) | util-linux (>=2.32-0.2), sed, wget
# Recommends: menu, pica-skel


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

CNAME="pica-actualiza"
SUBCNAME="pica-actualiza-check"
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=" -x""\t"$"Show help documentation."

while getopts x OPTION ; do
	case $OPTION in
		x )   docu_info ; exit 0 ;;
	esac
done


### ROOT CHECK ########################################

#if [ "$(cat /etc/passwd | grep -aE ^$(whoami): | cut -d ':' -f 3 )" -ne 0 ] ; then
if [ "$(id -u)" -ne 0 ] ; then
	echo $"$TITLE must be launched as SuperUser. Cannot continue."
	exit 1
fi
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/trinity/bin


### 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..." >> "$ERRORLOG"
	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." >> "$ERRORLOG"
		TESTINTERNET="Y"
	else
		echo $"No Internet connection." >> "$ERRORLOG"
		TESTINTERNET="N"
	fi
fi


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

YMD="$(date +%Y-%m-%d)"
if [ "$(date +%Y)" -lt 2025 ] ; then
	CLOCKTEXT=$"\nWARNING: The clock seems to be in the past: $YMD"
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 [ "$PRE_PICAVERSION" != "x" ] ; then
if [ "$(date +%Y%m)" -gt "$EOL" ] ; then
	EOLTEXT=$"\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'.)"
fi
fi


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

if [ "$TESTINTERNET" = "N" ] ; then
	echo -e $"No Internet connection. It is not possible to check if the operating system is updated."$CLOCKTEXT$EOLTEXT >> "$ERRORLOG"
	echo -e $"No Internet connection. It is not possible to check if the operating system is updated."$CLOCKTEXT$EOLTEXT
	exit 1
fi

##if [ "$(cat /etc/passwd | grep -aE ^$(whoami): | cut -d ':' -f 3 )" -eq 0 ] ; then
#if [ "$(id -u)" -eq 0 ] ; then
echo $(date +%Y-%m-%d_%H:%M:%S) "Start update" >> "$ERRORLOG"
apt-get --allow-releaseinfo-change update -y
apt-get update -y
echo $(date +%Y-%m-%d_%H:%M:%S) "End update" >> "$ERRORLOG"
#fi

echo $(date +%Y-%m-%d_%H:%M:%S) "upgrade check" >> "$ERRORLOG"
UPGRADETEST="$(echo $(apt-get -s dist-upgrade | grep -aE '^[0-9]' | grep -aEio '[0-9]') | sed 's| |+|g' | bc -l)"

if [ -e /tmp/pica-upgrade-not-necessary ] ; then rm -rf /tmp/pica-upgrade-not-necessary ; fi
if [ -e /tmp/pica-upgrade-necessary ] ; then rm -rf /tmp/pica-upgrade-necessary ; fi

if [ "$UPGRADETEST" -eq 0 ] && [ "$EOLTEXT" = "" ] ; then
	echo $(date +%Y-%m-%d_%H:%M:%S) "upgrade not necessary" >> "$ERRORLOG"
	echo -e "\e[1;32m"$"upgrade not necessary""\e[0m"
	touch /tmp/pica-upgrade-not-necessary
	chmod 666 /tmp/pica-upgrade-not-necessary
	for i in $(cat /etc/passwd | awk -F ':' '{print $3,$1}' | sort -n | grep -aEi "00[0-9] " | awk '{print $2}') ; do AUSER=$(users | sed 's| |\n|g' | grep -aE "^$i$" | head -n 1) ; if [ "$AUSER" != "" ] ; then if [ "$(id -nu $i)" != "" ] ; then chown $(id -nu $i):$(id -ng $i) /tmp/pica-upgrade-not-necessary ; else chown 1000:1000 /tmp/pica-upgrade-not-necessary ; fi ; break ; fi ; done
else
	echo $(date +%Y-%m-%d_%H:%M:%S) "upgrade is necessary" >> "$ERRORLOG"
	echo -e "\e[1;31m"$"upgrade is necessary""\e[0m"
	touch /tmp/pica-upgrade-necessary
	chmod 666 /tmp/pica-upgrade-necessary
	for i in $(cat /etc/passwd | awk -F ':' '{print $3,$1}' | sort -n | grep -aEi "00[0-9] " | awk '{print $2}') ; do AUSER=$(users | sed 's| |\n|g' | grep -aE "^$i$" | head -n 1) ; if [ "$AUSER" != "" ] ; then if [ "$(id -nu $i)" != "" ] ; then chown $(id -nu $i):$(id -ng $i) /tmp/pica-upgrade-necessary ; else chown 1000:1000 /tmp/pica-upgrade-necessary ; fi ; break ; fi ; done
fi

# Copy icon to /tmp
if [ -e /tmp/pica-upgrade-necessary ] ; then
	cp --remove-destination /usr/share/icons/upgrade-is-necessary.png /tmp/pica-actualiza.png
	cp --remove-destination /usr/share/pixmaps/upgrade-is-necessary.xpm /tmp/pica-actualiza.xpm
elif [ -e /tmp/pica-upgrade-not-necessary ] ; then
	cp --remove-destination /usr/share/icons/upgrade-not-necessary.png /tmp/pica-actualiza.png
	cp --remove-destination /usr/share/pixmaps/upgrade-not-necessary.xpm /tmp/pica-actualiza.xpm
else
	cp --remove-destination /usr/share/icons/pica-actualiza.png /tmp/pica-actualiza.png
	cp --remove-destination /usr/share/pixmaps/pica-actualiza.xpm /tmp/pica-actualiza.xpm
fi
# Permissions:
chmod 666 /tmp/pica-actualiza.png
chmod 666 /tmp/pica-actualiza.xpm
for i in $(cat /etc/passwd | awk -F ':' '{print $3,$1}' | sort -n | grep -aEi "00[0-9] " | awk '{print $2}') ; do AUSER=$(users | sed 's| |\n|g' | grep -aE "^$i$" | head -n 1) ; if [ "$AUSER" != "" ] ; then if [ "$(id -nu $i)" != "" ] ; then chown $(id -nu $i):$(id -ng $i) /tmp/pica-actualiza.png ; chown $(id -nu $i):$(id -ng $i) /tmp/pica-actualiza.xpm ; else chown 1000:1000 /tmp/pica-actualiza.png ; chown 1000:1000 /tmp/pica-actualiza.xpm ; fi ; break ; fi ; done
# Update menus:
if [ "$(command -v update-menus)" != "" ] ; then
	update-menus
fi
if [ -e "/tmp/pica-actualiza" ] ; then
	UORIG=$(stat -c "%U" /tmp/pica-actualiza)
	GORIG=$(stat -c "%G" /tmp/pica-actualiza)
	if [ "$(echo "$UORIG" | grep -aE "[a-z]")" = "" ] || [ "$(echo "$GORIG" | grep -aE "[a-z]")" = "" ] ; then
		UORIG=$(ls -l /tmp/pica-actualiza | awk '{print $3}')
		GORIG=$(ls -l /tmp/pica-actualiza | awk '{print $4}')
	fi
	chown root:root /tmp/pica-actualiza
	echo "icon:/tmp/pica-actualiza.png" > /tmp/pica-actualiza
	chown $UORIG:$GORIG /tmp/pica-actualiza
fi

# Run pica-actualiza if upgrade is necessary
if [ -e /tmp/pica-upgrade-necessary ] ; then

	#if [ -t 0 ] ; then
	if [ "$(setxkbmap 1>/dev/null 2>&1 ; echo $? )" -ne 0 ] ; then
		export DISPLAY=:0
	fi

	for i in $(cat /etc/passwd | awk -F ':' '{print $3,$1}' | sort -n | grep -aEi "00[0-9] " | awk '{print $2}') ; do AUSER=$(users | sed 's| |\n|g' | grep -aE "^$i$" | head -n 1) ; if [ "$AUSER" != "" ] ; then
		su -c 'pica-actualiza' $i &
		break
	fi ; done

fi

exit 0
