#! /bin/sh -e
#
# Copyright 2000,2001,2002,2003,2004,2005,2007,2009 by Stefan Hornburg (Racke) <racke@linuxia.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA  02110-1301  USA.

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

if [ -e /etc/interchange/init.cfg ]; then
	# Read current state from configuration file
	. /etc/interchange/init.cfg || true
	if [ "$MODE" = "--unix" ]; then
		db_set interchange/mode "unix mode"
	elif [ "$MODE" = "--inetmode" ]; then
		db_set interchange/mode "internet mode"
	else 
		db_set interchange/mode "both"
	fi
	db_set interchange/group $GROUP
	db_set interchange/user $USER
	if [ "$DOCROOT" ]; then
		db_set interchange/docroot $DOCROOT
	fi
	if [ "$GPGHOME" ]; then
		db_set interchange/gpghome $GPGHOME
	fi
fi

# Which mode we should use ?
db_input medium interchange/mode || [ $? -eq 30 ]
db_go

# Remember old interchange user/group configuration

db_get interchange/user || [ $? -eq 30 ]
OLDUSER=$RET
db_set interchange/olduser $OLDUSER

# Which user should be choosed for the interchange server ?

HAVEUSER=0
while [ $HAVEUSER -eq 0 ]; do
	db_input medium interchange/user || [ $? -eq 30 ]
	db_go
	db_get interchange/user
	USER=$RET

	if [ -z "$USER" ]; then
		USER=interchange
	fi

	if [ "$USER" = root ]; then
		db_input medium interchange/usernoroot || [ $? -eq 30 ]
		db_go
	elif grep -q ^$USER: /etc/passwd; then
		HAVEUSER=1
	else
		db_subst interchange/createuser USER $USER
		db_input medium interchange/createuser || [ $? -eq 30 ]
		db_go
		db_get interchange/createuser || [ $? -eq 30 ]
		CREATEUSER=$RET
		if [ "$CREATEUSER" = true ]; then
			HAVEUSER=1
		fi
	fi

	if [ $HAVEUSER -eq 0 ]; then
		db_fset interchange/user isdefault true
		db_reset interchange/user
		db_fset interchange/createuser isdefault true
		db_reset interchange/createuser
	fi
done

# Which group should be choosed for the interchange server ?

HAVEGROUP=0
while [ $HAVEGROUP -eq 0 ]; do
	db_input medium interchange/group || [ $? -eq 30 ]
	db_go
	db_get interchange/group
	GROUP=$RET

	if [ -z "$GROUP" ]; then
		GROUP=interchange
	fi

	if [ "$GROUP" = root ]; then
		db_input medium interchange/groupnoroot || [ $? -eq 30 ]
		db_go
	elif grep -q ^$GROUP: /etc/group; then
		HAVEGROUP=1
	else
		db_subst interchange/creategroup GROUP $GROUP
		db_input medium interchange/creategroup || [ $? -eq 30 ]
		db_go
		db_get interchange/creategroup || [ $? -eq 30 ]
		CREATEGROUP=$RET
		if [ "$CREATEGROUP" = true ]; then
			HAVEGROUP=1
		fi
	fi

	if [ $HAVEGROUP -eq 0 ]; then
		# no appropriate group, reset and try again
		db_fset interchange/group isdefault true
		db_reset interchange/group
		db_fset interchange/creategroup isdefault true
		db_reset interchange/creategroup
	fi
done

# Settings
if [ -f /etc/interchange/settings.cfg ]; then
	exec 4</etc/interchange/settings.cfg
	while read <&4 variable name value; do
		if [ "$variable" = "Variable" ]; then
			FLAG=false
			if [ "$value" = "1" ]; then
				FLAG=true
			fi
			if [ "$name" = "FULL_URL" ]; then				
				db_set interchange/full_url $FLAG
			fi
			if [ "$name" = "ROBOTS" ]; then
				db_set interchange/robots $FLAG
			fi
			if [ "$name" = "SOAP" ]; then
				db_set interchange/soap $FLAG
			fi
			if [ "$name" = "TRAFFIC" ]; then
				db_set interchange/traffic $value
			fi
		fi
	done
fi

# FullURL directive
db_input low interchange/full_url || [ $? -eq 30 ]
db_go

# SOAP server
if perl -MSOAP::Lite -e '' 2>/dev/null; then
	db_set interchange/cansoap true
	db_input low interchange/soap || [ $? -eq 30 ]
else
	db_input low interchange/cansoap || [ $? -eq 30 ]
fi

# Inclusion of robots directives
db_input low interchange/robots || [ $? -eq 30 ]
db_go

# Traffic mode
db_input medium interchange/traffic || [ $? -eq 30 ]
db_go

# GNUPG Home
db_input low interchange/gpghome || [ $? -eq 30 ]
db_go

# Catalog configuration
db_input medium interchange/docroot || [ $? -eq 30 ]
db_go
