#!/bin/bash

rm -f /etc/fstab
rm -f /etc/mtab
rm -f /etc/apt/sources.list~
rm -Rf /etc/systemd/system/timers.target.wants
rm -f /etc/systemd/system/multi-user.target.wants/ssh.service
rm -f /etc/ssh/ssh_host*

rm -f /var/backups/*
rm -f /var/cache/ldconfig/*
rm -f /var/cache/debconf/*
rm -f /var/cache/fontconfig/*
rm -f /var/lib/apt/extended_states
rm -f /var/lib/systemd/random-seed
rm -f /var/lib/apt/lists/deb.*
rm -Rf /root/.local/share/mc
rm -Rf /root/.cache
rm -f /root/.wget-hsts

rm -f /var/lib/dpkg/*-old
rm -f /var/log/*
rm -f /var/log/*/*
rm -f /var/log/*/*/*
rm -f /var/cache/apt/archives/*.deb
rm -f /var/cache/apt/*.bin
rm -f /var/cache/debconf/*-old
rm -f /var/lib/dhcp/dhclient.leases
rm -f /root/.bash_history
rm -f /root/.wget-hsts
rm -Rf /usr/share/doc/*
rm -Rf /usr/share/info/*
rm -f /usr/share/images/fluxbox/debian-squared.jpg
rm -Rf /usr/share/fluxbox/nls/??*

rm -Rf /usr/share/locale/??
rm -Rf /usr/share/locale/??_*
rm -Rf /usr/share/locale/??@*
rm -Rf /usr/share/locale/???
rm -Rf /usr/share/i18n/locales/*_*
rm -Rf /usr/share/man/??
rm -Rf /usr/share/man/*_*

rm -Rf /usr/share/icons/elementaryXubuntu-dark
rm -Rf /usr/share/icons/gnome/256x256


# Unzip gzipped files (man pages), so LZMA can compress 2times better.
# First we fix symlinks, then uncompress files
# $1 = search directory
uncompress_files()
{
   local LINK LINE

   find "$1" -type l -name "*.gz" | while read LINE; do
      LINK="$(readlink "$LINE" | sed -r 's/.gz$//')"
      FILE="$(echo "$LINE" | sed -r 's/.gz$//')"
      ln -sfn "$LINK" "$FILE"
      rm -f "$LINE"
   done
   find "$1" -type f -name "*.gz" | xargs -r gunzip
}

uncompress_files /etc/alternatives
uncompress_files /usr/share/man


# remove broken links
# $1 = search directory
remove_broken_links()
{
   find "$1" -type l -exec test ! -e {} \; -print | xargs rm -vf
}

remove_broken_links /etc/alternatives
remove_broken_links /usr/share/man

