#!/bin/sh
# config maintainer script for zoneminder

set -e

# Source the debconf stuff
. /usr/share/debconf/confmodule

# Set the first version in which dbconfig-common was introduced in the package
dbc_first_version="1.28.0"

CONFIGFILE=/etc/zm/zm.conf
if [ -e $CONFIGFILE ]; then
    # Source the config file if exists
    . $CONFIGFILE
elif [ -e ${CONFIGFILE}.dpkg-new ]; then
    # If no config file, source the config file which is going to be installed
    # by the core package
    . ${CONFIGFILE}.dpkg-new
else
    # If no config file is going to be installed, set some default values
    ZM_DB_HOST=
    ZM_DB_NAME="zm"
    ZM_DB_USER="zmuser"
fi

# Set some variables for the dbconfig-common stuff
dbc_dbserver="$ZM_DB_HOST"
dbc_dbname="$ZM_DB_NAME"
dbc_dbuser="$ZM_DB_USER"

if [ -f /usr/share/dbconfig-common/dpkg/config ]; then

    # Default use dbconfig-common
    dbc_install="true"

    # Currently we only support mysql database
    dbc_dbtypes="mysql"

    # Set authentication method to password
    dbc_authmethod_user="password"

    # Source the dbconfig-common stuff
    . /usr/share/dbconfig-common/dpkg/config
fi

# Do this when the package is installed, upgraded or reconfigured
# Most of answers are cached so the questions will not be asked again
if [ "$1" = "configure" ] || [ "$1" = "reconfigure" ]; then

    # Ask the user if the database shall be installed locally or remotely
    db_input high zoneminder/debconf_dblocation || true
    db_go || true
    db_get zoneminder/debconf_dblocation

    if [ "$RET" = "local" ]; then
        if [ ! -e "/usr/sbin/mysqld" ]; then
            # Display a message and exit if the user want a local database but
            # no database server is available
            db_input high zoneminder/debconf_dblocalmissingerror || true
            db_go || true
            exit 0
        else
            # Set the database server to localhost
            dbc_dbserver="localhost"
        fi
    else
        # Source the dbconfig main configuration file
        if [ -f /etc/dbconfig-common/config ]; then
            . /etc/dbconfig-common/config
        fi
        if [ "$dbc_remote_questions_default" = "false" ]; then
            # Display a message and exit if the dbconfig configuration does not
            # allow installation of remote databases
            # Note: To overcome this issue, we could think to override the
            # default setting by using dbc_remote_questions_default='true' in
            # maintainer scripts but unfortunately this does not work due to
            # current dbconfig design
            # More information here:
            # https://bugs.launchpad.net/ubuntu/+source/dbconfig-common/+bug/1065331
            db_input high zoneminder/debconf_dbconfigerror || true
            db_go || true
            exit 0
        fi
    fi

    # Ask the user for all database settings
    dbc_go zoneminder $@

    # Ask the user for the password of the database administrator if the user
    # has not yet answered to this question.
    # This situation may occur if the user skipped the database creation step
    # when reconfiguring the package.
    RET=""
    db_get zoneminder/mysql/admin-pass
    if [ -z "$RET" ]; then
        db_input high zoneminder/mysql/admin-pass || true
        db_go || true
    fi

    # Do this only when not upgrading the package (no old version in argument)
    if [ -z "$2" ]; then
        # Ask for the password of 'admin' user
        while :; do
            RET=""
            db_input high zoneminder/admin_password || true
            db_go || true
            db_get zoneminder/admin_password
            # If password isn't empty we ask for password verification
            if [ -z "$RET" ]; then
                db_fset zoneminder/admin_password seen false
                db_fset zoneminder/admin_password_again seen false
                break
            fi
            ROOT_PW="$RET"
            db_input high zoneminder/admin_password_again || true
            db_go || true
            db_get zoneminder/admin_password_again
            if [ "$RET" = "$ROOT_PW" ]; then
                ROOT_PW=""
                break
            fi
            db_fset zoneminder/password_mismatch seen false
            db_input critical zoneminder/password_mismatch || true
            db_set zoneminder/admin_password ""
            db_set zoneminder/admin_password_again ""
            db_go || true
        done
    else
        # If we are upgrading the package, set an empty password to disable
        # password update in ZoneMinder database
        db_set zoneminder/admin_password ""
    fi
    # Set the seen flag to not ask this question again if no password is
    # provided
    db_fset zoneminder/admin_password seen true

fi

#DEBHELPER#

exit 0
