#!/bin/bash

#   FILE: hal-luks-setup -- HAL method wrapper for luks-setup
# AUTHOR: W. Michael Petullo <mike@flyn.org>
#   DATE: 13 July 2005
# 
# Copyright (C) 2005 W. Michael Petullo <mike@flyn.org>
# All rights reserved.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# Check for environment variables
if [ "$HAL_PROP_BLOCK_DEVICE" == "" ] || [ "$HAL_PROP_INFO_UDI" == "" ] ; then
        echo "Missing or empty environment variable(s)." >&2
        echo "This script should be started by hald." >&2
        exit 1
fi

read password
PASSWORD=${PASSWORD//[^[:alnum:]_=[:space:]:.+-]/@}

CRYPTSETUP=/sbin/cryptsetup

if [ ! -f $CRYPTSETUP ]; then
    echo org.freedesktop.Hal.Device.Volume.Crypto.CryptSetupMissing >&2
    echo Error setting up $HAL_PROP_BLOCK_DEVICE - $CRYPTSETUP not found >&2
    exit 1
fi

if [ -e /dev/mapper/luks_crypto_$HAL_PROP_VOLUME_UUID ]; then
    echo org.freedesktop.Hal.Device.Volume.Crypto.SetupError >&2
    echo $HAL_PROP_BLOCK_DEVICE is already setup? >&2
    exit 1
fi

if ! echo $password | $CRYPTSETUP luksOpen $HAL_PROP_BLOCK_DEVICE luks_crypto_$HAL_PROP_VOLUME_UUID  2> /dev/null; then
    echo org.freedesktop.Hal.Device.Volume.Crypto.SetupPasswordError >&2
    echo Error setting up $HAL_PROP_BLOCK_DEVICE - bad password? >&2
    exit 1
fi

hal-set-property --udi=$UDI --key="info.callouts.remove" --strlist-pre="hal-luks-remove" > /dev/null 2>&1


exit 0
