#!/bin/bash
#
# Autoupdater Debian Linux und Ubuntu
# Autor:	Matthias Soika
# Date:		28.10.2015 | 02.12.2018 | 10.09.2019 | 01.12.2021 | 28.12.2021 | 12.08.2022 | 25.09.2022
# Version:	1.72
# Mail:		info@info-linux.de

# Installation 
# 	wget -c -O /usr/sbin/system-update.sh https://ftp.info-linux.de/arbeiten/Linux/system-update.sh && chmod 755 /usr/sbin/system-update.sh
## Update the Skript:
# 	if [ -f /usr/sbin/system-update.sh ]; then rm -f /usr/sbin/system-update.sh; fi && wget -c --progress=dot -O /usr/sbin/system-update.sh https://ftp.info-linux.de/arbeiten/Linux/system-update.sh && chmod 755 /usr/sbin/system-update.sh

NORMAL='\033[0;39m'
BLUE='\033[1;34m'
GREEN='\033[1;32m'
RED='\033[1;31m'
YELLOW='\033[1;33m'

case $1 in
	update)
 	if [ -f /usr/sbin/system-update.sh ]; then rm -f /usr/sbin/system-update.sh; fi && wget -c --progress=dot -O /usr/sbin/system-update.sh https://ftp.info-linux.de/arbeiten/Linux/system-update.sh && chmod 755 /usr/sbin/system-update.sh
	exit 0;
	;;
esac

function replaceDefaultsVim {
  for ((i=80; i<=100; i++)); do
    if [ -d /usr/share/vim/vim$i ]; then
      if [ ! -L /usr/share/vim/vim$i/defaults.vim ]; then
        if [ -f /usr/share/vim/vim$i/defaults.vim ]; then mv -f /usr/share/vim/vim$i/defaults.vim /usr/share/vim/vim$i/defaults.vim.bak; fi
        if [ -e /etc/vim/vimrc ]; then ln -s /etc/vim/vimrc /usr/share/vim/vim$i/defaults.vim; fi
      fi
        if [ "$(readlink /usr/share/vim/vim$i/defaults.vim)" != "/etc/vim/vimrc" ]; then
          rm -f /usr/share/vim/vim$i/defaults.vim
          ln -s /etc/vim/vimrc /usr/share/vim/vim$i/defaults.vim
        fi
     fi
  done
}

function dpkgUninstallMarkedPackages {
	dpkgResult=$(dpkg --get-selections | grep deinstall | cut -f1 | wc -l)
	if [ $dpkgResult -gt 0 ];then
		echo
		dpkg --purge $(dpkg --get-selections | grep deinstall | cut -f1)
		echo
	fi
}

if [ ! -z $(which apt) ]; then
	AG=$(which apt)
	listUpgradable=1
else
	if [ ! -z $(which apt-get) ]; then
		AG=$(which apt-get)
		listUpgradable=0
	else
		echo "No Package Manager found, something went wrong. Bye"
		exit 1;
	fi
fi

echo
echo "	------------ Pakete Aktualisieren -------------"
$AG update
echo 
echo "	==============================================="
if [ $listUpgradable -eq 1 ]; then
	echo "	------------ Aktualisierungen auflisten -------"
	$AG list --upgradable	
	echo "	==============================================="
fi
echo "	------------ System Aktualisien ---------------"
$AG upgrade -y
echo 
echo "	==============================================="
echo "	------------ Distributions Aktualisierung -----"
$AG dist-upgrade -y
echo 
echo "	==============================================="
echo "	------------ Löschen überflüssiger Pakete -----"
$AG autoremove -y
echo 
echo "	==============================================="
echo "	------------ Löschen überflüssiger Pakete -----"
$AG autoclean -y
echo 
echo "	==============================================="
echo

piHole=$(which pihole)
if [ ! -z $piHole ]; then
	$piHole -up
fi

dpkgUninstallMarkedPackages
replaceDefaultsVim

