#!/bin/bash -e
#
# uswsusp.config -- configure script for uswsusp debian package
#
# Copyright 2006    Tim Dijkstra <tim@famdijkstra.org>
# Released under GPLv2

. /usr/share/debconf/confmodule

CONFIGFILE=/etc/uswsusp.conf

LOWQ="snapshot_device compute_checksum compress early_writeout image_size suspend_loglevel max_loglevel"
MEDQ="resume_device encrypt"
HIGHQ=""
NOTQ="RSA_key_file splash shutdown_method"

# Apparently some people don't have /proc 
if mountpoint -q /proc ; then
    # List of partition swaps, listed by size
    SWAPPARTS=`sort -r -k 3 /proc/swaps | awk '$2=="partition" {print $1}'`
    IMAGESIZE=`awk '$1 == "MemTotal:" {print int($2*1024*0.46)}' /proc/meminfo 2> /dev/null`
fi

SWAPLIST=
SWAPDEFAULT=
KOMMA=
for I in $SWAPPARTS; do
    SWAPLIST=${SWAPLIST}${KOMMA}$I
    if [ -z "$KOMMA" ]; then
	SWAPDEFAULT=$I
	KOMMA=", "
    fi
done

echo "SWAPPARTS:$SWAPPARTS"
echo "SWAPLIST:$SWAPLIST"

# Try to detect snapshot support, if they don't even have /sys don't bother...
if mountpoint -q /sys && [ ! -e /sys/class/misc/snapshot/dev ]; then 
    db_input critical uswsusp/no_snapshot || true
    db_go || true
    exit 0
fi


if [ -n "$IMAGESIZE" ]; then
    db_set uswsusp/image_size $IMAGESIZE
fi

# Luckily the parser in s2disk is quite strict
if [ -e "$CONFIGFILE" ]; then
    for I in $LOWQ $MEDQ $HIGHQ $NOTQ; do
	VAL=`sed -n 's/^[[:space:]]*'"${I//_/ }"'[[:space:]]*[=:][[:space:]]*\([^[:space:]]*\)/\1/ p' $CONFIGFILE`

	# For boolean values it only checks for [yY]
	db_metaget uswsusp/$I type
	TYPE=$RET
	
	if [ "boolean" = "$TYPE" ]; then
	    if [ "$VAL" = "y" -o "$VAL" = "Y" ];  then
		db_set uswsusp/$I true
	    else
		db_set uswsusp/$I false
	    fi
	else
	    db_set uswsusp/$I "$VAL"
	fi
	    
    done
fi

# Some people want to use a swap partition that is only mounted 
# during suspend. This means we can't find it in /proc/swaps, but
# it is a valid option non the less. To not remove their changes
# we add it to the SWAPLIST, but not after we confirmed this is 
# what they want.
db_get uswsusp/resume_device
USERSWAP=$RET

echo "SWAPLIST:$SWAPLIST"
echo "USERSWAP:$USERSWAP"

if ! echo "$SWAPLIST" | grep -q -e '\(^\|,\)/'$USERSWAP'\(,\|$\)'; then
    db_input critical uswsusp/continue_without_swap || true
    db_get uswsusp/continue_without_swap 
    if [ "$RET" = "true" ]; then
	SWAPLIST=${SWAPLIST}${KOMMA}${USERSWAP} ;
	SWAPDEFAULT=${USERSWAP}
    fi
fi

# We had to move this untill after the config file parsing, because
# the issue with people having swap deactivated during configure.
if [ -z "$SWAPLIST" ]; then 
    db_input critical uswsusp/no_swap || true
    db_go || true
    exit 0
fi
db_subst uswsusp/resume_device list $SWAPLIST
db_set uswsusp/resume_device $SWAPDEFAULT

# If we're still here, reset the seen flags on error messages
# Maybe they had problems before, but not anymore...
db_fset uswsusp/no_swap seen false
db_fset uswsusp/no_snapshot seen false


# Stetup questions according to  priority
for I in $LOWQ; do
    db_input low uswsusp/$I || true
done	
    
for I in $MEDQ; do
    db_input medium uswsusp/$I || true
done

for I in $HIGHQ; do
    db_input high uswsusp/$I || true
done

# Ask questions
db_go || true

# Only if they want encryption, ask the RSA question
db_get uswsusp/encrypt
if [ "$RET" = "false" ]; then
    db_set uswsusp/create_RSA_key false
elif [ "$RET" = "true" ]; then
    # First ask filename
    while [ 1 ] ; do
	db_input low uswsusp/RSA_key_file || true
	db_go || true
        db_get uswsusp/RSA_key_file || true
	KEYFILE=$RET

	if [ -n "$KEYFILE" ]; then
	    break
	fi

	db_reset uswsusp/RSA_key_file
    done

    # Then ask if we should generate it, default to yes if they don't have one yet
    if [ -e "$KEYFILE" ]; then
	db_set uswsusp/create_RSA_key false
    else
	db_set uswsusp/create_RSA_key true
    fi

    db_input low uswsusp/create_RSA_key || true
    db_go || true

    # If they want it created, ask nr bits and passphrase (twice)
    db_get uswsusp/create_RSA_key
    if [ "$RET" = "true" ]; then
	while [ 1 ]; do
	    db_input low uswsusp/RSA_key_bits || true
	    db_go || true
	    db_get uswsusp/RSA_key_bits

	    if [ -z "$RET" ] || [ $RET -ge 1024 -a $RET -le 4094 ]; then
		break;
	    fi

	    db_reset uswsusp/RSA_key_bits
	done
	
	P1=A; P2=B;

	while [ "$P1" != "$P2" ]; do

	    db_input critical uswsusp/RSA_passphrase || true
	    db_input critical uswsusp/RSA_passphrase_v || true
	    db_go || true

	    db_get uswsusp/RSA_passphrase
	    P1=$RET
	    db_get uswsusp/RSA_passphrase_v
	    P2=$RET
	done
    fi
fi


