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

# Translation allows us to use this value on the command line
case $dbpass in
  none)
    mydbpass=
    ;;
  *)
    mydbpass=$dbpass
    ;;
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"
    ;;
esac

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

db_get "horde/debconf"
if [ "$RET" = "yes" ]; then
  echo "Getting data from debconf"
  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/custom_from_hook"
  custom_from_hook="$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
  # Read in template
  . /etc/imp/defaults.php3.in
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
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->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

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

if [ "$dblocal" = "yes" ]; then
  if [ "$dbtype" = "PostgreSQL" ]; then
    echo "fixing perms on db file"
    chown postgres /etc/imp/pgsql_create.sql
  fi
fi


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

if [ "$hrecheck" = "true" ]; then
  echo "Verifying/Fixing other config files"
  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

  # 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

if [ "$dblocal" = "yes" ]; then
  db_get "imp/dbconfigured"
  if [ "$RET" = "no" ] ; then
    echo "Configuring db"
    if [ "$dbtype" = "PostgreSQL" ]; then
      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
    fi

    if [ "$dbtype" = "MySql" ]; 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
      if [ "$restart" = "database" ] || [ "$restart" = "both" ]; then
        echo "Restarting MySQL"
        /etc/init.d/mysql restart >/dev/null 2>&1
      fi
    fi
    db_set "imp/dbconfigured" "yes"
    db_go
  fi
fi

# Now rebuild the IMP libraries
echo "Building IMP Libraries witha  --database=${mydbtype} flag"
cd /usr/share/horde/imp/lib/src && ./build.pl --database=${mydbtype} >/dev/null 2>&1

###  Done with the database stuff, move on to the next item on the list
echo "Fixing 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
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
    echo "Restarting PostgreSQL"
    /etc/init.d/postgresql restart > /dev/null 2>&1
  fi
fi

if [ "$restart" = "webserver" ] || [ "$restart" = "both" ]; then
  for server in $mywebtype; do
    echo "Restarting $server"
    /etc/init.d/$server restart > /dev/null 2>&1       
  done
fi



#DEBHELPER#

db_stop 
exit 0
