#!/bin/sh
# postinst script for phpmyadmin

set -e

avahi_install() {
	if [ ! -e /etc/avahi/services/phpmyadmin.service ] ; then
		mkdir -p /etc/avahi/services
		cp /etc/phpmyadmin/phpmyadmin.service /etc/avahi/services/
	fi
}

desktop_install() {
	if [ ! -e /usr/share/applications/phpmyadmin.desktop ] ; then
		mkdir -p /usr/share/applications
		ln -sf /etc/phpmyadmin/phpmyadmin.desktop /usr/share/applications/
	fi
}

lighttpd_install() {
	if [ ! -f /etc/lighttpd/conf-available/50-phpmyadmin.conf ] ; then
		if which lighty-enable-mod >/dev/null 2>&1 ; then
			ln -s ../../phpmyadmin/lighttpd.conf /etc/lighttpd/conf-available/50-phpmyadmin.conf
			# We also need auth to protect setup.php
			lighty-enable-mod phpmyadmin auth fastcgi fastcgi-php
			avahi_install
			desktop_install
		else
			echo "Lighttpd not installed, skipping"
		fi
	fi
}

apache_install() {
	mkdir -p /etc/apache2/conf-available
	ln -sf ../../phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf

	COMMON_STATE=$(dpkg-query -f '${Status}' -W 'apache2-data' 2>/dev/null | awk '{print $3}' || true)

	if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then
		. /usr/share/apache2/apache2-maintscript-helper
		apache2_invoke enconf phpmyadmin
	elif [ "$COMMON_STATE" = "installed" ] || [ "$COMMON_STATE" = "unpacked" ] ; then
		[ -d /etc/apache2/conf.d/ ] && [ ! -L /etc/apache2/conf.d/phpmyadmin.conf ] && ln -s ../conf-available/phpmyadmin.conf /etc/apache2/conf.d/phpmyadmin.conf
	fi

	avahi_install
	desktop_install
}

. /usr/share/debconf/confmodule

. /usr/share/dbconfig-common/dpkg/postinst.mysql
dbc_generate_include_owner="root:www-data"
dbc_generate_include_perms="0640"
dbc_generate_include=php:/etc/phpmyadmin/config-db.php

# dbconfig-common is designed to be sourced by maintainer scripts that do not
# use "set -e": several of the debconf calls in its code are deliberately not
# protected, and its own error handling asks the administrator what to do.
# With "set -e" a debconf protocol error status such as 10 ("badparams", which
# is returned when a template is not registered) aborts this script before
# dbconfig-common can handle the error and leaves the package half-configured,
# which in turn breaks dpkg/apt (Debian bug #1121130). Run it with "set +e"
# instead. Nothing here is fatal: a status of 1 means the administrator chose
# "abort" in dbconfig-common's error dialog, which asks not to configure the
# database, not to leave the package half-installed. The rest of this script
# (cookie secret, webserver configuration, temporary files) does not depend on
# the database, so it still runs and the administrator can configure the
# database later with "dpkg-reconfigure phpmyadmin".
run_dbc_go() {
	set +e
	dbc_go phpmyadmin $@
	dbc_go_status=$?
	set -e
	if [ "$dbc_go_status" -eq 1 ]; then
		echo "WARNING: phpMyAdmin's database was not configured: the operation was aborted at the administrator's request." >&2
		echo "WARNING: run 'dpkg-reconfigure phpmyadmin' once it can be set up." >&2
	elif [ "$dbc_go_status" -ne 0 ]; then
		echo "WARNING: dbconfig-common failed to configure phpMyAdmin's database (status $dbc_go_status)." >&2
		echo "WARNING: the database control user may not have been created; phpMyAdmin may not work" >&2
		echo "WARNING: until the database setup is fixed (run 'dpkg-reconfigure phpmyadmin' afterwards)." >&2
	fi
}

run_dbc_go "$@"

if [ "$1" = "configure" ]; then

	# blowfish_secret must be 32 characters long. Installations before 4:4.6.3-2 generated a secret with a length of 24
	# if there is a blowfish_secret found not meeting that requirement it is moved out of the way and recreated below
	if [ -f /var/lib/phpmyadmin/blowfish_secret.inc.php ] && grep -sq "blowfish_secret.*=.*'.\{24\}'" /var/lib/phpmyadmin/blowfish_secret.inc.php
	then
		mkdir -p /var/backups/phpmyadmin
		mv /var/lib/phpmyadmin/blowfish_secret.inc.php /var/backups/phpmyadmin/blowfish_secret.inc.php
		echo "The blowfish_secret configured in /var/lib/phpmyadmin/blowfish_secret.inc.php is too weak and will be regenerated."
		echo "A backup of the file has been placed at /var/backups/phpmyadmin/blowfish_secret.inc.php"
	fi

	# Generate secret for cookie encryption
	if [ ! -f /var/lib/phpmyadmin/blowfish_secret.inc.php ]; then
		touch /var/lib/phpmyadmin/blowfish_secret.inc.php
		chgrp www-data /var/lib/phpmyadmin/blowfish_secret.inc.php
		chmod 640 /var/lib/phpmyadmin/blowfish_secret.inc.php
        cat >> /var/lib/phpmyadmin/blowfish_secret.inc.php <<EOT
<?php
\$cfg['blowfish_secret'] = '`cat /dev/urandom | tr -dc 'a-zA-Z0-9~!@#$%^&*_()+}{?></";.,[]=-' | fold -w 32 | head -n 1`';
EOT
	fi

	db_version 2.0

	if [ -f /var/lib/phpmyadmin/config.inc.php ]
	then
		if [ -s /var/lib/phpmyadmin/config.inc.php ]; then
			# file is empty
			rm /var/lib/phpmyadmin/config.inc.php
		else
			mkdir -p /var/backups/phpmyadmin
			mv /var/lib/phpmyadmin/config.inc.php /var/backups/phpmyadmin/webbased-config.inc.php
			echo "The webbased installer was disabled on debian with phpmyadmin version 4.9.1."
			echo "An existing configuration was found under /var/lib/phpmyadmin/config.inc.php."
			echo "A backup of the file has been placed at /var/backups/phpmyadmin/webbased-config.inc.php"
		fi
	fi

	if ! dpkg-statoverride --list /var/lib/phpmyadmin/tmp >/dev/null; then
		chown www-data:www-data    /var/lib/phpmyadmin/tmp
	fi

	# Empty the temporary directory: the Twig cache is rebuilt on demand and
	# unconsumed uploads are already abandoned. -type f leaves symlinks and
	# the directory itself alone, and "|| true" keeps a failure from aborting
	# the configuration while still reporting it.
	if [ -d /var/lib/phpmyadmin/tmp ]; then
		find /var/lib/phpmyadmin/tmp -xdev -ignore_readdir_race -type f -delete || true
	fi

	# Configure Apache
	db_get phpmyadmin/reconfigure-webserver || true

	webservers="$RET"

	for webserver in $webservers; do
		webserver=${webserver%,}
		if [ "$webserver" = "lighttpd" ] ; then
			lighttpd_install
		else
			# Need to pass params for apache2-maintscript-helper
			apache_install $@
		fi
		# Reload webserver in any case, configuration might have changed
		# Redirection of 3 is needed because Debconf uses it and it might
		# be inherited by webserver. See bug #446324.
        if [ -f /etc/init.d/$webserver ] ; then
            if which invoke-rc.d > /dev/null ; then
                if invoke-rc.d $webserver status > /dev/null 2>&1 ; then
                    invoke-rc.d $webserver reload 3>/dev/null || true
                fi
            else
                if /etc/init.d/$webserver status > /dev/null 2>&1 ; then
                    /etc/init.d/$webserver reload 3>/dev/null || true
                fi
            fi
        fi
	done
fi

#DEBHELPER#

exit 0
