#! /bin/sh

# deb-cleanup v1.0
# Query deborphan to aggressively clean up a Debian system
# Copyright (c) 2016 Raphaël Halimi <raphael.halimi@gmail.com>

# Source shell-script-helper
. /lib/shell-script-helper


#
# Variables
#

# Configuration files
SYSTEM_CONFIG_FILE="/etc/$(basename "$0").conf"
USER_CONFIG_FILE="$HOME/.$(basename "$0").conf"

# Options defaults
DEBORPHAN_OPTIONS="--ignore-suggests"
APT_GET_OPTIONS="--purge --auto-remove"


#
# Functions
#

print_usage () {
  printf "Usage: %s [OPTION]...\n" "$(basename "$0")"
  printf "Query deborphan to aggressively clean up a Debian system\n"
  printf "\nOPTIONS:\n"
  print_option "-v" "Verbose mode"
  print_option "-d" "Debug mode"
  print_option "-h" "Print this help message"
}


#
# Config files
#

[ -e "$SYSTEM_CONFIG_FILE" ] && . "$SYSTEM_CONFIG_FILE"
[ -e "$USER_CONFIG_FILE" ] && . "$USER_CONFIG_FILE"


#
# Options processing
#

while getopts "vdh" OPTION ; do
  case $OPTION in
    v) enable_verbose ;;
    d) enable_debug ;;
    h) print_usage ; exit 0 ;;
    *) print_usage ; exit 1 ;;
  esac
done ; shift $((OPTIND-1))
print_debug "DEBORPHAN_OPTIONS=$DEBORPHAN_OPTIONS" "APT_GET_OPTIONS=$APT_GET_OPTIONS"


#
# Checks
#

lock_script

# This script needs deborphan
which deborphan > /dev/null || die "Please install deborphan"


#
# MAIN
#

# deborphan : libs
print_verbose "Checking unused library packages"
PKGS_TO_REMOVE=$(deborphan $DEBORPHAN_OPTIONS | sort | xargs)
if [ -n "$PKGS_TO_REMOVE" ] ; then
  printf "Unused library packages: %s. Remove ? [y/N] " "$PKGS_TO_REMOVE" ; read ANSWER
  case "$ANSWER" in
    y|Y) sudo apt-get $APT_GET_OPTIONS remove $PKGS_TO_REMOVE ; printf "\n" ;;
  esac
fi

# deborphan : guesses
for PKG_TYPE in section dummy common data debug dev kernel mono perl pike python ruby ; do
  print_debug "PKG_TYPE=$PKG_TYPE"
  print_verbose "Checking unused $PKG_TYPE packages"
  PKGS_TO_REMOVE=$(deborphan $DEBORPHAN_OPTIONS --guess-only --guess-$PKG_TYPE | sort | xargs)
  # Remove this line when #784087 is closed (deborphan misses some config files)
  [ "$PKG_TYPE" = "dummy" ] && PKGS_TO_REMOVE="$PKGS_TO_REMOVE $(dpkg -l | grep ^ii | grep -i dummy | awk '{print $2}' | xargs)"
  if [ -n "${PKGS_TO_REMOVE# }" ] ; then
    printf "Unused %s packages: %s. Remove ? [y/N] " "$PKG_TYPE" "$PKGS_TO_REMOVE" ; read ANSWER
    case "$ANSWER" in
      y|Y) sudo apt-get $APT_GET_OPTIONS remove $PKGS_TO_REMOVE ; printf "\n" ;;
    esac
  fi
done

# deborphan : config files
print_verbose "Checking residual config files"
PKGS_TO_REMOVE=$(deborphan $DEBORPHAN_OPTIONS --find-config | sort | xargs)
# Remove this line when #784087 is closed (deborphan misses some config files)
PKGS_TO_REMOVE="$PKGS_TO_REMOVE $(dpkg -l | grep ^rc | awk '{print $2}' | xargs)"
if [ -n "${PKGS_TO_REMOVE# }" ] ; then
  printf "Residual config files: %s. Remove ? [y/N] " "$PKGS_TO_REMOVE" ; read ANSWER
  case "$ANSWER" in
    y|Y) sudo dpkg --purge $PKGS_TO_REMOVE ; printf "\n" ;;
  esac
fi

# Last pass
print_verbose "Last pass: apt-get $APT_GET_OPTIONS autoremove"
sudo apt-get $APT_GET_OPTIONS autoremove ; printf "\n"
