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

# apt-problem
#
# Fix problems 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: apt, bash, coreutils, dpkg, gawk | mawk | original-awk, grep


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

CNAME="apt-problem"
VERSION="1.3.1"
TITLE="'apt-problem'"
ICON="/usr/share/icons/apt-problem.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=$"Fix problems installing packages."
LDESCRIP=$"$TITLE is a tool to fix problems 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


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

ping -c 3 ftp.fau.de 1>/dev/null 2>&1 || ping -c 3 mirrorservice.org 1>/dev/null 2>&1 || ping -c 3 ftp.ch.debian.org 1>/dev/null 2>&1

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


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

# https://manpages.debian.org/signal.7#Signal_numbering_for_standard_signals
for i in synaptic apt apt-get dpkg packagekit* ; do pkill -15 ^$i$ ; sleep 1 ; pkill -9 ^$i$ ; sleep 1 ; done

rm -f /var/lib/apt/lists/lock

rm -f /var/lib/apt/lists/lock-frontend

rm -f /var/lib/apt/lists/lock-frontened

rm -f /var/cache/apt/archives/lock

rm -f /var/cache/apt/archives/lock-frontend

rm -f /var/cache/apt/archives/lock-frontened

rm -f /var/lib/dpkg/lock

rm -f /var/lib/dpkg/lock-frontend

rm -f /var/lib/dpkg/lock-frontened

find "$TFS"/var/lib/apt/lists -type f | grep -aEiv "lock$" | while read i ; do rm -f "$i" ; done
if [ -e "$TFS/var/cache/apt-xapian-index/" ] ; then find "$TFS"/var/cache/apt-xapian-index/ -type f | while read i ; do rm -rf $i ; done ; fi
find "$TFS"/var/cache/apt/  -maxdepth 1 -type f | grep -aEi "pkgcache|srcpkgcache" | while read i ; do rm -rf $i ; done

apt-get --allow-releaseinfo-change update -y

apt-get update -y

apt-get -f install -y

dpkg --configure -a

for i in $(dpkg -l | grep -aEi "^i" | grep -aEiv "^ii" | awk '{print $2}' | grep -aEi "[a-z]") ; do apt-get purge -y $i ; done

for i in $(dpkg -l | awk '{if ($1 ~ /.F|.H|..r|..R/) print $2}' | grep -aEi "[a-z]") ; do dpkg --force-all --purge $i ; done

for i in $(dpkg -l | grep -aEi "^i" | grep -aEiv "^ii" | awk '{print $2}' | grep -aEi "[a-z]") ; do apt-get purge -y $i ; done

dpkg --configure -a

apt-get -f install -y

dpkg --configure -a

# /var/lib/dpkg/info/


### CHECK FDM ########################################

#FDS=$(df -m | grep -a /dev/ | grep -aEi " /$" | sort -n | head -n 1 | awk '{print $4}')
FDS=$(df -m /               | grep -aEi "^/" | awk '{print $4}')
FDP=$(df -m /var/cache/apt/ | grep -aEi "^/" | awk '{print $4}')
FDM=$(printf "$FDS\n$FDP\n" | sort -n | head -n 1)

if [ "$FDM" -lt 250 ] ; then
	echo " "
	echo $"WARNING: VERY little free space on the disk ($FDM MiB)."
	read -t 10 SEL
elif [ "$FDM" -lt 1000 ] ; then
	echo " "
	echo $"WARNING: Little free space on the disk ($FDM MiB)."
	read -t 10 SEL
else
	true
fi


### FINAL TEST ########################################

if [ "$TESTINTERNET" = "Y" ] ; then
	apt-get install -s dpkg
	if [ "$?" -ne 0 ] ; then
		echo " "
		echo $"WARNING: The problem to install packages seems to continue."
		read -t 10 SEL
		exit 1
	fi
fi

exit 0
