#!/bin/bash
# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/package/base/device-mapper/etc_stone.d_mod_dm.sh
# ROCK Linux is Copyright (C) 1998 - 2006 Clifford Wolf
# 
# 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. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---
#
# [MAIN] 60 dm DeviceMapper configuration (encrypted filesystems)

# TODO XXX This needs a complete overhaul!
# TODO XXX kick blindcoder in the gnats to do it.

dm_edit_status () {
# Here, BROKEN indicates NOT IMPLEMENTED
	gui_menu dm_edit_status "Please select new status of $device" \
		"Is encrypted" "status=encrypted" \
		"Decrypt (BROKEN)" "status=decrypt" \
		"Encrypt (BROKEN)" "status=encrypt" \
		"Encrypted Swap (read documentation) (BROKEN)" "status=swap" \
		"not encrypted" "status=plain"
}

dm_edit_dev () {
	cmd=""
	for x in /dev/{ide,scsi}/host*/bus*/target*/lun*/{disc,part*} ; do
		[ -b $x ] || continue
		cmd="$cmd '$x' 'device=$x'"
	done
	eval "gui_menu dm_edit_dev \"Please select new device to mount on $mountpoint\" $cmd"
}

dm_edit () {
	device=$1
	while read olddevice oldmountpoint oldstatus ; do
		[ "$olddevice" == "$device" ] && break
	done < /etc/conf/dm/mounts
	mountpoint=$oldmountpoint
	status=$oldstatus
	run=0
	while [ $run -eq 0 ] ; do
		gui_menu dm_edit "Edit encryption for $device" "Device: $device" "dm_edit_dev $mountpoint" \
		"Mountpoint: $mountpoint" "gui_input 'Please enter new mountpoint' '$mountpoint' mountpoint" \
		"Status: $status" "dm_edit_status"
		run=$?
	done
	sed -e "s,^$olddevice.*$oldmountpoint.*$oldstatus$,$device\t$mountpoint\t$status," -i /etc/conf/dm/mounts
	recreate_initrd=1
	return 0
}

dm_add () {
	cmd=""
	for x in /dev/{ide,scsi}/host*/bus*/target*/lun*/{disc,part*} ; do
		[ -b $x ] || continue
		grep -q $x /etc/conf/dm/mounts && continue
		cmd="$cmd '$x' 'device=$x'"
	done
	eval "gui_menu dm_add1 'Please select the device to use' $cmd" || return
	cmd=""
	gui_input "Please enter mountpoint of $device" "/data" mountpoint || return
	gui_menu dm_add2 "Please enter current state of $device" \
		"is encrypted" "status=encrypted" \
		"Decrypt" "status=decrypt" \
		"Encrypt" "status=encrypt" \
		"Encrypted Swap (read documentation)" "status=swap" \
		"not encrypted" "status=plain" || return

	echo -e "$device\t$mountpoint\t$status" >>/etc/conf/dm/mounts
	recreate_initrd=1
}

main() {
	run=0
	if [ ! -f /etc/conf/dm/mounts ] ; then
		mkdir -p /etc/conf/dm
		touch /etc/conf/dm/mounts 
	fi
	while [ $run -eq 0 ] ; do
		cmd=""
		while read device mountpoint status ; do
			cmd="$cmd 'Edit device $device ($status on $mountpoint)' 'dm_edit $device'"
		done < /etc/conf/dm/mounts
		cmd="$cmd 'Add new device' 'dm_add'"
		eval "gui_menu dm 'encrypted filesystems' $cmd"
		run=$?
	done
	tmp="`mktemp`"
	grep -v "added by mod_dm" /etc/conf/kernel >$tmp
	while read device mountpoint status ; do
		if [ "$status" == "swap" -o "$status" == "encrypted" -o "$status" == "encrypt" -o "$status" == "decrypt" ] ; then
			for x in aes-i586 dm-mod dm-crypt ; do
				grep -q $x $tmp || cat >> $tmp <<-EOF
### $x ### added by mod_dm
modprobe $x # added by mod_dm
				EOF
			done
		fi
	done < /etc/conf/dm/mounts
	rm -f /etc/conf/kernel
	mv $tmp /etc/conf/kernel
	chmod 644 /etc/conf/kernel

	tmp=`mktemp`
	rm -rf ${tmp}
	grep -v fsck /etc/conf/initrd/initrd_dm > ${tmp}
	rm -f /etc/conf/initrd/initrd_dm
	mv ${tmp} /etc/conf/initrd/initrd_dm
	
	while read dev a mnt b fs c ; do
		[ -e "/sbin/fsck.${fs}" ]  && echo "/sbin/fsck.${fs} /sbin/fsck.${fs}"
	done < <( mount ) | sort | uniq >>/etc/conf/initrd/initrd_dm
	echo "/sbin/fsck /sbin/fsck" >>/etc/conf/initrd/initrd_dm
}

