#!/bin/sh

set -e

# Debconf configuration script for Sympa.
# by Jerome Marant <jerome@debian.org>

# Modifications by Stefan Hornburg (Racke) <racke@linuxia.de>.

# Source debconf library
. /usr/share/debconf/confmodule

db_version 2.0

conf="/etc/sympa/sympa.conf"
wwconf="/etc/sympa/wwsympa.conf"

# Language selection
# - currently supported languages
possible_langs="ar bg br ca cs da de el en es et eu fi fr gl hu id it ja ko la ml nb_NO nl oc pl pt pt_BR ro ru sv tr vi zh_CN zh_TW"
supported_langs="en"
locales=$(locale -a)

for lang in $possible_langs ; do
	if locale -a | grep -q "$lang" ; then
		supported_langs="$supported_langs, $lang"
	fi
done

# Get the language value in case of a manual change
if [ -f "$conf" ]; then
	lang=$(perl -nle 'if (/^\s*lang\s+(\w+)/) {print $1;}' "$conf")
	db_set sympa/language "$lang"
	# Get the hostname.
	# Since this parameter could be a unix command, we have
	# to check this and run the command to get the value
	if grep -q "^[ 	]*host" "$conf" ; then
		host=$(perl -nle 'if (/^\s*host\s+(.*)$/) {
					if ($1 =~ m/^`(.*)`$/) {
						$cmd=`$1`; print $cmd;
					} else {
						print $1;
					}
				}' "$conf")
	else
		host=$(perl -nle 'if (/^\s*domain\s+(.*)$/) {
					if ($1 =~ m/^`(.*)`$/) {
						$cmd=`$1`; print $cmd;
					} else {
						print $1;
					}
				}' "$conf")
	fi
	# Get the listmasters, since this parameter could be a unix command, we have
	# to check this and run the command to get the value
	listmaster=$(perl -nle 'if (/^\s*listmaster\s+(.*)$/) {
					if ($1 =~ m/^`(.*)`$/) {
						$cmd=`$1`; print $cmd;
					} else {
						print $1;
					}
				}' "$conf")
else
	# Get default values on configure
    host=$(head -n 1 /etc/mailname || hostname -f || echo localhost)
	listmaster=
fi

# Set default values

# Ask for language
if [ "$supported_langs" != "en" ]; then
	db_subst sympa/language supported_langs $supported_langs
	db_input medium sympa/language || [ $? -eq 30 ]
	db_go
else
	db_set sympa/language en
fi

db_get sympa/hostname
if [ -z "$RET" ]; then
	db_set sympa/hostname "$host"
fi
db_input medium sympa/hostname || [ $? -eq 30 ]
db_go

db_get sympa/listmaster
if [ -z "$RET" ]; then
	db_get sympa/hostname
	if [ "$listmaster" ]; then
		db_set sympa/listmaster "$listmaster"
	else
		db_set sympa/listmaster "listmaster@$RET"
	fi
fi
db_input medium sympa/listmaster || [ $? -eq 30 ]
db_go

if [ -f /usr/share/dbconfig-common/dpkg/config ]; then
	dbc_dbtypes="mysql, pgsql, sqlite3"
	dbc_authmethod_user="password"
	. /usr/share/dbconfig-common/dpkg/config
	dbc_first_version="5.3.4-6~"
	if [ -f "$conf" ] && dbctmpfile=$(tempfile -p sympa); then
		# Extract sympa.conf options parsable by dbconfig-common
		perl -ne 'print "$1=\"$2\"\n" if /^\s*(db_[a-z]+)\s*(.*)\s*$/' \
			"$conf" > "$dbctmpfile"
		dbc_load_include="sh:$dbctmpfile"
		dbc_load_include_args="-d db_name -p db_passwd -s db_host -P db_port -u db_user -t db_type"
	fi
	dbc_go sympa $@
	rm -f -- "$dbctmpfile"
fi

db_get wwsympa/wwsympa_url
if [ -z "$RET" ]; then
	wwsympa_url_default="http://${host}/wws"
	# Get the url from sympa.conf
	if [ -f "$conf" ]; then
		wwsympa_url_conf=$(perl -nle 'if (/^\s*wwsympa_url\s+(.*)$/) {print $1;}' "$conf")
		if [ -z "$wwsympa_url" ]; then
			db_get wwsympa/wwsympa_url
			wwsympa_url_conf="$RET"
		fi
	fi
	if [ -z "$wwsympa_url_conf" ]; then
		db_set wwsympa/wwsympa_url "$wwsympa_url_default"
	else
		db_set wwsympa/wwsympa_url "$wwsympa_url_conf"
	fi
fi

db_input medium wwsympa/wwsympa_url || [ $? -eq 30 ]
db_go

db_get wwsympa/fastcgi
if [ -z "$RET" ] && [ -f "$wwconf" ]; then
	fastcgi=$(perl -nle 'if (/^\s*use_fast_cgi\s+(\d)$/) {print $1;}' "$wwconf")
	case "$fastcgi" in
		1)
			fastcgi="true"
			;;
		*)
			fastcgi="false"
		;;
	esac
	db_set wwsympa/fastcgi "$fastcgi"
fi

# Ask for the installed web server
db_input high wwsympa/webserver_type || [ $? -eq 30 ]
db_go

db_get wwsympa/webserver_type
webserver="$RET"

if [ "$webserver" != "none" ]; then
	# Ask for fastCGI configuration
	db_input medium wwsympa/fastcgi || [ $? -eq 30 ]
	db_go
	# Ask for soap usage
	db_input medium sympa/use_soap || [ $? -eq 30 ]
	db_go
fi

# Ask for spool directories removal
db_input medium wwsympa/remove_spool || [ $? -eq 30 ]
db_go

