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

# Where the config file is that stores the previous selections
CHOICES_FILE="/etc/imp/defaults.choices";
OPTIONS_FILE="/etc/imp/defaults.options";
DEFAULTS_FILE="/etc/imp/defaults.php3";
HCHOICES_FILE="/etc/horde/defaults.choices";
HCHOICES_PHP="/etc/horde/horde.choices";

# read those selections in if and only if they exist
if [ -f "$CHOICES_FILE." ]; then
   . $CHOICES_FILE
fi

if [ -f "$HCHOICES_FILE." ]; then
   . $HCHOICES_FILE
fi

if [ -f "$HCHOICES_PHP." ]; then
   . $CHOICES_PHP
fi

## Grab some extra db info from horde configs
  db_input "critical" "horde/database_type"     
  db_go
  db_get "horde/database_type"
  dbtype="$RET"
  db_input "high" "horde/database_server"
  db_go  
  db_get "horde/database_server"
  dbserver="$RET"
  db_input "high" "horde/database_name"
  db_go
  db_get "horde/database_name"
  dbname="$RET"
  db_input "high" "horde/database_user"
  db_go
  db_get "horde/database_user"
  dbuser="$RET"
  db_input "high" "horde/database_pass"
  db_go
  db_get "horde/database_pass"
  dbpass="$RET"
  db_input "high" "horde/path_to_sendmail"
  db_go
  db_get "horde/path_to_sendmail"
  path_to_sendmail="$RET"

## Ok set our temp file to use
p=`basename $0`
TMPFILE=`mktemp /tmp/$p.XXXXXX` || exit 1

### Set the shell variable so that it can be sourced in
echo "#!/bin/sh" >>$TMPFILE 2>&1;
echo "# Config file read in by IMP setup program" >>$TMPFILE
echo "# Autobuilt by debconf/hordeconf Do not edit" >>$TMPFILE
echo "#" >>$TMPFILE
echo "#  DO NOT EDIT" >>$TMPFILE
echo "#" >>$TMPFILE
echo "# configure program (defaults.php3)" >>$TMPFILE

## Ok, now we need to run through the choices ##
for i in `cat $OPTIONS_FILE | grep -v "#"`; do
  # go get the value, prompt for it if we don't have it
  db_input "high" "imp/$i"
  db_go
  # set the tmp file with this value
  db_get "imp/$i"
  echo "$i=\"$RET\";" >>$TMPFILE
done

# Now drop the newly created file into it's place
mv $TMPFILE $CHOICES_FILE > /dev/null 2>&1;
chmod 755 $CHOICES_FILE
chmod go-rwx $CHOICES_FILE

# Read in the new values
. $CHOICES_FILE

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

echo "<?php" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* true = put the compose window in the main frame; false = use a popup window for compose windows */" >>$TMPFILE
echo "\$default->minimum_popups = $minimum_popups;" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* For debugging purposes */" >>$TMPFILE
echo "\$default->error_level               = 15;" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* The longest that things like file uploads and slow functions should be" >>$TMPFILE
echo " * allowed to run. 0 means run until termination (forever if infinite loop)." >>$TMPFILE
echo " * NOTE: you can't set this to 0 if safe_mode is on. */ " >>$TMPFILE
echo "\$default->max_execution_time        = 0;" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* Imap Server Default */" >>$TMPFILE
echo "\$default->server                    = '$server'; " >>$TMPFILE
echo "\$default->from_server               = '$from_server'; " >>$TMPFILE
echo "\$default->port                      = $port; " >>$TMPFILE
echo "\$default->servtype                  = '$servtype'; " >>$TMPFILE
echo "" >>$TMPFILE
echo "/* Web server configuration */" >>$TMPFILE
echo "\$default->root_url                  = '$root_url'; " >>$TMPFILE
echo "\$default->include_dir               = '$include_dir'; " >>$TMPFILE
echo "\$default->graphics_url              = '$graphics_url'; " >>$TMPFILE
echo "" >>$TMPFILE
echo "/* Folder configuration */" >>$TMPFILE
echo "\$default->folders                   = '$folders'; " >>$TMPFILE
echo "\$default->use_imap_subscribe        = $use_imap_subscribe; " >>$TMPFILE
echo "\$default->show_dotfiles             = false; " >>$TMPFILE
echo "\$default->sent_mail                 = 'sent_mail'; " >>$TMPFILE
echo "\$default->save_sent_mail            = true; " >>$TMPFILE
echo "\$default->drafts                    = 'drafts'; " >>$TMPFILE
echo "" >>$TMPFILE
echo "/* Ldap searching */" >>$TMPFILE
echo "\$default->use_ldap_search           = $use_ldap_search;" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* user is presented with an list of available imap servers */" >>$TMPFILE
echo "\$default->use_server_list           = $use_server_list;" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* User changable items */" >>$TMPFILE
echo "\$default->user_change_server        = $user_change_server;" >>$TMPFILE
echo "\$default->user_change_port          = $user_change_port;" >>$TMPFILE
echo "\$default->user_change_servtype      = $user_change_servtype;" >>$TMPFILE
echo "\$default->user_change_folder        = $user_change_folder;" >>$TMPFILE
echo "\$default->user_change_from          = $user_change_from;" >>$TMPFILE
echo "\$default->user_change_fullname      = $user_change_fullname;" >>$TMPFILE
echo "\$default->user_use_addressbook      = $user_use_addressbook;" >>$TMPFILE
echo "\$default->user_use_folders          = $user_use_folders;" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* New mail configuration */" >>$TMPFILE
echo "/* Refresh between check for new mail */" >>$TMPFILE
echo "\$default->refresh_delay                  = 300;" >>$TMPFILE
echo "\$default->newmail_popup                  = $newmail_popup;" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* Path to sendmail */" >>$TMPFILE
echo "\$default->path_to_sendmail          = '$path_to_sendmail';" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* Ispell setup */" >>$TMPFILE
echo "\$default->path_to_ispell            = '$path_to_ispell';" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* Mswordview setup */" >>$TMPFILE
echo "\$default->path_to_mswordview        = '$path_to_mswordview';" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* path to tar so that tarballs can be listed */" >>$TMPFILE
echo "\$default->path_to_tar               = '/bin/tar';" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* Cyrus configuration block */" >>$TMPFILE
echo "\$default->personal_folders          = '$personal_folders';" >>$TMPFILE
echo "// cyrus configs may look like this:" >>$TMPFILE
echo "// $default->personal_folders       = 'INBOX.';" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* Default Language */" >>$TMPFILE
echo "\$default->language                  = '$language';" >>$TMPFILE
echo "" >>$TMPFILE
echo "\$default->append_header             = true;" >>$TMPFILE
echo "\$default->append_trailer            = true;" >>$TMPFILE
echo "" >>$TMPFILE
echo "\$default->text_parts_inline         = true;" >>$TMPFILE
echo "" >>$TMPFILE
echo "/* database config */" >>$TMPFILE
echo "\$default->use_db                    = true;" >>$TMPFILE
echo "\$default->db_user_name              = '$dbuser';" >>$TMPFILE
echo "\$default->db_password               = '$dbpass';" >>$TMPFILE
echo "\$default->db_name                   = '$dbname';" >>$TMPFILE
echo "\$default->db_server_name            = '$dbserver';" >>$TMPFILE
echo "\$default->db_pref_table             = 'imp_pref';" >>$TMPFILE
echo "\$default->db_address_table          = 'imp_addr';" >>$TMPFILE
echo "\$default->db_connect_string         = '';" >>$TMPFILE
echo "\$default->db_server_port            = '';" >>$TMPFILE
echo "\$default->db_server_options         = '';" >>$TMPFILE
echo "\$default->db_server_tty             = '';" >>$TMPFILE
echo "" >>$TMPFILE
echo "?>" >>$TMPFILE


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

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

  if (grep 'extension' /etc/php3/apache/php3.ini | grep 'imap.so'>/dev/null 2>&1); then (notsome="1"); else (echo 'extension=imap.so' >> /etc/php3/apache/php3.ini ); fi
  if (grep 'extension' /etc/php3/apache/php3.ini | grep 'ldap.so'>/dev/null 2>&1);
  then
    (notsome="1");
  else
    (echo 'extension=ldap.so' >> /etc/php3/apache/php3.ini );
  fi
  /bin/sed -e "s#@dbname@#$dbname#" /usr/share/doc/imp/examples/pgsql_create.sql > /usr/share/doc/imp/examples/pgsql_create.sql.new
  cp /usr/share/doc/imp/examples/pgsql_create.sql /usr/share/doc/imp/examples/pgsql_create.sql.bak >/dev/null 2>&1
  mv /usr/share/doc/imp/examples/pgsql_create.sql.new /usr/share/doc/imp/examples/pgsql_create.sql >/dev/null 2>&1

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

    PSQL2_QUERY="GRANT SELECT, INSERT, UPDATE ON active_sessions, auth_user,auth_user_md5, imp_pref, imp_addr TO $dbuser"
    su - postgres -c '/usr/bin/psql -q template1 < /usr/share/doc/imp/examples/pgsql_create.sql >/dev/null 2>&1'
    su - postgres -c '/usr/bin/psql -q horde -c "$PSQL2_QUERY" >/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#" /usr/share/doc/horde/examples/mysql_create.sql > /usr/share/doc/horde/examples/mysql_create.sql.new
    cp /usr/share/doc/horde/examples/mysql_create.sql /usr/share/doc/horde/examples/mysql_create.sql.bak >/dev/null 2>&1
    mv /usr/share/doc/horde/examples/mysql_create.sql.new /usr/share/doc/horde/examples/mysql_create.sql >/dev/null 2>&1

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

  db_input "high" "imp/dbconfigured"
  db_go
  db_get "imp/dbconfigured"
  if [ "$RET" = "no" ] ; then
    if [ "$dbpass" = "none" ] ; then
      /usr/bin/mysql -f < /usr/share/doc/imp/examples/mysql_create.sql >/dev/null 2>&1
    else
      /usr/bin/mysql -f --password=$dbpass < /usr/share/doc/imp/examples/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

db_input "critical" "horde/webserver_type"
db_go
db_get "horde/webserver_type"
webtype="$RET"

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

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

chown -R www-data.root /usr/share/horde >> /dev/null 2>&1
chown -R www-data.root /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

#DEBHELPER#

exit 0
