#!/bin/sh
# Copyright (C) 2015-2022, Andrea Peluso <sya54m@gmail.com>
# Tutti i diritti riservati.
#
# ___ changelog ___
#
# 2.5.0 (23/09/2022)
# aggiunto supporto Slackware AArch64
#
# 2.4.0 (24/02/2022)
# aggiunto supporto Slackware stable
#
# 2.3.2 (16/02/2021)
# aggiornato per cambio nome di glibc-solibs
#
# 2.3.1 (25/11/2020)
# aggiunta sottodirectory input-methods in vtown
#
# 2.3.0 (05/11/2020)
# aggiunta gestione dei pacchetti KDE vtown
#
# 2.2.0 (12/12/2018)
# aggiunto supporto a slackwarearm
#
# 2.1.2 (07/04/2018)
# sostituito --caption con --title in kdialog
#
# 2.1.1 (08/05/2017)
# aggiornata selezione pacchetti rimossi
#
# 2.1.0 (01/02/2017)
# aggiunta controllo delle istanze in esecuzione
# aggiunta pulizia file temporanei in caso di interruzione
#
# 2.0.0 (30/01/2017)
# aggiunta gestione dei pacchetti KDE di alien
#
# 1.3.3 (25/01/2017)
# escluso glibc-zoneinfo dai pacchetti multilib
#
# 1.3.2 (21/05/2016)
# non disinstalla i pacchetti rimossi in current se presenti in whitelist
# aggiunto commento di esempio per KDE5
#
# 1.3.1 (10/03/2016)
# aggiunto whitelist
#
# 1.3.0 (09/03/2016)
# modificato separatore blacklist
# modificata selezione pacchetti kdei
# bugfix
#
# 1.2.2 (23/12/2015)
# migliorata rimozione vecchi pacchetti
#
# 1.2.1 (19/12/2015)
# bugfix
#
# 1.2.0 (15/12/2015)
# aggiunto supporto elilo
# bugfix
#
# 1.1.0 (22/11/2015)
# download separati nelle directory
# bugfix
#
# 1.0.0 (05/11/2015)
# prima versione

############################### Configurazione ################################
lng="it"  #codice lingua
MIRROR="http://ftp.slackware.com/pub/slackware"
#MIRROR="http://ftp.arm.slackware.com/slackwarearm"
VERSIONE="current" # current || 15.0 || 14.2
SOLOPATCH="no" # si || no
DVD="/media/SlackDVD"
tmp="/root/slackware"
KDE_testing="no"  # ktown || vtown || no
# separare con pipe: |
blacklist="kernel-source|hplip|bsd-games"
whitelist=""
###############################################################################

aiuto(){
	echo "Uso: $(basename $0)"
	echo "Procedura automatizzata per l'aggiornamento di Slackware."
	echo "Copyright (C) 2015-2022, Andrea Peluso."
	echo -e "\n  -v\tstampa le informazioni sulla versione ed esce"
	echo -e "  -h\tmostra questo aiuto ed esce"
	echo -e "\nSegnalare i bug di SlackUp a <sya54m@gmail.com>"
	echo -e "Sito web di SlackUp: <https://www.sya54m.eu/>\n"
}
if [ "$(whoami)" != "root" ]; then
	echo -e "Permesso negato: eseguire come root.\n"
	exit 1
fi
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH="" ;;
    arm*) ARCH=arm ;;
    aarch64) ARCH=aarch64 ;;
       *) ARCH="" ;;
  esac
fi
if [ -d /usr/lib64 -a $ARCH != "aarch64" ]; then
	bit="64"
else
	bit=""
fi
PRGVER="2.5.0"
DVD="$DVD/slackware$bit"
LOCKFILE="/tmp/slackup.lock"
N=1
while [ $N -le 1 ]; do
	if [ ! -f $LOCKFILE.$N ]; then
		touch $LOCKFILE.$N
		break
	fi
	N=$[$N+1]
done
trap "{ rm -f $LOCKFILE.$N ; exit 255; }" EXIT
trap "{ rm -rf $tmp ; exit 255; }" SIGINT SIGTERM
if [ $N -gt 1 ]; then
	echo -e "Il programma è già in esecuzione.\nTermino."
	exit 1
fi
while :
do
	case "$1" in
	-v)
		echo -e "$(basename $0) $PRGVER\nCopyright (C) 2015-2022, Andrea Peluso."
		exit 0
	;;
	-h)
		aiuto
		exit 0
	;;
	-*)
		aiuto
		exit 1
	;;
	*)
		break
	;;
	esac
	shift
done
mkdir -p $tmp/a $tmp/ap $tmp/d $tmp/e $tmp/f $tmp/k $tmp/kde $tmp/kdei $tmp/l $tmp/n $tmp/t $tmp/tcl $tmp/x $tmp/xap $tmp/xfce $tmp/y
cd $tmp
utente=$(who |grep ":0" |cut -d" " -f1 |uniq)
if [ "$utente" != "root" -a -n "$DISPLAY" -a -n "`ls /var/log/packages/kde-base* 2> /dev/null`" ]; then
	cp /home/$utente/.Xauthority /root/.Xauthority
	export $(dbus-launch)
fi
patches3=()
if [ "$VERSIONE" != "current" ]; then
	mkdir -p $tmp/patches
	if [ ! -r FILE_LIST-patches ] || [ -r FILE_LIST-patches -a $(expr `date +%s` - `stat -c %Z FILE_LIST-patches`) -gt 43200 ]; then
		wget $MIRROR/slackware$bit$ARCH-$VERSIONE/patches/FILE_LIST -O FILE_LIST-patches || exit 1
	fi
	if [ -z "$whitelist" ]; then
		whitelist3="riempitorechenonpuoesistere"
	else
		whitelist3="$whitelist"
	fi
	patches0=(`cat FILE_LIST-patches |egrep "./packages/" |egrep ".tgz|.txz" |egrep -v ".asc|.md5|debug" |egrep -v "$blacklist" |rev |cut -d / -f1 |rev` `cat FILE_LIST-patches |egrep "./packages/" |egrep ".tgz|.txz" |egrep -v ".asc|.md5|debug" |egrep "$whitelist3" |rev |cut -d / -f1 |rev`)
	patches=(`cat FILE_LIST-patches |egrep "./packages/" |egrep ".tgz|.txz" |egrep -v ".asc|.md5|debug" |egrep -v "$blacklist" |cut -d / -f2,3,4` `cat FILE_LIST-patches |egrep "./packages/" |egrep ".tgz|.txz" |egrep -v ".asc|.md5|debug" |egrep "$whitelist3" |cut -d / -f2,3,4`)
	patches2=(`cat FILE_LIST-patches |egrep "./packages/" |egrep ".tgz|.txz" |egrep -v ".asc|.md5|debug" |egrep -v "$blacklist" |rev |cut -d / -f1 |cut -f2- -d. |rev` `cat FILE_LIST-patches |egrep "./packages/" |egrep ".tgz|.txz" |egrep -v ".asc|.md5|debug" |egrep "$whitelist3" |rev |cut -d / -f1 |cut -f2- -d. |rev`)
	NUM=0
	NUM2=0
	patches4=()
	while [ "$NUM" -lt "${#patches2[*]}" ]; do
		if [ ! -f /var/log/packages/${patches2[$NUM]} ]; then
			patches3[$NUM2]=${patches[$NUM]}
			patches4[$NUM2]=${patches0[$NUM]}
			NUM2=`expr $NUM2 + 1`
		fi
		NUM=`expr $NUM + 1`
	done
	if [ "${#patches3[*]}" -gt "0" ]; then
		echo -e "\E[031;1mDownload dei pacchetti patches...\E[031;0m"
	fi
	POS=$(kdialog --progressbar "File totali: ${#patches3[*]}" --title "SlackUp - Patches Download" 2> /dev/null) &
	NUM=0
	while [ "$NUM" -lt "${#patches3[*]}" ]; do
		if [ ! -f $tmp/${patches3[$NUM]} ]; then
			echo "+=============================================================================="
			echo "| Download del file `expr $NUM + 1` di ${#patches3[*]}: ${patches3[$NUM]}"
			echo "+=============================================================================="
			percentuale=$(expr $NUM \* 100 / ${#patches3[*]})
			qdbus $POS Set "" value $percentuale &> /dev/null
			qdbus $POS setLabelText "Download del file `expr $NUM + 1` di ${#patches3[*]}: ${patches3[$NUM]}" &> /dev/null
			dcop $POS setProgress $percentuale &> /dev/null
			dcop $POS setLabel "Download del file `expr $NUM + 1` di ${#patches3[*]}: ${patches3[$NUM]}" &> /dev/null
			wget $MIRROR/slackware$bit$ARCH-$VERSIONE/patches/${patches3[$NUM]} -O $tmp/patches/${patches4[$NUM]} || rm $tmp/patches/${patches4[$NUM]}
		fi
		NUM=`expr $NUM + 1`
	done
	qdbus $POS close &> /dev/null
	dcop $POS close &> /dev/null
fi
multilib3=()
if [ "$bit" == "64" -a -n "`ls /var/log/packages/*_multilib-x86_64-* 2> /dev/null`" ]; then
	if [ ! -r FILELIST-multilib.TXT ] || [ -r FILELIST-multilib.TXT -a $(expr `date +%s` - `stat -c %Z FILELIST-multilib.TXT`) -gt 43200 ]; then
		wget http://www.slackware.com/~alien/multilib/$VERSIONE/FILELIST.TXT -O FILELIST-multilib.TXT || exit 1
	fi
	multilib=(`cat FILELIST-multilib.TXT |egrep "./gcc-|./*glibc-" |egrep ".tgz|.txz" |egrep -v ".asc|.md5|debug" |cut -d / -f2`)
	multilib2=(`cat FILELIST-multilib.TXT |egrep "./gcc-|./*glibc-" |egrep ".tgz|.txz" |egrep -v ".asc|.md5|debug" |cut -d / -f2 |rev |cut -f2- -d. |rev`)
	NUM=0
	NUM2=0
	while [ "$NUM" -lt "${#multilib2[*]}" ]; do
		if [ ! -f /var/log/packages/${multilib2[$NUM]} ]; then
			multilib3[$NUM2]=${multilib[$NUM]}
			NUM2=`expr $NUM2 + 1`
		fi
		NUM=`expr $NUM + 1`
	done
	if [ "${#multilib3[*]}" -gt "0" ]; then
		echo -e "\E[031;1mDownload dei pacchetti multilib di Alien...\E[031;0m"
	fi
	POS=$(kdialog --progressbar "File totali: ${#multilib3[*]}" --title "SlackUp - Multilib Download" 2> /dev/null) &
	NUM=0
	while [ "$NUM" -lt "${#multilib3[*]}" ]; do
		if [ ! -f $tmp/?/${multilib3[$NUM]} ]; then
			if [ -n "`echo ${multilib3[$NUM]} |grep "solibs" 2> /dev/null`" ]; then
				dir="a"
			elif [ -n "`echo ${multilib3[$NUM]} |grep "glibc" 2> /dev/null`" ]; then
				dir="l"
			elif [ -n "`echo ${multilib3[$NUM]} |grep "gcc" 2> /dev/null`" ]; then
				dir="d"
			fi
			echo "+=============================================================================="
			echo "| Download del file `expr $NUM + 1` di ${#multilib3[*]}: $dir/${multilib3[$NUM]}"
			echo "+=============================================================================="
			percentuale=$(expr $NUM \* 100 / ${#multilib3[*]})
			qdbus $POS Set "" value $percentuale &> /dev/null
			qdbus $POS setLabelText "Download del file `expr $NUM + 1` di ${#multilib3[*]}: $dir/${multilib3[$NUM]}" &> /dev/null
			dcop $POS setProgress $percentuale &> /dev/null
			dcop $POS setLabel "Download del file `expr $NUM + 1` di ${#multilib3[*]}: $dir/${multilib3[$NUM]}" &> /dev/null
			wget http://www.slackware.com/~alien/multilib/$VERSIONE/${multilib3[$NUM]} -O $tmp/$dir/${multilib3[$NUM]} || rm $tmp/$dir/${multilib3[$NUM]}
		fi
		NUM=`expr $NUM + 1`
	done
	qdbus $POS close &> /dev/null
	dcop $POS close &> /dev/null
fi
kdealien3=()
if [ "$KDE_testing" == "ktown" -o "$KDE_testing" == "vtown" ] && [ "$ARCH" != "arm" ]; then
	blacklist2="kde|akonadi|dbus-1|attica-|eigen3-|grantlee-|libdbusmenu-qt-|phonon-4|polkit-0|polkit-kde-agent-1|polkit-qt|scim-|SDL2|qt5|xfce"
	if [ -z "$blacklist" ]; then
		blacklist="$blacklist2"
	else
		blacklist="$blacklist|$blacklist2"
	fi
	whitelist2="kwallet-|calligra-l10n-$lng"
	if [ -z "$whitelist" ]; then
		whitelist="$whitelist2"
	else
		whitelist="$whitelist|$whitelist2"
	fi
	if [ "$bit" == "64" ]; then
		bit2="_64"
	else
		bit2=""
	fi
	if [ ! -r FILE_LIST_KDE ] || [ -r FILE_LIST_KDE -a $(expr `date +%s` - `stat -c %Z FILE_LIST_KDE`) -gt 43200 ]; then
		if [ "$KDE_testing" == "ktown" ]; then
			wget http://alien.slackbook.org/slackware/ktown/$VERSIONE/latest/x86$bit2/FILELIST.TXT -O FILE_LIST_KDE || exit 1
		elif [ "$KDE_testing" == "vtown" ]; then
			wget $MIRROR/slackware$bit-$VERSIONE/testing/FILE_LIST -O FILE_LIST_KDE || exit 1
		fi
	fi
	if [ "$KDE_testing" == "ktown" ]; then
		kdealien=(`cat FILE_LIST_KDE |egrep ".tgz|.txz" |egrep -v ".asc|.md5" |awk '!(/.\/kdei\// && !/l10n-'$lng'-/)' |cut -d / -f2,3,4`)
		kdealien2=(`cat FILE_LIST_KDE |egrep ".tgz|.txz" |egrep -v ".asc|.md5" |awk '!(/.\/kdei\// && !/l10n-'$lng'-/)' |rev |cut -d / -f1 |cut -f2- -d. |rev`)
	elif [ "$KDE_testing" == "vtown" ]; then
		kdealien=(`cat FILE_LIST_KDE |egrep "vtown" |egrep ".tgz|.txz" |egrep -v ".asc|.md5" |awk '!(/.\/kdei\// && !/l10n-'$lng'-/)' |cut -d / -f2,3,4,5`)
		kdealien2=(`cat FILE_LIST_KDE |egrep "vtown" |egrep ".tgz|.txz" |egrep -v ".asc|.md5" |awk '!(/.\/kdei\// && !/l10n-'$lng'-/)' |rev |cut -d / -f1 |cut -f2- -d. |rev`)
	fi
	NUM=0
	NUM2=0
	while [ "$NUM" -lt "${#kdealien2[*]}" ]; do
		if [ ! -f /var/log/packages/${kdealien2[$NUM]} ]; then
			kdealien3[$NUM2]=${kdealien[$NUM]}
			NUM2=`expr $NUM2 + 1`
		fi
		NUM=`expr $NUM + 1`
	done
	if [ "${#kdealien3[*]}" -gt "0" ]; then
		echo -e "\E[031;1mDownload dei pacchetti KDE...\E[031;0m"
	fi
	POS=$(kdialog --progressbar "File totali: ${#kdealien3[*]}" --title "SlackUp - KDE Alien Download" 2> /dev/null) &
	NUM=0
	if [ "$KDE_testing" == "ktown" ]; then
		mkdir -p $tmp/testing/deps/telepathy $tmp/testing/kde/applications $tmp/testing/kde/applications-extra $tmp/testing/kde/frameworks $tmp/testing/kde/kde4 $tmp/testing/kde/kde4-extragear $tmp/testing/kde/kdepim $tmp/testing/kde/plasma $tmp/testing/kde/plasma-extra $tmp/testing/kde/telepathy $tmp/testing/kdei
	elif [ "$KDE_testing" == "vtown" ]; then
		mkdir -p $tmp/testing/packages/vtown/deps $tmp/testing/packages/vtown/kde $tmp/testing/packages/vtown/input-methods $tmp/testing/packages/vtown/xfce
	fi
	while [ "$NUM" -lt "${#kdealien3[*]}" ]; do
		if [ ! -f $tmp/testing/${kdealien3[$NUM]} ]; then
			echo "+=============================================================================="
			echo "| Download del file `expr $NUM + 1` di ${#kdealien3[*]}: ${kdealien3[$NUM]}"
			echo "+=============================================================================="
			percentuale=$(expr $NUM \* 100 / ${#kdealien3[*]})
			qdbus $POS Set "" value $percentuale &> /dev/null
			qdbus $POS setLabelText "Download del file `expr $NUM + 1` di ${#kdealien3[*]}: ${kdealien3[$NUM]}" &> /dev/null
			dcop $POS setProgress $percentuale &> /dev/null
			dcop $POS setLabel "Download del file `expr $NUM + 1` di ${#kdealien3[*]}: ${kdealien3[$NUM]}" &> /dev/null
			if [ "$KDE_testing" == "ktown" ]; then
				wget http://alien.slackbook.org/slackware/ktown/$VERSIONE/latest/x86$bit2/${kdealien3[$NUM]} -O $tmp/testing/${kdealien3[$NUM]} || rm $tmp/testing/${kdealien3[$NUM]}
			elif [ "$KDE_testing" == "vtown" ]; then
				wget $MIRROR/slackware$bit-$VERSIONE/testing/${kdealien3[$NUM]} -O $tmp/testing/${kdealien3[$NUM]} || rm $tmp/testing/${kdealien3[$NUM]}
			fi
		fi
		NUM=`expr $NUM + 1`
	done
	qdbus $POS close &> /dev/null
	dcop $POS close &> /dev/null
fi
if [ -z "$blacklist" ]; then
	blacklist="riempitorechenonpuoesistere"
fi
if [ -z "$whitelist" ]; then
	whitelist="riempitorechenonpuoesistere"
fi
if [ ! -r FILE_LIST ] || [ -r FILE_LIST -a $(expr `date +%s` - `stat -c %Z FILE_LIST`) -gt 43200 ]; then
	wget $MIRROR/slackware$bit$ARCH-$VERSIONE/slackware$bit/FILE_LIST -O FILE_LIST || exit 1
fi
if [ "$SOLOPATCH" == "no" ]; then
if [ ! -r CHANGES_AND_HINTS.TXT ] || [ -r CHANGES_AND_HINTS.TXT -a $(expr `date +%s` - `stat -c %Z CHANGES_AND_HINTS.TXT`) -gt 43200 ]; then
	wget $MIRROR/slackware$bit$ARCH-$VERSIONE/CHANGES_AND_HINTS.TXT -O CHANGES_AND_HINTS.TXT
fi
fi
FILE=(`cat FILE_LIST |egrep ".tgz|.txz" |egrep -v ".asc|.md5" |awk '!(/.\/kdei\// && !/l10n-'$lng'-/)' |egrep -v "$blacklist" |cut -d / -f2,3` `cat FILE_LIST |egrep ".tgz|.txz" |egrep -v ".asc|.md5" |egrep "$whitelist" |cut -d / -f2,3`)
FILE2=(`cat FILE_LIST |egrep ".tgz|.txz" |egrep -v ".asc|.md5" |awk '!(/.\/kdei\// && !/l10n-'$lng'-/)' |egrep -v "$blacklist" |cut -d / -f3 |rev |cut -f2- -d. |rev` `cat FILE_LIST |egrep ".tgz|.txz" |egrep -v ".asc|.md5" |egrep "$whitelist" |cut -d / -f3 |rev |cut -f2- -d. |rev`)
echo -e "\E[031;1mControllo gli aggiornamenti...\E[031;0m"
NUM=0
NUM2=0
while [ "$NUM" -lt "${#FILE2[*]}" ]; do
	if [ ! -f /var/log/packages/${FILE2[$NUM]} ]; then
		if [ ! -f FILELIST-multilib.TXT ] || [[ -f FILELIST-multilib.TXT && ${FILE2[$NUM]} != "gcc-"* && ${FILE2[$NUM]} != *"glibc-"* ]] || [[ ${FILE2[$NUM]} == "glibc-zoneinfo-"* ]]; then
			FILE3[$NUM2]=${FILE[$NUM]}
			NUM2=`expr $NUM2 + 1`
		fi
	fi
	NUM=`expr $NUM + 1`
done
if [ "${#FILE3[*]}" -eq "0" -a "${#multilib3[*]}" -eq "0" -a "${#kdealien3[*]}" -eq "0" -a "${#patches3[*]}" -eq "0" ] || [ "${#patches3[*]}" -eq "0" -a "$SOLOPATCH" == "si" -a "${#multilib3[*]}" -eq "0" -a "${#kdealien3[*]}" -eq "0" ]; then
	rm -rf $tmp
	echo -e "\E[031;1mIl sistema è già aggiornato.\E[031;0m"
	exit 0
fi
if [ "$SOLOPATCH" == "no" ]; then
if [ "${#FILE3[*]}" -eq "0" ]; then
	echo -e "\E[031;1mDownload dei pacchetti ufficiali...\E[031;0m"
fi
POS=$(kdialog --progressbar "File totali: ${#FILE3[*]}" --title "SlackUp - PKG Ufficiali Download" 2> /dev/null) &
NUM=0
while [ "$NUM" -lt "${#FILE3[*]}" ]; do
	if [ ! -f $tmp/${FILE3[$NUM]} ]; then
		percentuale=$(expr $NUM \* 100 / ${#FILE3[*]})
		qdbus $POS Set "" value $percentuale &> /dev/null
		dcop $POS setProgress $percentuale &> /dev/null
		if [ -r $DVD/${FILE3[$NUM]} ]; then
			echo "+=============================================================================="
			echo "| Copia del file `expr $NUM + 1` di ${#FILE3[*]}"
			echo "+=============================================================================="
			qdbus $POS setLabelText "Copia del file `expr $NUM + 1` di ${#FILE3[*]}: ${FILE3[$NUM]}" &> /dev/null
			dcop $POS setLabel "Copia del file `expr $NUM + 1` di ${#FILE3[*]}: ${FILE3[$NUM]}" &> /dev/null
			cp $DVD/${FILE3[$NUM]} $tmp/${FILE3[$NUM]} || rm $tmp/${FILE3[$NUM]}
		else
			echo "+=============================================================================="
			echo "| Download del file `expr $NUM + 1` di ${#FILE3[*]}: ${FILE3[$NUM]}"
			echo "+=============================================================================="
			qdbus $POS setLabelText "Download del file `expr $NUM + 1` di ${#FILE3[*]}: ${FILE3[$NUM]}" &> /dev/null
			dcop $POS setLabel "Download del file `expr $NUM + 1` di ${#FILE3[*]}: ${FILE3[$NUM]}" &> /dev/null
			wget $MIRROR/slackware$bit$ARCH-$VERSIONE/slackware$bit/${FILE3[$NUM]} -O $tmp/${FILE3[$NUM]} || rm $tmp/${FILE3[$NUM]}
		fi
	fi
	NUM=`expr $NUM + 1`
done
qdbus $POS close &> /dev/null
dcop $POS close &> /dev/null
fi
echo -e "\E[031;1mDownload completato. Aggiornamento in corso...\E[031;0m"
kdialog --passivepopup "Download completato. Aggiornamento in corso..." --title SlackUp 2> /dev/null &
runlevel=$(runlevel |cut -d" " -f2)
if [ "$runlevel" -gt "1" ] && [ -n "`ls $tmp/a/*glibc-solibs-*.t?z 2> /dev/null`" ]; then
	echo -e "\E[031;1mRieseguire in runlevel 1.\E[031;0m"
	kdialog --msgbox "Rieseguire in runlevel 1." --title SlackUp 2> /dev/null
	exit 1
fi
if [ "$runlevel" -gt "3" ] && [ -n "`ls $tmp/x/xorg-server-*.t?z 2> /dev/null`" -o -n "`ls $tmp/x/mesa-*.t?z 2> /dev/null`" ]; then
	echo -e "\E[031;1mRieseguire in runlevel 3.\E[031;0m"
	kdialog --msgbox "Rieseguire in runlevel 3." --title SlackUp 2> /dev/null
	exit 1
fi
if [ -n "`ls $tmp/a/*glibc-solibs-*.t?z 2> /dev/null`" ]; then
	upgradepkg $tmp/a/*glibc-solibs-*.t?z
fi
if [ -n "`ls $tmp/a/pkgtools-*.t?z 2> /dev/null`" ]; then
	upgradepkg $tmp/a/pkgtools-*.t?z
fi
if [ -n "`ls $tmp/a/tar-*.t?z 2> /dev/null`" ]; then
	upgradepkg $tmp/a/tar-*.t?z
fi
if [ -n "`ls $tmp/a/xz-*.t?z 2> /dev/null`" ]; then
	upgradepkg $tmp/a/xz-*.t?z
fi
if [ -n "`ls $tmp/a/findutils-*.t?z 2> /dev/null`" ]; then
	upgradepkg $tmp/a/findutils-*.t?z
fi
if [ "${#FILE3[*]}" -gt "0" -o "${#multilib3[*]}" -gt "0" ] && [ "$SOLOPATCH" == "no" ]; then
	upgradepkg --install-new --terse $tmp/*/*.t?z
fi
if [ "${#patches3[*]}" -gt "0" ]; then
	upgradepkg --terse $tmp/patches/*.t?z
fi
if [ "$KDE_testing" == "ktown" -o "$KDE_testing" == "vtown" ] && [ "${#kdealien3[*]}" -gt "0" ]; then
	upgradepkg --install-new --terse $tmp/testing/*/*.t?z $tmp/testing/*/*/*.t?z $tmp/testing/*/*/*/*.t?z
fi
if [ "$SOLOPATCH" == "no" ]; then
rimossi=(`awk '/*** OTHER NOTABLE CHANGES AND HINTS ***/{found=0} {if(found) print} /*** PACKAGE REMOVALS SINCE/{found=1}' CHANGES_AND_HINTS.TXT |grep "^[a-z]/" |cut -d/ -f2 |cut -d" " -f1 |tr -d "\r" |grep -v "^$"`)
NUM=0
while [ "$NUM" -lt "${#rimossi[*]}" ]; do
	if [ -n "`ls /var/log/packages/${rimossi[$NUM]}-[0-9]*-[i-x]*-[1-9]* 2> /dev/null`" -a -z "`echo ${rimossi[$NUM]}- |egrep "$whitelist"`" ]; then
		removepkg --terse ${rimossi[$NUM]}
	fi
	NUM=`expr $NUM + 1`
done
fi
cd /etc/rc.d
find . -name "*.new" | while read configfile ; do
	#if [ ! "$configfile" = "./rc.inet1.conf.new" -a ! "$configfile" = "./rc.local.new" -a ! "$configfile" = "./rc.M.new" ]; then
	if [ ! "$configfile" = "./rc.inet1.conf.new" -a ! "$configfile" = "./rc.local.new" ]; then
		cp -a $(echo $configfile | rev | cut -f 2- -d . | rev) \
		$(echo $configfile | rev | cut -f 2- -d . | rev).bak 2> /dev/null
		mv $configfile $(echo $configfile | rev | cut -f 2- -d . | rev)
	fi
done
mv /usr/share/vim/vimrc.new /usr/share/vim/vimrc 2> /dev/null
if [ -n "`ls $tmp/a/kernel-huge-*.t?z 2> /dev/null`" -a -z "`ls /boot/vmlinuz-* |egrep -v "huge|generic" 2> /dev/null`" ]; then
	DIS=$(cat /etc/mtab |grep " / " |cut -d" " -f1 |sed s/.$//)
	MBR=$(dd if=$DIS bs=446 count=1 2> /dev/null |strings)
	CURVER=$(uname -r |cut -d- -f1)
	VER=$(ls $tmp/a/kernel-huge-?.*.t?z |cut -d- -f3)
	if [ "`zcat /proc/config.gz |grep "CONFIG_EXT4_FS=" |cut -d= -f2`" == "m" ]; then
		if [ "`zcat /proc/config.gz |grep "CONFIG_LOCALVERSION=" |cut -d= -f2`" == "\"-smp\"" -a -z "$bit" ]; then
			/usr/share/mkinitrd/mkinitrd_command_generator.sh -k $VER-smp
		else
			/usr/share/mkinitrd/mkinitrd_command_generator.sh -k $VER
		fi
	fi
	controllo="0"
	if [ -n "`echo $MBR |grep -i grub`" ]; then
		sed -i "s|$CURVER|$VER|g" /boot/grub/grub.cfg
	elif [ -n "`echo $MBR |grep -i elilo`" ]; then
		sed -i "s|$CURVER|$VER|g" /etc/elilo.conf
		/sbin/elilo --efiboot
	elif [ -n "`echo $MBR |grep -i lilo`" ]; then
		sed -i "s|$CURVER|$VER|g" /etc/lilo.conf
		/sbin/lilo
	else
		controllo="1"
		echo -e "\E[031;1mErrore: Bootloader non aggiornato.\E[031;0m"
		kdialog --error "Errore: Bootloader non aggiornato." --title SlackUp 2> /dev/null
	fi
	if [ "$controllo" -eq "0" ]; then
		echo -e "\E[031;1mBootloader aggiornato.\E[031;0m"
		kdialog --passivepopup "Bootloader aggiornato." --title SlackUp 2> /dev/null
	fi
fi
rm -rf $tmp $LOCKFILE.$N
echo -e "\E[031;1mAggiornamento completato.\E[031;0m"
kdialog --passivepopup "Aggiornamento completato." --title SlackUp 2> /dev/null &
