#!/bin/sh
set -e

/usr/sbin/update-webmin add ldap-users
/usr/sbin/update-webmin remove useradmin

CONFIGFILE=/etc/webmin/ldap-users/config
CONFOPT="secret server rootdn basedn \
         homeprefix mailprefix sambasync \
         passwd_capital passwd_lowercase passwd_number \
         passwd_length"
. /usr/share/debconf/confmodule

db_get webmin-ldap-skolelinux/no-auto-config
no_auto_config=$RET

case "$1" in
    configure)
	if [ "$no_auto_config"x = x ]; then
	    
            # Generate config file, if it doesn't exist.
            # An alternative is to copy in a template
            # file from elsewhere.
	    if [ ! -e $CONFIGFILE ]; then
		echo "# Config file for webmin-ldap-skolelinux" > $CONFIGFILE
		chmod 600 $CONFIGFILE
		for i in $CONFOPT; do
		    echo "$i="  >> $CONFIGFILE
		done
	    else 
		for i in $CONFOPT ; do
		    if ! grep "^ *$i=" $CONFIGFILE >/dev/null 2>/dev/null
			then
			echo "$i=" >> $CONFIGFILE
		    fi
		done
	    fi
	    
        # Substitute in the values from the debconf db.
        # There are obvious optimizations possible here.
        # The cp before the sed ensures we do not mess up
        # the config file's ownership and permissions.
	    for i in $CONFOPT ; do
		eval "db_get webmin-ldap-skolelinux/$i"
		eval "$i=\"\$RET\""
	    done
	    
	    cp -a -f $CONFIGFILE $CONFIGFILE.tmp
	    
	    for i in $CONFOPT; do
		SEDSTRING="$SEDSTRING -e \"s^ *$i=.*$i=\$$i\" "
	    done
	    eval "sed $SEDSTRING < $CONFIGFILE > $CONFIGFILE.tmp"
            mv -f $CONFIGFILE.tmp $CONFIGFILE
	fi
	
	db_get webmin-ldap-skolelinux/auto-update_nextid
	auto_update_nextid=$RET
	if  [ "$auto_update_nextid" = true ]; then
	#    mail root -s "please run /usr/share/webmin/ldap-users/update-nextid.pl to upgrade your lpap directory structure" < /dev/null
	    :
	fi

	;;

    abort-upgrade|abort-remove|abort-deconfigure)
        # We need not do anything.
	;;
    
    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 0
	;;
esac

#DEBHELPER#
