#!/bin/bash

# Ubuntu postremove
# called with $1 == "purge" or $1 == "remove" after files are removed

if [[ "$1" = "purge" ]] ; then
     echo "PURGING all sensitive information"

     if [ -f /usr/lib/cryptobone/bootswitch ] ; then
          chattr -i /usr/lib/cryptobone/bootswitch
          /usr/bin/rm -f /usr/lib/cryptobone/bootswitch 2> /dev/null
     fi

     # remove all messages from the ramdisks
     /usr/bin/rm -rf /dev/shm/RAM 2>/dev/null
     /usr/bin/rm -rf /dev/shm/EXRAM 2>/dev/null
 
     if grep cryptobone /etc/passwd >/dev/null 2>/dev/null; then
            /usr/sbin/userdel cryptobone
     fi

     # umount the keys directory
     /usr/bin/umount /usr/lib/cryptobone/keys 2> /dev/null
     # delete all config files in main cryptobone directories

     /usr/bin/rm -rf /usr/lib/cryptobone/keys/* 2> /dev/null
     /usr/bin/rm -rf /usr/lib/cryptobone/cryptobone 2> /dev/null
     /usr/bin/rm -f /usr/lib/cryptobone/database* 2> /dev/null
     /usr/bin/rm -f /usr/lib/cryptobone/cbb.config 2> /dev/null
     /usr/bin/rm -f /usr/lib/cryptobone/keys.tgz 2> /dev/null
     /usr/bin/rm -f /usr/lib/cryptobone/masterkey 2> /dev/null
     /usr/bin/rm -f /usr/lib/cryptobone/pinghost 2> /dev/null
     /usr/bin/rm -f /usr/lib/cryptobone/safewebdrop/p* 2> /dev/null
     /usr/bin/rm -f /usr/lib/cryptobone/ext/masterkey 2> /dev/null
     /usr/bin/rm -f /usr/lib/cryptobone/ext/database* 2> /dev/null
     /usr/bin/rm -f /usr/lib/cryptobone/ext/safewebdrop/p* 2> /dev/null
     /usr/bin/rm -rf /usr/lib/cryptobone/ext/cryptobone 2> /dev/null
     /usr/bin/rm -f /usr/lib/cryptobone/ext/*.hash 2> /dev/null
     /usr/bin/rm -rf /usr/lib/cryptobone/cltls 2> /dev/null
     /usr/bin/rm -rf /usr/lib/cryptobone/ext/cltls 2> /dev/null

     # remove the sudo file
     /usr/bin/rm -f /etc/sudoers.d/cbcontrol 2> /dev/null
     # disable all daemons
     /usr/bin/systemctl disable cryptoboned 2> /dev/null
     /usr/bin/systemctl disable cryptoboneexternd 2> /dev/null
     /usr/bin/systemctl disable cryptobone-fetch.timer 2> /dev/null
fi

/usr/bin/touch --no-create /usr/share/icons/default &>/dev/null
/usr/bin/gtk-update-icon-cache /usr/share/icons/default &>/dev/null  || :

exit 0
