#!/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
#

# Source debconf library
. /usr/share/debconf/confmodule || exit
db_version 2.0

# Where the config file is that stores the previous selections
DEFAULTS_FILE="/etc/imp/defaults.php3";
TEMPLATE_FILE="/etc/imp/defaults.php3.in";

## Grab some extra db info from horde configs
  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/mysql_pass"
  mysqlpass="$RET"
  db_get "horde/path_to_sendmail"
  path_to_sendmail="$RET"

db_get "imp/configmethod"
if [ "$RET" = "auto" ]; then

db_get "imp/server"
server="$RET"
db_get "imp/servtype"
servtype="$RET"
db_get "imp/port"
port="$RET"
db_get "imp/from_server"
from_server="$RET"
db_get "imp/use_server_list"
use_server_list="$RET"
db_get "imp/root_url"
root_url="$RET"
db_get "imp/graphics_url"
graphics_url="$RET"
db_get "imp/include_dir"
include_dir="$RET"
db_get "imp/folders"
folders="$RET"
db_get "imp/save_sent_mail"
save_sent_mail="$RET"
db_get "imp/drafts"
drafts="$RET"
db_get "imp/sent_mail"
sent_mail="$RET"
db_get "imp/show_dotfiles"
show_dotfiles="$RET"
db_get "imp/error_level"
error_level="$RET"
db_get "imp/max_execution_time"
max_execution_time="$RET"
db_get "imp/path_to_mswordview"
path_to_mswordview="$RET"
db_get "imp/path_to_ispell"
path_to_ispell="$RET"
db_get "imp/path_to_tar"
path_to_tar="$RET"
db_get "imp/path_to_dpkg"
path_to_dpkg="$RET"
db_get "imp/path_to_rpm"
path_to_rpm="$RET"
db_get "imp/path_to_unzip"
path_to_unzip="$RET"
db_get "imp/minimum_popups"
minimum_popups="$RET"
db_get "imp/use_ldap_search"
use_ldap_search="$RET"
db_get "imp/use_imap_subscribe"
use_imap_subscribe="$RET"
db_get "imp/user_change_server"
user_change_server="$RET"
db_get "imp/user_change_port"
user_change_port="$RET"
db_get "imp/user_change_servtype"
user_change_servtype="$RET"
db_get "imp/user_change_folder"
user_change_folder="$RET"
db_get "imp/user_change_from"
user_change_from="$RET"
db_get "imp/user_change_fullname"
user_change_fullname="$RET"
db_get "imp/user_use_addressbook"
user_use_addressbook="$RET"
db_get "imp/user_use_folders"
user_use_folders="$RET"
db_get "imp/newmail_popup"
newmail_popup="$RET"
db_get "imp/refresh_delay"
refresh_delay="$RET"
db_get "imp/append_header"
append_header="$RET"
db_get "imp/append_trailer"
append_trailer="$RET"
db_get "imp/text_parts_inline"
text_parts_inline="$RET"
db_get "imp/language"
language="$RET"

else

    . "$TEMPLATE_FILE"

fi

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

# Now let's create the defaults.php3 file needed by IMP 

echo "<?php" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/*   DO NOT EDIT   */" >>$TMPFILE 2>&1
echo "/* AUTOMATICALLY GENERATED FILE  */" >>$TMPFILE 2>&1
echo "/* edit defaults.php3.in file instead */" >>$TMPFILE 2>&1
echo "/* then run update-imp to read in file */" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* true = put the compose window in the main frame; false = use a popup window for compose windows */" >>$TMPFILE 2>&1
echo "\$default->minimum_popups = $minimum_popups;" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* For debugging purposes */" >>$TMPFILE 2>&1
echo "\$default->error_level               = $error_level;" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* The longest that things like file uploads and slow functions should be" >>$TMPFILE 2>&1
echo " * allowed to run. 0 means run until termination (forever if infinite loop)." >>$TMPFILE 2>&1
echo " * NOTE: you can't set this to 0 if safe_mode is on. */ " >>$TMPFILE 2>&1
echo "\$default->max_execution_time        = $max_execution_time;" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* Imap Server Default */" >>$TMPFILE 2>&1
echo "\$default->server                    = '$server'; " >>$TMPFILE 2>&1
echo "\$default->from_server               = '$from_server'; " >>$TMPFILE 2>&1
echo "\$default->port                      = $port; " >>$TMPFILE 2>&1
echo "\$default->servtype                  = '$servtype'; " >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* Web server configuration */" >>$TMPFILE 2>&1
echo "\$default->root_url                  = '$root_url'; " >>$TMPFILE 2>&1
echo "\$default->include_dir               = '$include_dir'; " >>$TMPFILE 2>&1
echo "\$default->graphics_url              = '$graphics_url'; " >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* Folder configuration */" >>$TMPFILE 2>&1
echo "\$default->folders                   = '$folders'; " >>$TMPFILE 2>&1
echo "\$default->use_imap_subscribe        = $use_imap_subscribe; " >>$TMPFILE 2>&1
echo "\$default->show_dotfiles             = $show_dotfiles; " >>$TMPFILE 2>&1
echo "\$default->sent_mail                 = '$sent_mail'; " >>$TMPFILE 2>&1
echo "\$default->save_sent_mail            = $save_sent_mail; " >>$TMPFILE 2>&1
echo "\$default->drafts                    = '$drafts'; " >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* Ldap searching */" >>$TMPFILE 2>&1
echo "\$default->use_ldap_search           = $use_ldap_search;" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* user is presented with an list of available imap servers */" >>$TMPFILE 2>&1
echo "\$default->use_server_list           = $use_server_list;" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* User changable items */" >>$TMPFILE 2>&1
echo "\$default->user_change_server        = $user_change_server;" >>$TMPFILE 2>&1
echo "\$default->user_change_port          = $user_change_port;" >>$TMPFILE 2>&1
echo "\$default->user_change_servtype      = $user_change_servtype;" >>$TMPFILE 2>&1
echo "\$default->user_change_folder        = $user_change_folder;" >>$TMPFILE 2>&1
echo "\$default->user_change_from          = $user_change_from;" >>$TMPFILE 2>&1
echo "\$default->user_change_fullname      = $user_change_fullname;" >>$TMPFILE 2>&1
echo "\$default->user_use_addressbook      = $user_use_addressbook;" >>$TMPFILE 2>&1
echo "\$default->user_use_folders          = $user_use_folders;" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* New mail configuration */" >>$TMPFILE 2>&1
echo "/* Refresh between check for new mail */" >>$TMPFILE 2>&1
echo "\$default->refresh_delay                  = $refresh_delay;" >>$TMPFILE 2>&1
echo "\$default->newmail_popup                  = $newmail_popup;" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* Path to sendmail */" >>$TMPFILE 2>&1
echo "\$default->path_to_sendmail          = '$path_to_sendmail';" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* Ispell setup */" >>$TMPFILE 2>&1
echo "\$default->path_to_ispell            = '$path_to_ispell';" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* Mswordview setup */" >>$TMPFILE 2>&1
echo "\$default->path_to_mswordview        = '$path_to_mswordview';" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* path to tar so that tarballs can be listed */" >>$TMPFILE 2>&1
echo "\$default->path_to_tar               = '$path_to_tar';" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* path to rpm and dpkg so that packaging info can be listed */" >>$TMPFILE 2>&1
echo "/* These will only be usefull if you use Debian or RedHat mostly */" >>$TMPFILE 2>&1
echo "/* By default they are commented out.  You must uncomment the  */" >>$TMPFILE 2>&1
echo "/* appropriate lines in config/mime.php3 */" >>$TMPFILE 2>&1
echo "\$default->path_to_rpm               = '$path_to_rpm';" >>$TMPFILE 2>&1
echo "\$default->path_to_dpkg              = '$path_to_dpkg'; " >>$TMPFILE 2>&1
echo "\$default->path_to_unzip              = '$path_to_unzip'; " >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* Cyrus configuration block */" >>$TMPFILE 2>&1
echo "\$default->personal_folders          = '$personal_folders';" >>$TMPFILE 2>&1
echo "// cyrus configs may look like this:" >>$TMPFILE 2>&1
echo "// $default->personal_folders       = 'INBOX.';" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* Default Language */" >>$TMPFILE 2>&1
echo "\$default->language                  = '$language';" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "\$default->append_header             = $append_header;" >>$TMPFILE 2>&1
echo "\$default->append_trailer            = $append_trailer;" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "\$default->text_parts_inline         = $text_parts_inline;" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* database config */" >>$TMPFILE 2>&1
echo "\$default->use_db                    = true;" >>$TMPFILE 2>&1
echo "\$default->db_user_name              = '$dbuser';" >>$TMPFILE 2>&1
echo "\$default->db_password               = '$dbpass';" >>$TMPFILE 2>&1
echo "\$default->db_name                   = '$dbname';" >>$TMPFILE 2>&1
echo "\$default->db_server_name            = '$dbserver';" >>$TMPFILE 2>&1
echo "\$default->db_pref_table             = 'imp_pref';" >>$TMPFILE 2>&1
echo "\$default->db_address_table          = 'imp_addr';" >>$TMPFILE 2>&1
echo "\$default->db_connect_string         = '';" >>$TMPFILE 2>&1
echo "\$default->db_server_port            = '';" >>$TMPFILE 2>&1
echo "\$default->db_server_options         = '';" >>$TMPFILE 2>&1
echo "\$default->db_server_tty             = '';" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "?>" >>$TMPFILE 2>&1


# Now lets move that file into place

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

##  Now take care of the rest of the conf files needing changed

##  We need to figure out what type of database to use now:

db_get "imp/recheck"
hrecheck="$RET"

if [ "$dbtype" = "PostgreSQL" ]; then
 if [ "$hrecheck" = "yes" ]; then
  if (!(grep 'extension' /etc/php3/apache/php3.ini | grep 'imap.so'>/dev/null 2>&1)); 
  then 
    echo 'extension=imap.so' >> /etc/php3/apache/php3.ini 2>&1
  fi
  if (!(grep 'extension' /etc/php3/apache/php3.ini | grep 'ldap.so'>/dev/null 2>&1));
  then
    echo 'extension=ldap.so' >> /etc/php3/apache/php3.ini 2>&1
  fi
 fi
  /bin/sed -e "s#@dbname@#$dbname#; s#@dbuser@#$dbuser#" /usr/share/doc/imp/examples/pgsql_create.sql.in > /etc/imp/pgsql_create.sql
  chown postgres /etc/imp/pgsql_create.sql

  db_get "imp/dbconfigured"
  if [ "$RET" = "no" ] ; then

    su - postgres -c "/usr/bin/psql -q template1 < /etc/imp/pgsql_create.sql >/dev/null 2>&1"
    db_set "imp/dbconfigured" "yes"
  fi
  /etc/init.d/postgresql restart > /dev/null 2>&1
  cd /usr/share/horde/imp/lib/src && ./build.pl --database=postgres >/dev/null 2>&1

fi

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

    /bin/sed -e "s#@dbname@#$dbname#; s#@dbuser@#$dbuser#; s#@dbpass@#$dbpass#; s#@dbserver@#$dbserver#" /usr/share/doc/imp/examples/mysql_create.sql.in > /etc/imp/mysql_create.sql
 
 if [ "$hrecheck" = "yes" ]; then

  if (!(grep 'extension' /etc/php3/apache/php3.ini | grep 'imap.so'>/dev/null 2>&1)); 
  then 
    echo 'extension=imap.so' >> /etc/php3/apache/php3.ini 2>&1
  fi
  if (!(grep 'extension' /etc/php3/apache/php3.ini | grep 'ldap.so'>/dev/null 2>&1)); 
  then 
    echo 'extension=ldap.so' >> /etc/php3/apache/php3.ini 2>&1
  fi    
 fi

  db_get "imp/dbconfigured"
  if [ "$RET" = "no" ] ; then
    if [ "$dbpass" = "none" ] ; then
      /usr/bin/mysql -f < /etc/imp/mysql_create.sql >/dev/null 2>&1
    else
      /usr/bin/mysql -f --password=$mysqlpass < /etc/imp/mysql_create.sql > /dev/null 2>&1
    fi
    db_set imp/dbconfigured "yes"
    db_go
  fi
  /etc/init.d/mysql restart >/dev/null 2>&1
  cd /usr/share/horde/imp/lib/src && ./build.pl --database=mysql >> /dev/null 2>&1
      
fi

###  Done with the database stuff, move on to the next item on the list

if [ "$hrecheck" = "yes" ]; then

db_get "horde/webserver_type"
webtype="$RET";
if [ "$webtype" = "Apache" ] || [ "$webtype" = "Both" ]; then
  if (!(grep 'Added for IMP' /etc/apache/srm.conf >/dev/null 2>&1)); \
  then \
    echo ' ' >> /etc/apache/srm.conf 2>&1 ;\
    echo '# Added for IMP' >> /etc/apache/srm.conf 2>&1 ;\
    echo 'Alias /imp /usr/share/horde/imp' >> /etc/apache/srm.conf 2>&1 ; \
  fi
fi

if [ "$webtype" = "Apache-SSL" ] || [ "$webtype" = "Both" ]; then
  if (!(grep 'Added for IMP' /etc/apache-ssl/srm.conf >/dev/null 2>&1)); \
  then \
    echo ' ' >> /etc/apache-ssl/srm.conf 2>&1 ;\
    echo '# Added for IMP' >> /etc/apache-ssl/srm.conf 2>&1 ;\
    echo 'Alias /imp /usr/share/horde/imp' >> /etc/apache-ssl/srm.conf 2>&1; \
  fi
fi

fi

db_input "critical" "imp/recheck" || true
db_go

chown -R www-data.www-data /usr/share/horde >> /dev/null 2>&1
chown -R www-data.www-data /etc/imp >> /dev/null 2>&1
chmod -R 555 /usr/share/horde/imp/lib/*.lib >> /dev/null 2>&1
chmod 555 `find /etc/imp/ -type f -print ` >/dev/null 2>&1
chmod o-rwx `find /etc/imp/ -type f -print ` >/dev/null 2>&1
if [ -f /etc/imp/pgsql_create.sql ]; then
  chown postgres /etc/imp/pgsql_create.sql >>/dev/null 2>&1
  /etc/init.d/postgresql stop > /dev/null 2>&1
  sleep 1
  /etc/init.d/postgresql start > /dev/null 2>&1
fi

if [ "$webtype" = "Apache" ] || [ "$webtype" = "Both" ]; then
  /etc/init.d/apache stop > /dev/null 2>&1
  sleep 1
  /etc/init.d/apache start > /dev/null 2>&1
fi
if [ "$webtype" = "Apache-SSL" ] || [ "$webtype" = "Both" ]; then
  /etc/init.d/apache-ssl stop > /dev/null 2>&1
  sleep 1
  /etc/init.d/apache-ssl start > /dev/null 2>&1
fi

#DEBHELPER#

db_stop 
exit 0
