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

# 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"
if [ "$RET" = "MySql" ]; then
  db_reset "horde/mysql_pass"
  db_input "critical" "horde/mysql_pass" || true
  db_go
  db_get "horde/mysql_pass"
  mysqlpass="$RET"
  db_reset "horde/mysql_pass"
fi
  db_get "horde/phpversion"
  phpver="$RET"
  db_get "horde/web_user"
  webuser="$RET"
  db_get "horde/web_group"
  webgroup="$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/webserver_type"
  webtype="$RET"
  db_get "horde/dblocal"
  dblocal="$RET"
  db_get "horde/restart"
  restart="$RET"
  db_get "imp/server"
  server="$RET"
  db_get "imp/port"
  port="$RET"
  db_get "imp/servtype"
  servtype="$RET"

  . "$TEMPLATE_FILE"

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

# Allows us to loop and substitute in one pass
case $webtype in
  Apache)
    mywebtype="apache"
    ;;
  Apache-SSL)
    mywebtype="apache-ssl"
    ;;
  Both)
    mywebtype="apache apache-ssl"
    ;;
  *)
    mywebtype="none"
    ;;
esac


# 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->postponed                    = '$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_change_language      = $user_change_language;" >>$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 "\$default->path_to_xlHtml        = '$path_to_xlHtml';" >>$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 "// how big a part can be (in k) before we won\'t display it inline. 0 means" >>$TMPFILE 2>&1
echo "// no size limit." >>$TMPFILE 2>&1
echo "\$default->text_inline_size          = '$text_inline_size';" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "/* database config */" >>$TMPFILE 2>&1
echo "\$default->use_db                    = true;" >>$TMPFILE 2>&1
echo "\$default->database_driver           = '$mydbtype';" >>$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 "\$default->path_to_zipinfo           = '$zipinfo';" >>$TMPFILE 2>&1
echo "\$default->path_to_xlHtml            = '$path_to_xlHtml';" >>$TMPFILE 2>&1
echo "\$default->hide_deleted              = '$hide_deleted';" >>$TMPFILE 2>&1
echo "\$default->menu_contacts_link        = '$menu_contacts_link';" >>$TMPFILE 2>&1
echo "\$default->compose_date_format       = '$compose_date_format';" >>$TMPFILE 2>&1
echo "\$default->date_format               = '$date_format';" >>$TMPFILE 2>&1
echo "\$default->time_format               = '$time_format'; " >>$TMPFILE 2>&1
echo "\$default->inline_in_parts_list      = '$inline_in_parts_list'; " >>$TMPFILE 2>&1
echo "\$default->quote_prefix              = '$quote_prefix';" >>$TMPFILE 2>&1
echo "\$default->newuser_link = $newuser_link;" >>$TMPFILE 2>&1
echo "\$default->to_domain = $to_domain;" >>$TMPFILE 2>&1
echo "\$default->show_shared_hierarchy = $show_shared_hierarchy;" >>$TMPFILE 2>&1
echo "\$default->show_public_hierarchy = $show_public_hierarchy;" >>$TMPFILE 2>&1
echo "\$default->show_news_hierarchy = $show_news_hierarchy;" >>$TMPFILE 2>&1
echo "\$default->view_message_source = $view_message_source;" >>$TMPFILE 2>&1
echo "\$default->log_stats = $log_stats;" >>$TMPFILE 2>&1
echo "\$default->log_stats_facil = $log_stats_facil; " >>$TMPFILE 2>&1
echo "\$default->log_prio = $log_prio; " >>$TMPFILE 2>&1
echo "\$default->log_ident = '$log_ident'; " >>$TMPFILE 2>&1
echo "\$default->log_auth = $log_auth;" >>$TMPFILE 2>&1
echo "\$default->log_auth_facil = $log_auth_facil; " >>$TMPFILE 2>&1
echo "\$default->change_password = '$change_password';" >>$TMPFILE 2>&1
echo "\$default->poppassd_server = '$poppassd_server'; " >>$TMPFILE 2>&1
echo "\$default->poppassd_port = '$poppassd_port';" >>$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 root.root /usr/share/horde >> /dev/null 2>&1
chown -R root.$webgroup /etc/imp >> /dev/null 2>&1
chmod -R 555 /usr/share/horde/imp/lib/*.lib >> /dev/null 2>&1
chmod 640 `find /etc/imp/ -type f -print ` >/dev/null 2>&1

db_stop 
echo "Done!"
exit 0
