#!/bin/sh -e
# Horde postinst script using debconf
# written by Ivan E. Moore II <rkrusty@tdyc.com> with help from
# debconf examples and tons of other references
#
# All questions should have been asked via debconf 
# now we just go get those, build a temp file which we read in and
# then build our config files and set everthing up
# Source debconf library
. /usr/share/debconf/confmodule.sh

# Where the config file is that stores the previous selections
OPTIONS_FILE="/etc/horde/horde.options";
CHOICES_FILE="/etc/horde/horde.choices";
HORDE_FILE="/etc/horde/horde.php3";

# remove choices file if it exists since we will rebuild it from debconf info
if [ -f "$CHOICES_FILE" ]; then
   rm -f $CHOICES_FILE
fi

## Ok set our temp file to use
p=`basename $0`
TMPFILE=`mktemp /tmp/$p.XXXXXX` || exit 1

### Set the shell variable so that it can be sourced in
echo "#!/bin/sh" >>$TMPFILE 
echo "# Config file read in by HORDE setup program" >>$TMPFILE
echo "# Autobuilt by debconf/hordeconf Do not edit" >>$TMPFILE
echo "#" >>$TMPFILE
echo "#  DO NOT EDIT" >>$TMPFILE
echo "#" >>$TMPFILE
echo "# configure program (horde.php3)" >>$TMPFILE

for i in `cat $OPTIONS_FILE | grep -v "#"`; do
  db_get "horde/$i"
  echo "$i=\"$RET\";" >>$TMPFILE
done

db_get "horde/signup"
echo "signup=\"$RET\";" >>$TMPFILE
db_get "horde/signup_email"
echo "signup_email=\"$RET\";" >>$TMPFILE
db_get "horde/problem_reporting"
echo "problem_reporting=\"$RET\";" >>$TMPFILE
db_get "horde/problem_email"
echo "problem_email=\"$RET\";" >>$TMPFILE

# Now drop the newly created file into it's place
mv $TMPFILE $CHOICES_FILE > /dev/null 2>&1;
chmod 755 $CHOICES_FILE
chmod go-rwx $CHOICES_FILE

# Now read the new file in
. $CHOICES_FILE

if [ -f $CHOICES_FILE ]; then
  rm -f $CHOICES_FILE
fi

# Create a new temp file
p=`basename $0`
TMPFILE=`mktemp /tmp/$p.XXXXXX` || exit 1

# Now let's create the horde.php3 file needed by HORDE

echo "<?php" >>$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"

## Now take care of the phplib include file ##

  /bin/sed -e "s#@dbname@#$dbname#; s#@dbserver@#$dbserver#; s#@dbuser@#$dbuser#; s#@dbpass@#$dbpass#" /etc/horde/horde_phplib.in > /etc/horde/horde_phplib.inc

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

 /bin/sed -e "s#@dbname@#$dbname#" /usr/share/doc/horde/examples/pgsql_create.sql > /usr/share/doc/horde/examples/pgsql_create.sql.new
 cp /usr/share/doc/horde/examples/pgsql_create.sql /usr/share/doc/horde/examples/pgsql_create.sql.bak >/dev/null 2>&1
 mv /usr/share/doc/horde/examples/pgsql_create.sql.new /usr/share/doc/horde/examples/pgsql_create.sql >/dev/null 2>&1

 /bin/sed -e 's#\# PGALLOWTCPIP=no#PGALLOWTCPIP=yes#; s#\# PGALLOWTCPIP=yes#PGALLOWTCPIP=yes#' /etc/postgresql/postmaster.init >/etc/postgresql/postmaster.init.tmp
 cp /etc/postgresql/postmaster.init /etc/postgresql/postmaster.init.horde.back > /dev/null 2>&1
 if (grep 'PGALLOWTCPIP=yes' /etc/postgresql/postmaster.init.tmp >/dev/null 2>&1); 
 then 
   (mv /etc/postgresql/postmaster.init.tmp /etc/postgresql/postmaster.init > /dev/null 2>&1); 
 fi
 /etc/init.d/postgresql restart > /dev/null 2>&1
 sleep 2
 if (grep 'extension' /etc/php3/apache/php3.ini | grep 'pgsql.so'>/dev/null 2>&1); then (notsome="1"); else (echo 'extension=pgsql.so' >> /etc/php3/apache/php3.ini); fi 
 /bin/sed -e 's#@dbtype@#pgsql#' /etc/horde/prepend.php3 > /etc/horde/prepend.php3.new
 cp /etc/horde/prepend.php3 /etc/horde/prepend.php3.back
 mv /etc/horde/prepend.php3.new /etc/horde/prepend.php3 > /dev/null 2>&1

 # Now do some database stuff

 db_get "horde/dbconfigured"
 if [ "$RET" = "no" ]; then
   PSQL2_QUERY="GRANT SELECT, INSERT, UPDATE ON active_sessions, auth_user, auth_user_md5 TO $dbuser"
   su - postgres -c "/usr/share/doc/horde/examples/pgsql_cuser.sh $dbuser $dbpass >/dev/null 2>&1"
   su - postgres -c '/usr/bin/psql -q template1 < /usr/share/doc/horde/examples/pgsql_create.sql >/dev/null 2>&1'
   su - postgres -c '/usr/bin/psql -q template1 -c "$PSQL2_QUERY" >/dev/null 2>&1'
   db_set "horde/dbconfigured" "yes"
 fi
 /etc/init.d/postgresql restart > /dev/null 2>&1
 cd /usr/share/horde/lib/src && ./build.pl --database=postgres >> /dev/null 2>&1
fi

if [ "$dbtype" = "MySql" ]; then
  /bin/sed -e "s#@dbname@#$dbname#; s#@dbserver@#$dbserver#; s#@dbuser@#$dbuser#; s#@dbpass@#$dbpass#" /usr/share/doc/horde/examples/mysql_create.sql > /usr/share/doc/horde/examples/mysql_create.sql.new
  cp /usr/share/doc/horde/examples/mysql_create.sql /usr/share/doc/horde/examples/mysql_create.sql.bak >/dev/null 2>&1
  mv /usr/share/doc/horde/examples/mysql_create.sql.new /usr/share/doc/horde/examples/mysql_create.sql >/dev/null 2>&1
  chmod go-rwx /usr/share/doc/horde/examples/mysql_create.sql

  if (grep 'extension' /etc/php3/apache/php3.ini | grep 'imap.so'>/dev/null 2>&1); then (notsome="1"); else (echo 'extension=imap.so' >> /etc/php3/apache/php3.ini); fi
  if (grep 'extension' /etc/php3/apache/php3.ini | grep 'ldap.so'>/dev/null 2>&1); then (notsome="1"); else (echo 'extension=ldap.so' >> /etc/php3/apache/php3.ini ); fi 
  if (grep 'extension' /etc/php3/apache/php3.ini | grep 'mysql.so'>/dev/null 2>&1); then (notsome="1"); else (echo 'extension=mysql.so' >> /etc/php3/apache/php3.ini); fi
        
 /bin/sed -e 's#@dbtype@#mysql#' /etc/horde/prepend.php3 > /etc/horde/prepend.php3.new
 cp /etc/horde/prepend.php3 /etc/horde/prepend.php3.back              
 cp /etc/horde/prepend.php3.new /etc/horde/prepend.php3 > /dev/null 2>&1
 db_get "horde/dbconfigured"
 if [ "$RET" = "no" ] ; then 
   if [ "$db_pass" = "none" ] ; then
     /usr/bin/mysql -f < /usr/share/doc/horde/examples/mysql_create.sql >/dev/null 2>&1
   else
     /usr/bin/mysql -f --password=$db_pass < /usr/share/doc/horde/examples/mysql_create.sql > /dev/null 2>&1
   fi
   db_set "horde/dbconfigured" "yes"
 fi
 /etc/init.d/mysql restart >/dev/null 2>&1
 cd /usr/share/horde/lib/src && ./build.pl --database=mysql >> /dev/null 2>&1
fi

db_get "horde/webserver_type"
webtype="$RET";
if [ "$webtype" = "Apache" ] || [ "$webtype" = "Both" ]; then  
  /bin/sed -e 's#\# LoadModule php3_module#LoadModule php3_module#' /etc/apache/httpd.conf >/etc/apache/httpd.conf.tmp
  cp /etc/apache/httpd.conf /etc/apache/httpd.conf.horde.back > /dev/null 2>&1
  if (grep 'php3_module' /etc/apache/httpd.conf.tmp >/dev/null 2>&1);
  then
    (mv /etc/apache/httpd.conf.tmp /etc/apache/httpd.conf);
  fi
  if (!(grep 'Added for HORDE' /etc/apache/access.conf >/dev/null 2>&1));  \
  then \
    echo ' ' >> /etc/apache/access.conf ;\
    echo '# Added for HORDE' >> /etc/apache/access.conf ;\
    echo '<Directory /usr/share/horde>' >> /etc/apache/access.conf ;\
    echo 'Options Indexes FollowSymLinks' >> /etc/apache/access.conf ;\
    echo 'AllowOverride None' >> /etc/apache/access.conf ;\
    echo 'order allow,deny' >> /etc/apache/access.conf ;\
    echo 'allow from all' >> /etc/apache/access.conf ;\
    echo 'php3_include_path /etc/horde:/usr/lib/phplib' >> /etc/apache/access.conf ;\
    echo 'php3_auto_prepend_file /etc/horde/prepend.php3' >> /etc/apache/access.conf ;\
    echo 'php3_magic_quotes_gpc Off' >> /etc/apache/access.conf ;\
    echo '</Directory>' >> /etc/apache/access.conf ;\
    echo '# End HORDE Configuration Block' >> /etc/apache/access.conf ;\ 
  fi

  if (!(grep 'Added for HORDE' /etc/apache/srm.conf >/dev/null 2>&1)); \
  then \
    echo '# Added for HORDE' >> /etc/apache/srm.conf ;\
    echo 'Alias /horde /usr/share/horde' >> /etc/apache/srm.conf ;\
  fi
  /etc/init.d/apache reload >/dev/null 2>&1
fi
if [ "$webtype" = "Apache-SSL" ] || [ "$webtype" = "Both" ]; then
  /bin/sed -e 's#\# LoadModule php3_module#LoadModule php3_module#' /etc/apache-ssl/httpd.conf >/etc/apache-ssl/httpd.conf.tmp
  cp /etc/apache-ssl/httpd.conf /etc/apache-ssl/httpd.conf.horde.back > /dev/null 2>&1
  if (grep 'php3_module' /etc/apache-ssl/httpd.conf.tmp >/dev/null 2>&1);
  then
    (mv /etc/apache-ssl/httpd.conf.tmp /etc/apache-ssl/httpd.conf);
  fi
  if (!(grep 'Added for HORDE' /etc/apache-ssl/access.conf >/dev/null 2>&1)); \
  then \
    echo ' ' >> /etc/apache-ssl/access.conf ;\
    echo '# Added for HORDE' >> /etc/apache-ssl/access.conf ;\
    echo '<Directory /usr/share/horde>' >> /etc/apache-ssl/access.conf ;\
    echo 'Options Indexes FollowSymLinks' >> /etc/apache-ssl/access.conf ;\
    echo 'AllowOverride None' >> /etc/apache-ssl/access.conf ;\
    echo 'order allow,deny' >> /etc/apache-ssl/access.conf ;\
    echo 'allow from all' >> /etc/apache-ssl/access.conf ;\
    echo 'php3_include_path /etc/horde:/usr/lib/phplib' >> /etc/apache-ssl/access.conf ;\
    echo 'php3_auto_prepend_file /etc/horde/prepend.php3' >> /etc/apache-ssl/access.conf ;\
    echo 'php3_magic_quotes_gpc Off' >> /etc/apache-ssl/access.conf ;\
    echo '</Directory>' >> /etc/apache-ssl/access.conf ;\
    echo '# End HORDE Configuration Block' >> /etc/apache-ssl/access.conf ;\
  fi

  if (!(grep 'Added for HORDE' /etc/apache-ssl/srm.conf >/dev/null 2>&1)); \
  then \
    echo '# Added for HORDE' >> /etc/apache-ssl/srm.conf ;\
    echo 'Alias /horde /usr/share/horde' >> /etc/apache-ssl/srm.conf ;\
  fi
  /etc/init.d/apache-ssl reload >/dev/null 2>&1
fi

chown -R www-data.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 www-data.root /etc/horde >> /dev/null 2>&1
chmod 555 `find /etc/horde/ -type f -print ` >/dev/null 2>&1
chmod o-rwx `find /etc/horde/ -type f -print ` >/dev/null 2>&1

#DEBHELPER#

db_stop
exit 0
