#!/bin/bash

# Copyright (C) 2005 W. Michael Petullo <mike@flyn.org>
# Copyright (C) 2006 David Zeuthen <davidz@redhat.com>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2.

CRYPTSETUP=/sbin/cryptsetup

echo LUKS cryptotext device $HAL_PROP_INFO_UDI is going away

# make sure that the cleartext volume is unmounted
CLEARTEXT_VOLUME_UDI=`hal-find-by-property --key volume.crypto_luks.clear.backing_volume --string $HAL_PROP_INFO_UDI`
RET=$?
if [ x$RET = x0 ]; then
    echo Attempting unmount of corresponding LUKS cleartext volume $CLEARTEXT_VOLUME_UDI
    # lazy unmount via D-BUS
    dbus-send --print-reply --system \
	      --dest=org.freedesktop.Hal \
	      $CLEARTEXT_VOLUME_UDI \
	      org.freedesktop.Hal.Device.Volume.Unmount \
	      array:string:lazy
fi

echo Tearing down LUKS cryptotext volume

if [ ! -f $CRYPTSETUP ]; then
	echo Error removing $HAL_PROP_BLOCK_DEVICE - $CRYPTSETUP not found >&2
	exit 1
fi

if [ -z $HAL_PROP_VOLUME_UUID ]; then
	echo Error determining UUID of $HAL_PROP_BLOCK_DEVICE >&2
	exit 1
fi

if ! $CRYPTSETUP luksClose luks_crypto_$HAL_PROP_VOLUME_UUID 2> /dev/null; then
	echo Error removing luks_crypto_$HAL_PROP_VOLUME_UUID >&2
	exit 1
fi

exit 0
