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

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

# These shortnames allow us to make sed statements common
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

# Extensions to verify for php3
extensions="ldap imap"

echo "Getting data from debconf"
db_get "imp/server"
server="$RET"
db_get "imp/servtype"
servtype="$RET"
db_get "imp/port"
port="$RET"

. /etc/imp/defaults.php3.in

# 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
cat /etc/imp/custom_from.in >>$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->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_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 "\$default->inline_in_parts_list      = $inline_in_parts_list;" >>$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->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->user_change_language      = '$user_change_language';" >>$TMPFILE 2>&1
echo "\$default->date_format               = '$date_format';" >>$TMPFILE 2>&1
echo "\$default->time_format               = '$time_format'; " >>$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

##  We need to figure out what type of database to use now:
  echo "Setting up db file"
  /bin/sed -e "s#@dbname@#$dbname#; s#@dbuser@#$dbuser#; s#@dbserver@#$dbserver#; s#@dbpass@#$dbpass#" /usr/share/doc/imp/examples/${mydbtype}_create.sql.in > /etc/imp/${mydbtype}_create.sql


db_get "horde/horderecheck"
hrecheck="$RET"

if [ "$hrecheck" = "yes" ]; then
  echo "Verifying Non-IMP config files"
  if [ "$mywebtype" = "none" ]; then
    echo "Not configuring web server"
  else 
    for server in $mywebtype; do
      if grep 'Added for IMP' /etc/$server/srm.conf >/dev/null 2>&1; then
        echo "No need to fix /etc/$server/srm.conf"
      else
        echo "Fixing /etc/$server/srm.conf"
        echo ' ' >> /etc/$server/srm.conf 2>&1 
        echo '# Added for IMP' >> /etc/$server/srm.conf 2>&1 
        echo 'Alias /imp /usr/share/horde/imp' >> /etc/$server/srm.conf 2>&1 
      fi
    done
  fi
  if [ "$phpver" = "php3" ]; then  
    if [ -f /etc/php3/apache/php3.ini ]; then
      # Make sure all the extensions are being loaded
      for ext in $extensions; do
        if egrep "^[[:space:]]*extension.*$ext\.so" /etc/php3/apache/php3.ini >/dev/null 2>&1; then
          echo "php3 extension for $ext found!"
        else
          echo "php3 extension for $ext added!"
          echo "extension=$ext.so" >>/etc/php3/apache/php3.ini
        fi
      done
    fi
  fi
  if [ "$phpver" = "php4" ]; then  
   if [ -f /etc/php4/apache/php.ini ]; then
      # Make sure all the extensions are being loaded
      for ext in $extensions; do
        if egrep "^[[:space:]]*extension.*$ext\.so" /etc/php4/apache/php.ini >/dev/null 2>&1; then
          echo "php4 extension for $ext found!"
        else
          echo "php4 extension for $ext added!"
          echo "extension=$ext.so" >>/etc/php4/apache/php.ini
        fi
      done
    fi
  fi
  echo "Finished checking Non-IMP config files"
else
  echo "Not checking Non-IMP config files at your request"
fi

if [ "$dblocal" = "yes" ]; then
  db_get "imp/dbconfigured"
  if [ "$RET" = "no" ] ; then
    if [ "$dbtype" = "PostgreSQL" ]; then
      if [ -f /etc/postgresql/postmaster.init ]; then
        echo "Configuring PostgreSQL database"
        chown postgres /etc/imp/pgsql_create.sql
        su postgres -c "/usr/bin/psql -q -f /etc/imp/pgsql_create.sql template1 >/dev/null 2>&1"
        if [ "$restart" = "database" ] || [ "$restart" = "both" ]; then
          echo "Restarting PostgreSQL"
          /etc/init.d/postgresql restart > /dev/null 2>&1
          sleep 2
        fi
        db_set "imp/dbconfigured" "yes"
        db_go
      else
        echo "      **** WARNING ****  "
        echo "You stated your using PostgreSQL as a database and"
        echo "that it is local yet the install program cannot seem"
        echo "to find the config files for it.  The install program"
        echo "will NOT confiugre your database for use with HORDE"
        echo "and thus will not function properly until you get this"
        echo "fixed.  "
        echo " "
        echo "Please press ENTER"
        read q
      fi
    fi

    if [ "$dbtype" = "MySql" ]; then
      echo "Configuring MySQL database"
      if [ "$mysqlpass" = "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
      if [ "$restart" = "database" ] || [ "$restart" = "both" ]; then
        echo "Restarting MySQL"
        /etc/init.d/mysql restart >/dev/null 2>&1
      fi
      db_set "imp/dbconfigured" "yes"
      db_go
    fi
    echo "Finished configuring database"
  else
    echo "Database already configured"
  fi
else
  echo "Not configuring database since it's not local"
fi

###  Done with the database stuff, move on to the next item on the list
echo "Fixing 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

if [ -f /etc/imp/pgsql_create.sql ]; then
  if [ "$dblocal" = "yes" ]; then
    if [ "$dbtype" = "PostgreSQL" ]; then
      chown postgres /etc/imp/pgsql_create.sql
    fi
  fi
fi

if [ "$restart" = "database" ] || [ "$restart" = "both" ]; then
  if [ "$dbtype" = "PostgreSQL" ]; then
    if [ -f /etc/postgresql/postmaster.init ]; then
      echo "Restarting PostgreSQL"
      /etc/init.d/postgresql restart > /dev/null 2>&1
    fi
  fi
fi

if [ "$restart" = "webserver" ] || [ "$restart" = "both" ]; then
  for server in $mywebtype; do
    if [ "$server" = "none" ]; then
      echo "Not reloading web server"
    else
      echo "Reloading $server"
      /etc/init.d/$server reload > /dev/null 2>&1       
    fi
  done
fi

echo "Done!"

#DEBHELPER#

db_stop 
exit 0
