#!/bin/sh
# Make sure to call this script from the root of the dbconfig-common tree

set -e

ROOT_USER=debian-sys-maint
ROOT_PASSWD=$(grep -m1 password /etc/mysql/debian.cnf | awk '{print $3}')
FRONTENDS="noninteractive-defaults noninteractive readline"
export DEBCONF_DEBUG=developer
export dbc_debug=true

preseed(){
    # Maybe a bug in debconf (but not bug 779920) or in dbconfig-common
    /usr/share/debconf/fix_db.pl 2> /dev/null

    if [ $PRESEED = true ] ; then
	echo
	echo PRESEEDING now $PACKAGE
	echo $PACKAGE ${PACKAGE}/dbconfig-install boolean true          | debconf-set-selections
	# In pbuilder env, installation fails, so don't try during upgrade
	echo $PACKAGE ${PACKAGE}/dbconfig-upgrade boolean true          | debconf-set-selections
	if [ "$PACKAGE" = db-test-pgsql-migration ] && \
	       [ "$(dpkg-query -W -f'${db:Status-Status}' $PACKAGE)" = installed ] ; then
	    # After migration, the host is bogus and thus not reachable
	    echo $PACKAGE ${PACKAGE}/dbconfig-remove boolean false      | debconf-set-selections
	elif [ "$PACKAGE" = db-test-sqlite ] || [ "$PACKAGE" = db-test-sqlite3 ] ; then
	    # sqlite stores the db on file, which can be removed
	    echo $PACKAGE ${PACKAGE}/dbconfig-remove boolean true       | debconf-set-selections
	else
	    # In pbuilder env, installation fails, so don't try during purge
	    echo $PACKAGE ${PACKAGE}/dbconfig-remove boolean true       | debconf-set-selections
	fi
	# Next line isn't honored of course
	# echo $PACKAGE ${PACKAGE}/install-error select ignore            | debconf-set-selections
	echo $PACKAGE ${PACKAGE}/mysql/admin-user string $ROOT_USER     | debconf-set-selections
	echo $PACKAGE ${PACKAGE}/mysql/admin-pass password $ROOT_PASSWD | debconf-set-selections
	echo $PACKAGE ${PACKAGE}/mysql/app-pass password blabla         | debconf-set-selections
	echo $PACKAGE ${PACKAGE}/pgsql/app-pass password blabla         | debconf-set-selections
	if [ "$PACKAGE" = db-test-multidbtype ] ; then
	    echo $PACKAGE ${PACKAGE}/database-type string mysql         | debconf-set-selections
	fi

        echo
	echo debconf state:
	debconf-show $PACKAGE
    fi
}

install_and_purge(){
    CONF=/etc/dbconfig-common/${PACKAGE}.conf
    echo
    if [ ! -f ${PACKAGE}_2.0_all.deb ] ; then
        ./buildpackages.sh
    fi
    preseed
    dpkg -i ${PACKAGE}_2.0_all.deb
    if [ -f $CONF ] ; then
	echo
	echo Content of dbc configuration file for $PACKAGE:
	egrep "^dbc_" $CONF || true
    fi
    echo
    # Preseeding purge does not work during install because dbc will think
    # it is a reinstallation
    if [ $PRESEED = true ] ; then
	echo $PACKAGE ${PACKAGE}/purge boolean true                     | debconf-set-selections
    fi
    preseed
    apt-get purge -y ${PACKAGE}

    if [ -f $CONF ] ; then
	echo
	echo dbc configuration file for $PACKAGE still exists, which is wrong
	exit 1
    fi
}

cd examples

for FRONTEND in $FRONTENDS ; do
    if [ "${FRONTEND#*-}" = defaults ] ; then
	PRESEED=false
	FRONTEND=${FRONTEND%-*}
    else
	PRESEED=true
    fi
    export DEBIAN_FRONTEND=$FRONTEND

    echo
    echo
    echo "Now running installation with DEBIAN_FRONTEND=$FRONTEND and preseeding=$PRESEED"
    echo

    PACKAGE=db-test-multidbtype
    install_and_purge

    PACKAGE=db-test-mysql-frontend
    install_and_purge

    PACKAGE=db-test-mysql-perl
    install_and_purge

    PACKAGE=db-test-mysql
    install_and_purge
    # Updating fails without preseeding as the admin password was not available
    # during installation, so installation failed. Ergo, nothing to update.
    if [ $PRESEED = true ] ; then
	preseed
	dpkg -i ${PACKAGE}_2.0_all.deb
	preseed
	dpkg -i ${PACKAGE}_2.1_all.deb
	if [ $PRESEED = true ] ; then
	    echo $PACKAGE ${PACKAGE}/purge boolean true                     | debconf-set-selections
	fi
	preseed
	apt-get purge -y ${PACKAGE}
    fi
    preseed
    dpkg -i ${PACKAGE}_2.1_all.deb
    if [ $PRESEED = true ] ; then
	echo $PACKAGE ${PACKAGE}/purge boolean true                     | debconf-set-selections
    fi
    preseed
    apt-get purge -y ${PACKAGE}

    PACKAGE=db-test-pgsql-migration
    preseed
    dpkg -i ${PACKAGE}_1.9_all.deb
    preseed
    apt-get purge -y ${PACKAGE}
    preseed
    dpkg -i ${PACKAGE}_1.9_all.deb
    preseed
    dpkg -i ${PACKAGE}_2.0_all.deb
    if [ $PRESEED = true ] ; then
	echo $PACKAGE ${PACKAGE}/purge boolean true                     | debconf-set-selections
    fi
    preseed
    apt-get purge -y ${PACKAGE}
    install_and_purge

    PACKAGE=db-test-pgsql
    install_and_purge

    PACKAGE=db-test-sqlite
    install_and_purge

    PACKAGE=db-test-sqlite3
    install_and_purge
done
