#!/bin/sh

if test -f /etc/parsec/swap_wiper.conf
then
	. /etc/parsec/swap_wiper.conf
else
	exit 1
fi

test "$ENABLED" != "Y" && exit 0

SWAP_LIST=`blkid -t TYPE=swap | grep swap | cut -d ':' -f1`
SWAP_LIST+=" "
SWAP_LIST+=`swapon --show --raw --noheadings | cut -d " " -f1`
SWAP_LIST=`echo $SWAP_LIST | tr " " "\n" | sort -u | tr "\n" " "`

for IGNOR in $IGNORE
do
	SWAP_LIST=`echo $SWAP_LIST | sed "s#$IGNOR##g"`
done

TIMEOUT=2

for SWAP in $SWAP_LIST
do
(
	echo "Cleaning swap $SWAP"
	UUID=`blkid -s UUID -o value $SWAP`
	if [ -n "$UUID" ]; then UUID="-U $UUID"; fi
	swapoff $SWAP 2> /dev/null

	# mkswap with saved uuid after timeout to keep it
	# operable if operator forcefully shuts down the
	# system while shred is still running
	( sleep $TIMEOUT; mkswap $UUID $SWAP 1> /dev/null ) &
	shred -n0 -z $SWAP
	wait
	echo "$SWAP done"
) &
done
wait
exit 0
