#!/bin/sh -e
# update-horde
#
# written by Ivan E. Moore II <rkrusty@tdyc.com> with help from
# debconf examples and tons of other references
#
# Source debconf library
. /usr/share/debconf/confmodule
db_version 2.0

# Where the config file is that stores the previous selections
HORDE_FILE="/etc/horde/horde.php3";
TEMPLATE_FILE="/etc/horde/horde.php3.in";

# Read in template
. "$TEMPLATE_FILE"

# Create a new temp file
TMPFILE=`mktemp /tmp/horde.XXXXXX` || exit 1

# Now let's create the horde.php3 file needed by HORDE
echo "Creating new horde.php3 file"
echo "<?php" >>$TMPFILE
echo "" >>$TMPFILE
echo "/*            DO NOT EDIT           */" >>$TMPFILE
echo "/*   AUTOMATICALLY GENERATED FILE   */" >>$TMPFILE
echo "/* Edit /etc/horde/horde.php3.in and then */" >>$TMPFILE
echo "/* use update-horde to generate this file */" >>$TMPFILE
echo "" >>$TMPFILE
echo "\$default->horde_root_url                 = '$horde_root_url';" >>$TMPFILE
echo "\$default->horde_graphics_url             = '$horde_graphics_url';" >>$TMPFILE
echo "\$default->faq_url                  = '$faq_url';" >>$TMPFILE
echo "\$default->horde_include_dir        = '$horde_include_dir';" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* Path to sendmail */" >>$TMPFILE
echo "\$default->path_to_sendmail         = '$path_to_sendmail';" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* Problem Reporting */" >>$TMPFILE
echo "\$default->problem_email             = '$problem_email';" >>$TMPFILE
echo "\$default->problem_reporting         = $problem_reporting;" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* Account Signup */" >>$TMPFILE
echo "\$default->signup_email              = '$signup_email';" >>$TMPFILE
echo "\$default->signup                    = $signup;" >>$TMPFILE
echo "" >>$TMPFILE
echo "?>" >>$TMPFILE

# Now lets move that file into place

mv $TMPFILE "$HORDE_FILE" > /dev/null 2>&1

##  Now take care of the rest of the conf files needing changed
db_get "horde/database_type"
dbtype="$RET"
db_get "horde/database_server"
dbserver="$RET"
db_get "horde/database_name"
dbname="$RET"
db_get "horde/database_user"
dbuser="$RET"
db_get "horde/database_pass"
dbpass="$RET"
db_get "horde/web_user"
webuser="$RET"
db_get "horde/web_group"
webgroup="$RET"
db_get "horde/mysql_pass"
mysqlpass="$RET"

case $dbtype in
  MySql)
    mydbtype="mysql"
    ;; 
  PostgreSQL)
    mydbtype="pgsql"
    ;;
esac


## Now take care of the phplib include file ##
  echo "Creating updated horde_phplib.inc file"
  /bin/sed -e "s#@dbname@#$dbname#; s#@dbserver@#$dbserver#; s#@dbuser@#$dbuser#; s#@dbpass@#$dbpass#" /etc/horde/horde_phplib.inc.in > /etc/horde/horde_phplib.inc

if [ "$dbtype" = "PostgreSQL" ]; then
 echo "Creating updated prepend.php3 file"
 /bin/sed -e 's#@dbtype@#pgsql#' /etc/horde/prepend.php3.in > /etc/horde/prepend.php3

fi

if [ "$dbtype" = "MySql" ]; then

 /bin/sed -e 's#@dbtype@#mysql#' /etc/horde/prepend.php3.in > /etc/horde/prepend.php3

fi

echo "Fixing file permissions"
chown -R root.root /usr/share/horde >> /dev/null 2>&1
chmod -R 555 /usr/share/horde/lib/*.lib >> /dev/null 2>&1
chmod 000 /usr/share/horde/setup.php3 >> /dev/null 2>&1
chown -R root.$webgroup /etc/horde >> /dev/null 2>&1
chmod 640 `find /etc/horde/ -type f -print ` >/dev/null 2>&1

db_stop
echo "Done!"
exit 0
