#!/bin/bash

set -e

. /usr/share/debconf/confmodule

CONFDIR=/etc/lemonldap-ng
SESSIONSDIR=/var/lib/lemonldap-ng/sessions
CONFSTORAGEDIR=/var/lib/lemonldap-ng/conf
FIRSTCONFFILE=$CONFSTORAGEDIR/lmConf-1.json
LMINIFILE=/etc/lemonldap-ng/lemonldap-ng.ini
MIGRATION=/usr/share/lemonldap-ng/bin/lmMigrateConfFiles2ini

if [ "$1" == "configure" ]
then
    for i in domain ldapServer ldapPort ldapBase managerDn managerPassword portal; do
        db_get liblemonldap-ng-common-perl/$i || true
        perl -000 -i -pe "s#^$i(\\n\\s+)('?)[^\\n]*?('?)\$#$i\${1}\${2}$RET\${3}#m" $FIRSTCONFFILE
    done

    # Run migration script to convert menu format if old version is 0.9.*
    if [ "$2" != "" ]; then
        if dpkg --compare-versions "$2" lt 1.0; then
            if [ -e $CONFDIR/storage.conf -o -e $CONFDIR/apply.conf -o -e $CONFDIR/apps-list.xml ] ; then
                db_get liblemonldap-ng-common-perl/migrate
                #if [ "$RET" ]; then
                #    $MIGRATION 2>&1 > /dev/null || :
                #fi
            fi
        fi
    fi
fi

#DEBHELPER#
exit 0
