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

# apt-post
#
# Checks after installing packages.
#
# 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: bash, coreutils, dpkg, gawk | mawk | original-awk, grep, lsof, sed
# Recommends: debian-security-support, pica-skel


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

CNAME="apt-post"
VERSION="1.3.1"
TITLE="'apt-post'"
ICON="/usr/share/icons/apt-post.png"

# Translations
if [ "$LANG" = "" ] ; then export $(cat /etc/default/locale | grep -a 'LANG=') ; fi
TEXTDOMAIN=pica-apt
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=$"Checks after installing packages."
LDESCRIP=$"$TITLE is a tool to checks after installing packages."

# 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


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

if [ "$(lsof /var/lib/apt/lists/lock 2>>$ERRORLOG | grep -a -c ^)" -eq 0 ] && [ "$(lsof /var/lib/apt/lists/lock-frontend 2>>$ERRORLOG | grep -a -c ^)" -eq 0 ] && [ "$(lsof /var/lib/apt/lists/lock-frontened 2>>$ERRORLOG | grep -a -c ^)" -eq 0 ] && [ "$(lsof /var/cache/apt/archives/lock 2>>$ERRORLOG | grep -a -c ^)" -eq 0 ] && [ "$(lsof /var/cache/apt/archives/lock-frontend 2>>$ERRORLOG | grep -a -c ^)" -eq 0 ] && [ "$(lsof /var/cache/apt/archives/lock-frontened 2>>$ERRORLOG | grep -a -c ^)" -eq 0 ] && [ "$(lsof /var/lib/dpkg/lock 2>>$ERRORLOG | grep -a -c ^)" -eq 0 ] && [ "$(lsof /var/lib/dpkg/lock-frontend 2>>$ERRORLOG | grep -a -c ^)" -eq 0 ] && [ "$(lsof /var/lib/dpkg/lock-frontened 2>>$ERRORLOG | grep -a -c ^)" -eq 0 ] ; then
# Translations
cd /
for i in guvcview pdfarranger synaptic thunar ; do
	if [ -e "/var/lib/dpkg/info/$i-extras.md5sums" ] ; then
		cat "/var/lib/dpkg/info/$i-extras.md5sums" | grep -aEi "\.mo$" | while read l ; do
			if [ "$(md5sum $(echo $l | awk '{print $2}'))" != "$l" ] ; then
				apt-get install --reinstall --no-install-recommends -y -o Dpkg::Options::="--force-confmiss" -o Dpkg::Options::="--force-confnew" $i-extras
				#echo -e "\e[1;31m"$"$i must be reinstalled.""\e[0m"
			fi
		done
	fi
done
cd - 1>/dev/null
fi

if [ "$(command -v postinst)" != "" ] ; then
	postinst # -a
fi

# Non-accurate positives that scare unnecessarily (qtwebengine is updated via backports)
#if [ "$(command -v check-support-status)" != "" ] ; then
#	check-support-status
#fi

# 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

	echo $"The update will be completed when the computer is rebooted or the next time it is turned on."

# Synaptic final result must be 0 ...
#	exit 3
#
#else
#
#	exit 0
#
fi
# ... so
exit 0
