#! /bin/sh -e
#
# Copyright 2001,2002,2003,2004,2006,2007,2009,2010 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

# Get interchange user and group
db_get interchange/user
USER=$RET
db_get interchange/group
GROUP=$RET

# Creating interchange group if he isn't already there
if ! grep -q ^$GROUP: /etc/group; then
        echo Adding system group: $GROUP.
        addgroup --system $GROUP
fi

# Creating interchange user if he isn't already there
if ! grep -q ^$USER: /etc/passwd; then
        echo Adding system user: $USER.
        adduser --system --ingroup $GROUP --home /usr/lib/interchange $USER
fi

# Ensure correct permissions for directories with log files
chown -R $USER.$GROUP /var/log/interchange
chmod 770 /var/log/interchange
chown -R $USER.$GROUP /usr/lib/cgi-bin/ic
chmod u+s /usr/lib/cgi-bin/ic/*

# Ensure correct permissions for catalog base directory
# and static HTML base directory

chown $USER.$GROUP /var/lib/interchange/catalogs
db_get interchange/docroot
if [ -d "$RET" ]; then
	chown $USER.$GROUP "$RET"
fi

# More permissions

chown -R $USER.$GROUP /etc/interchange/usertag
chown -R $USER.$GROUP /var/lib/interchange/catalog.d

# Record debconf configuration in multiple files
# 1. Stuff needed for the init script

INITCFG=/etc/interchange/init.cfg
cat > $INITCFG <<EOF
# This file is automatically generated !
#
# YOU MAY MODIFY THIS FILE
# But we recommend to use dpkg-reconfigure interchange instead.

EOF

db_get interchange/mode
case "$RET" in
	"unix mode") MODE=--unix;;
	"internet mode") MODE=--inetmode;;
	"both") MODE="--unix --inetmode";;
esac
echo MODE=\"$MODE\" >> $INITCFG
echo USER=$USER >> $INITCFG
echo GROUP=$GROUP >> $INITCFG

db_get interchange/docroot
echo DOCROOT=$RET >> $INITCFG

db_get interchange/gpghome
echo GPGHOME=$RET >> $INITCFG

# 2. Settings which influence the global configuration
# and make senses to be configured by debconf

db_get interchange/full_url
if [ "$RET" = true ]; then
	FULL_URL=1
else
	FULL_URL=0
fi
db_get interchange/cansoap
SOAP=0
if [ "$RET" = true ]; then
	db_get interchange/soap
	if [ "$RET" = true ]; then
		SOAP=1
	fi
fi
db_get interchange/traffic
TRAFFIC="$RET"

interchangeconfig FULL_URL=$FULL_URL SOAP=$SOAP TRAFFIC=$RET

# Make configuration files owned by the interchange user
chown -R $USER.$GROUP /etc/interchange

# Create directory for static HTML files and grant it to the
# interchange user
db_get interchange/docroot
DOCROOT="$RET"
if [ "$DOCROOT" ]; then
	mkdir -p -m 755 $DOCROOT
	chown $USER.$GROUP $DOCROOT
fi

# Check if we delay the startup
if [ -f /var/run/interchange-install ]; then
	PACKAGES=""
	exec 4</var/run/interchange-install
	while read <&4 PKG; do
		if [ ! "$PKG" = "interchange" ]; then
			PACKAGES="$PKG $PACKAGES"
		fi
	done
	if [ -z "$PACKAGES" ]; then
		rm /var/run/interchange-install
	else 
		exec 5>/var/run/interchange-install
		for PKG in $PACKAGES; do
			echo "$PKG" >&5
		done
	fi
fi

#DEBHELPER#

# Don't wait on Interchange to close file handles
db_stop

exit 0

