#!/usr/bin/env bash

set -eux

dpkg --list \
    | awk '{ print $2 }' \
    | grep 'linux-image-.*-generic' \
    | grep -v `uname -r` \
    | sudo xargs apt-get -y purge;

# Delete Linux source
dpkg --list \
    | awk '{ print $2 }' \
    | grep linux-source \
    | sudo xargs apt-get -y purge;

# Delete development packages
# dpkg --list \
#     | awk '{ print $2 }' \
#     | grep -- '-dev\(:[a-z0-9]\+\)\?$' \
#     | sudo xargs apt-get -y purge;

# delete docs packages
dpkg --list \
    | awk '{ print $2 }' \
    | grep -- '-doc$' \
    | sudo xargs apt-get -y purge;

if [ "${CHEF_KEEP}" != "true" ]; then
    sudo apt-get -y purge chef
fi

sudo apt-get -y autoremove;
sudo apt-get -y clean;

# Remove caches
sudo find /var/cache -type f -exec rm -rf {} \;

# delete any logs that have built up during the install
sudo find /var/log -type f -exec truncate --size=0 {} \;

# Whiteout root
count=$(df --sync -kP / | tail -n1  | awk -F ' ' '{print $4}')
count=$(($count-1))
sudo dd if=/dev/zero of=/tmp/whitespace bs=1M count=$count || echo "dd exit code $? is suppressed";
sudo rm /tmp/whitespace

# Whiteout /boot
count=$(df --sync -kP /boot | tail -n1 | awk -F ' ' '{print $4}')
count=$(($count-1))
sudo dd if=/dev/zero of=/boot/whitespace bs=1M count=$count || echo "dd exit code $? is suppressed";
sudo rm /boot/whitespace

set +e
swapuuid="`/sbin/blkid -o value -l -s UUID -t TYPE=swap`";
case "$?" in
    2|0) ;;
    *) exit 1 ;;
esac
set -e

if [ "x${swapuuid}" != "x" ]; then
    # Whiteout the swap partition to reduce box size
    # Swap is disabled till reboot
    swappart="`readlink -f /dev/disk/by-uuid/$swapuuid`";
    sudo /sbin/swapoff "$swappart";
    sudo dd if=/dev/zero of="$swappart" bs=1M || echo "dd exit code $? is suppressed";
    sudo /sbin/mkswap -U "$swapuuid" "$swappart";
fi

sudo sync;
