#!/bin/sh -e
# update-imp
# 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

case "$1" in
    debconf)
         db_set "horde/debconf" "yes"
         echo "Setting configuration method to: debconf"
         exit 0
    ;;
esac
echo ""
echo "Setting configuration method to: Manual"
echo ""
echo "To change back to the debconf method run: update-imp debconf"
echo ""
db_set "horde/debconf" "no"


# 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"

  . "$TEMPLATE_FILE"

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

# Now let's create the defaults.php3 file needed by IMP 
echo "Building defaults.php3 file"
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
cat /etc/imp/custom_from.in >>$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 "/* Set the process's umask. */ " >>$TMPFILE 2>&1
echo "\$default->umask                     = '$umask';  " >>$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 "\$default->custom_from_hook          = $custom_from_hook;" >>$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

echo "Fixing up file permissions"
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

db_stop 
echo "Done!"
exit 0
