#!/bin/bash

# Ldeb (C) 2011 Jesús Hernández Gormaz
# This program is free software; you can redistribute it and/or modify it under
#  the terms of the GNU General Public License as published by the Free Software
#  Foundation; either version 2 of the License, or (at your option) any later
#  version. This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
#  details. You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.

OLDCONF=$(dpkg -l|grep "^rc"|awk '{print $2}')
CURKERNEL=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
LINUXPKG="linux-(image|headers|ubuntu-modules|restricted-modules)"
METALINUXPKG="linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)"
OLDKERNELS=$(dpkg -l|awk '{print $2}'|grep -E $LINUXPKG |grep -vE $METALINUXPKG|grep -v $CURKERNEL)
YELLOW="\033[1;33m"
RED="\033[0;31m"
ENDCOLOR="\033[0m"

if [ $USER != root ]; then
  echo -e $RED"Error: necesitas ser root"
  sudo $0 $1
  exit 0
fi

if [ $1 == "update" ]; then
  echo -e $YELLOW"Descargando la ultima version del script desde internet..."$ENDCOLOR
  wget -O $0 -t 2 -N --no-cache --referer=https://sourceforge.net/projects/ldeb/files/Ldeb.sh/download --max-redirect 2 -U Ldeb/Wget https://downloads.sourceforge.net/project/ldeb/Ldeb.sh
  echo -e $YELLOW"Ejecutando el script actualizado..."$ENDCOLOR
  sudo $0
  exit 0
fi

echo -e $RED"Es necesario cerrar algunos programas para la ejecucion de este script"$ENDCOLOR
echo -e $YELLOW"Tienes 10 segundos para cerrar los siguientes programas:"$ENDCOLOR
echo -e $RED" - Synaptic"$ENDCOLOR
echo -e $RED" - Aptitude"$ENDCOLOR
echo -e $RED" - Apt-get"$ENDCOLOR
echo -e $RED" - OpenOffice"$ENDCOLOR
echo -e $RED" - Firefox"$ENDCOLOR
echo -e $RED" - Evolution"$ENDCOLOR
echo -e $RED" - aMSN"$ENDCOLOR
echo -e $YELLOW"Despues se forzara el cierre y comenzara la ejecucion del script"$ENDCOLOR
sleep 10

echo -e $YELLOW"Cerrando programas para poder trabajar correctamente..."$ENDCOLOR
pkill -9 synaptic
pkill -9 aptitude
pkill -9 apt-get
pkill -9 evolution
pkill -9 amsn
pkill -9 wish8.5
pkill -9 firefox
pkill -9 soffice.bin
sleep 5

echo -e $YELLOW"Limpiando cache apt..."$ENDCOLOR
aptitude -fyq autoclean
aptitude -fyq clean

echo -e $YELLOW"Actualizando la lista de paquetes..."$ENDCOLOR
aptitude -fyq update

echo -e $YELLOW"Comprobando que no existen dependencias incumplidas..."$ENDCOLOR
apt-get -fy -qq check

echo -e $YELLOW"Actualizando paquetes instalados..."$ENDCOLOR
aptitude -fyq safe-upgrade

echo -e $YELLOW"Limpiando cache apt..."$ENDCOLOR
aptitude -fyq autoclean
aptitude -fyq clean

echo -e $YELLOW"Eliminando archivos de configuracion antiguos..."$ENDCOLOR
aptitude -fyq purge $OLDCONF

echo -e $YELLOW"Eliminando nucleos antiguos..."$ENDCOLOR
aptitude -fyq purge $OLDKERNELS

echo -e $YELLOW"Eliminando archivos de respaldo antiguos..."$ENDCOLOR
find /home/ -type f -name "*~" -atime +7 -exec rm -rf {} \;
find /home/ -type f -name "#*" -atime +7 -exec rm -rf {} \;
find /media/ -type f -name "*~" -atime +7 -exec rm -rf {} \;
find /media/ -type f -name "#*" -atime +7 -exec rm -rf {} \;
find /root/ -type f -name "*~" -atime +1 -exec rm -rf {} \;
find /root/ -type f -name "#*" -atime +1 -exec rm -rf {} \;

echo -e $YELLOW"Eliminando archivos de codigo objeto..."$ENDCOLOR
find /home/ -type f -name "*.o" -atime +2 -exec rm -rf {} \;
find /media/ -type f -name "*.o" -atime +2 -exec rm -rf {} \;
find /root/ -type f -name "*.o" -exec rm -rf {} \;

echo -e $YELLOW"Eliminando archivos de log..."$ENDCOLOR
find /home/ -type f -name "*.log" -atime +30 -exec shred -fzu -n=7 {} \;
find /media/ -type f -name "*.log" -atime +30 -exec shred -fzu -n=7 {} \;
find /root/ -type f -name "*.log" -atime +90 -exec shred -fzu -n=7 {} \;

echo -e $YELLOW"Eliminando cache de usuario..."$ENDCOLOR
find /home/*/.cache/ -type f -name "*" -atime +1 -exec shred -fzu -n=7 {} \;
find /home/*/.cache/*/ -type d -name "*" -atime +1 -exec rm -rf {} \;

echo -e $YELLOW"Eliminando cache de Firefox..."$ENDCOLOR
find /home/*/.mozilla/firefox/*.*/Cache/ -type f -name "*" -atime +7 -exec shred -fzu -n=7 {} \;
find /home/*/.mozilla/firefox/*.*/cache/*/ -type d -name "*" -atime +7 -exec rm -rf {} \;

echo -e $YELLOW"Eliminando archivos temporales de OpenOffice..."$ENDCOLOR
find /home/*/.openoffice.org/*/user/*/cache/ -type f -name "*" -atime +2 -exec shred -fzu -n=7 {} \;
find /home/*/.openoffice.org/*/user/*/cache/*/ -type d -name "*" -atime +2 -exec rm -rf {} \;

echo -e $YELLOW"Eliminando cache de OpenOffice..."$ENDCOLOR
find /home/*/.openoffice.org/*/user/temp/ -type f -name "*" -atime +1 -exec shred -fzu -n=7 {} \;
find /home/*/.openoffice.org/*/user/temp/*/ -type d -name "*" -atime +1 -exec rm -rf {} \;

echo -e $YELLOW"Eliminando respaldos de OpenOffice..."$ENDCOLOR
find /home/*/.openoffice.org/*/user/backup/ -type f -name "*" -atime +1 -exec shred -fzu -n=7 {} \;

echo -e $YELLOW"Eliminando cache e imap de Evolution..."$ENDCOLOR
find /home/*/.evolution/cache/ -type f -name "*" -atime +7 -exec shred -fzu -n=7 {} \;
find /home/*/.evolution/cache/*/ -type d -name "*" -atime +7 -exec rm -rf {} \;
find /home/*/.evolution/mail/imap/ -type f -name "*" -atime +14 -exec shred -fzu -n=7 {} \;
find /home/*/.evolution/mail/imap/*/ -type d -name "*" -atime +14 -exec rm -rf {} \;
find /home/*/.evolution/mail/views/ -type f -name "*" -atime +30 -exec shred -fzu -n=7 {} \;
find /home/*/.evolution/mail/views/*/ -type d -name "*" -atime +30 -exec rm -rf {} \;
find /home/*/.evolution/mail/config/ -type f -name "*" -atime +90 -exec shred -fzu -n=7 {} \;
find /home/*/.evolution/mail/config/*/ -type d -name "*" -atime +90 -exec rm -rf {} \;

echo -e $YELLOW"Eliminando cache de imagenes de conctactos de aMSN..."$ENDCOLOR
find /home/*/.amsn/displaypic/cache/ -type f -name "*" -atime +1 -exec rm -rf {} \;
find /home/*/.amsn/displaypic/cache/*/ -type d -name "*" -atime +1 -exec rm -rf {} \;
find /home/*/.amsn/*_*/displaypic/cache/ -type f -name "*" -atime +2 -exec rm -rf {} \;
find /home/*/.amsn/*_*/displaypic/cache/*/ -type d -name "*" -atime +2 -exec rm -rf {} \;

echo -e $YELLOW"Eliminando cache de emoticonos de conctactos de aMSN..."$ENDCOLOR
find /home/*/.amsn/smileys/cache/ -type f -name "*" -atime +1 -exec rm -rf {} \;
find /home/*/.amsn/*_*/smileys/cache/ -type f -name "*" -atime +7 -exec rm -rf {} \;

echo -e $YELLOW"Eliminando historiales de conversaciones de aMSN..."$ENDCOLOR
find /home/*/.amsn/logs/ -type f -name "*" -atime +30 -exec shred -fzu -n=7 {} \;
find /home/*/.amsn/logs/*/ -type d -name "*" -atime +30 -exec rm -rf {} \;
find /home/*/.amsn/*_*/logs/ -type f -name "*" -atime +60 -exec shred -fzu -n=7 {} \;
find /home/*/.amsn/*_*/logs/*/ -type d -name "*" -atime +60 -exec rm -rf {} \;

echo -e $YELLOW"Eliminando otros archivos innecesarios de aMSN..."$ENDCOLOR
find /home/*/.amsn/FT/cache/ -type f -name "*" -atime +1 -exec shred -fzu -n=7 {} \;
find /home/*/.amsn/FT/cache/*/ -type d -name "*" -atime +1 -exec rm -rf {} \;
find /home/*/.amsn/*_*/FT/cache/ -type f -name "*" -atime +1 -exec shred -fzu -n=7 {} \;
find /home/*/.amsn/*_*/FT/cache/*/ -type d -name "*" -atime +1 -exec rm -rf {} \;
find /home/*/.amsn/winks/cache/ -type f -name "*" -atime +1 -exec rm -rf {} \;
find /home/*/.amsn/winks/cache/*/ -type d -name "*" -atime +1 -exec rm -rf {} \;
find /home/*/.amsn/*_*/winks/cache/ -type f -name "*" -atime +1 -exec rm -rf {} \;
find /home/*/.amsn/*_*/winks/cache/*/ -type d -name "*" -atime +1 -exec rm -rf {} \;
find /home/*/.amsn/webcam/ -type f -name "*" -atime +7 -exec shred -fzu -n=7 {} \;
find /home/*/.amsn/webcam/*/ -type d -name "*" -atime +7 -exec rm -rf {} \;
find /home/*/.amsn/*_*/webcam/ -type f -name "*" -atime +14 -exec shred -fzu -n=7 {} \;
find /home/*/.amsn/*_*/webcam/*/ -type d -name "*" -atime +14 -exec rm -rf {} \;
find /home/*/.amsn/voiceclips/cache/ -type f -name "*" -atime +7 -exec shred -fzu -n=7 {} \;
find /home/*/.amsn/voiceclips/cache/*/ -type d -name "*" -atime +7 -exec rm -rf {} \;
find /home/*/.amsn/*_*/voiceclips/cache/ -type f -name "*" -atime +14 -exec shred -fzu -n=7 {} \;
find /home/*/.amsn/*_*/vociceclips/cache/*/ -type d -name "*" -atime +14 -exec rm -rf {} \;

echo -e $YELLOW"Vaciando todas las papeleras..."$ENDCOLOR
rm -rfv /home/*/.local/share/Trash/*/** &> /dev/null
rm -rfv /root/.local/share/Trash/*/** &> /dev/null

echo -e $YELLOW"¡Fin del script!"$ENDCOLOR

exit 0

