#!/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
#
# All questions should have been asked via debconf 
# now we just go get those, build a temp file which we read in and
# then build our config files and set everthing up

# Source debconf library
. /usr/share/debconf/confmodule
db_version 2.0

# Read in template
. /etc/horde/horde.php3.in

db_get "horde/web_user"
webuser="$RET"
db_get "horde/web_group"
webgroup="$RET"
db_get "horde/phpversion"
phpver="$RET"
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/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/dbconfigured"
dbconfigured="$RET"
db_get "horde/dblocal"
dblocal="$RET"
db_get "horde/horderecheck"
hrecheck="$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

for server in $mywebtype; do   
 if [ -f /etc/$server/httpd.conf ]; then
   echo "Web installation seems ok..."
 else 
   mywebtype="none"
 fi
done

# php extensions to verify
extensions="$mydbtype"

/bin/sed -e "s#@dbname@#$dbname#; s#@dbserver@#$dbserver#; s#@dbuser@#$dbuser#; s#@dbpass@#$dbpass#" /etc/horde/horde_phplib.inc.in > /etc/horde/horde_phplib.inc

/bin/sed -e "s#@dbname@#$dbname#; s#@dbserver@#$dbserver#; s#@dbuser@#$dbuser#; s#@dbpass@#$dbpass#" /usr/share/doc/horde/examples/${mydbtype}_create.sql.in > /etc/horde/${mydbtype}_create.sql

/bin/sed -e "s#@dbtype@#${mydbtype}#" /etc/horde/prepend.php3.in > /etc/horde/prepend.php3

db_input "critical" "horde/horderecheck" || true
db_go


# If we're asked to check others' files
if [ "$hrecheck" = "yes" ]; then
  echo "Checking non-HORDE config files"
if [ "$dblocal" = "yes" ]; then
  if [ "$dbtype" = "PostgreSQL" ]; then
    chown postgres /etc/horde/pgsql_create.sql
    if [ -f /etc/postgresql/postmaster.init ]; then
      /bin/sed -e 's#\# PGALLOWTCPIP=no#PGALLOWTCPIP=yes#; s#\# PGALLOWTCPIP=yes#PGALLOWTCPIP=yes#' /etc/postgresql/postmaster.init >/etc/postgresql/postmaster.init.tmp
      cp /etc/postgresql/postmaster.init /etc/postgresql/postmaster.init.horde.bak > /dev/null 2>&1
      if grep 'PGALLOWTCPIP=yes' /etc/postgresql/postmaster.init.tmp >/dev/null 2>&1; then
        mv /etc/postgresql/postmaster.init.tmp /etc/postgresql/postmaster.init >/dev/null 2>&1;
      fi
      # FIXME: You shouldn't go around doing people's databases like this
      if [ "$restart" = "database" ] || [ "$restart" = "both" ]; then
        /etc/init.d/postgresql restart >/dev/null 2>&1
        sleep 2
      fi
    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 "...sleeping for 5 seconds..."
      echo " "
      sleep 5
    fi
  fi
fi

if [ "$phpver" = "php3" ]; then
  # Check for php3 files
  if [ -f /etc/php3/apache/php3.ini ]; then
    # Make sure all the extensions are being loaded
    for ext in "$extensions"; do
      echo "checking for $ext php3 extension configuration"
      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
  # Check for php4 files
  if [ -f /etc/php4/apache/php.ini ]; then
    # Make sure all the extensions are being loaded
    for ext in "$extensions"; do
      echo "checking for $ext php4 extension configuration"
      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

  # Make sure all our web servers are appropriately configured.
  if [ "$mywebtype" = "none" ]; then
    echo "Not configuring web server"
  else 
    for server in $mywebtype; do
      if [ "$phpver" = "php3" ]; then
      /bin/sed -e 's#\# LoadModule php3_module#LoadModule php3_module#' /etc/$server/httpd.conf > /etc/$server/httpd.conf.tmp
      fi
      if [ "$phpver" = "php4" ]; then
        /bin/sed -e 's#\# LoadModule php4_module#LoadModule php4_module#' /etc/$server/httpd.conf > /etc/$server/httpd.conf.tmp
      fi
      cp /etc/$server/httpd.conf /etc/$server/httpd.conf.horde.back >/dev/null 2>&1
      if [ "$phpver" = "php3" ]; then
        if grep 'php3_module' /etc/$server/httpd.conf.tmp >/dev/null 2>&1; then
          mv /etc/$server/httpd.conf.tmp /etc/$server/httpd.conf
        fi
      fi
      if [ "$phpver" = "php4" ]; then 
        if grep 'php4_module' /etc/$server/httpd.conf.tmp >/dev/null 2>&1; then
          mv /etc/$server/httpd.conf.tmp /etc/$server/httpd.conf
        fi
      fi

      if grep 'Added for HORDE' /etc/$server/access.conf >/dev/null 2>&1; then
        echo "HORDE settings in /etc/$server/access.conf found!"
      cat /etc/$server/access.conf | grep -v "auto_prepend_file /etc/horde/prepend.php3" | grep -v "include_path /etc/horde" >/etc/$server/access.conf.new
      if grep 'Added for HORDE' /etc/$server/access.conf >/dev/null 2>&1; then
        mv /etc/$server/access.conf.new /etc/$server/access.conf
      fi

        if [ "$phpver" = "php3" ]; then
          if grep 'php3_include_path /etc/horde;/usr/lib/phplib' /etc/$server/access.conf >/dev/null 2>&1; then
            /bin/sed -e "s#/etc/horde;/usr/lib/phplib#/etc/horde#" /etc/$server/access.conf >/etc/$server/access.conf.temp
            if grep 'php3_include_path /etc/horde' /etc/$server/access.conf.temp >/dev/null 2>&1; then 
              mv /etc/$server/access.conf.temp /etc/$server/access.conf >/dev/null 2>&1; 
            fi
          fi
        fi
        if [ "$phpver" = "php4" ]; then
          if grep 'php_value include_path /etc/horde;/usr/lib/phplib' /etc/$server/access.conf >/dev/null 2>&1; then
            /bin/sed -e "s#/etc/horde;/usr/lib/phplib#/etc/horde#" /etc/$server/access.conf >/etc/$server/access.conf.temp
            if grep 'php_value include_path /etc/horde' /etc/$server/access.conf.temp >/dev/null 2>&1; then
              mv /etc/$server/access.conf.temp /etc/$server/access.conf >/dev/null 2>&1; 
            fi
          fi
        fi
      else
        if [ "$phpver" = "php3" ]; then
          echo "HORDE settings in /etc/$server/access.conf added"
          cat /usr/share/doc/horde/examples/apache_access.conf.in >>/etc/$server/access.conf
        fi
        if [ "$phpver" = "php4" ]; then
          echo "HORDE settings in /etc/$server/access.conf added"
          cat /usr/share/doc/horde/examples/apache_access.conf.in >>/etc/$server/access.conf
        fi
      fi
      if grep 'Added for HORDE' /etc/$server/srm.conf >/dev/null 2>&1; then
        echo "HORDE settings in /etc/$server/srm.conf found!"
      else
        echo "HORDE settings in /etc/$server/srm.conf added!"
        cat /usr/share/doc/horde/examples/apache_srm.conf.in >>/etc/$server/srm.conf
      fi
      if grep 'index.php3' /etc/$server/srm.conf >/dev/null 2>&1; then
        echo "php settings in /etc/$server/srm.conf found!"
        /bin/sed -e "s#index.html index.php3#index.html index.php index.php3#" /etc/$server/srm.conf > /etc/$server/srm.conf.new
        if grep 'index.php' /etc/$server/srm.conf.new > /dev/null 2>&1; then
          mv /etc/$server/srm.conf.new /etc/$server/srm.conf
        fi
      else 
        echo "php settings in /etc/$server/srm.conf added!"
        /bin/sed -e "s#index.html#index.html index.php index.php3#" /etc/$server/srm.conf > /etc/$server/srm.conf.new
        if grep 'index.php' /etc/$server/srm.conf.new > /dev/null 2>&1; then
          mv /etc/$server/srm.conf.new /etc/$server/srm.conf
        fi
      fi
    done
  fi
  echo "Finished checking non-HORDE config files"
else
  echo "Not checking non-HORDE config files.  I hope everything is"
  echo "configured properly..."
fi


if [ "$dblocal" = "yes" ]; then
if [ "$dbconfigured" = "no" ] ; then 
  case $dbtype in
    PostgreSQL)
    if [ -f /etc/postgresql/postmaster.init ]; then
      echo "Trying to configure your PostgreSQL database"
      # FIXME: These statements aren't very robust, they should take
      # into account the possibility of the database being on another
      # host, etc.
      if [ "$restart" = "database" ] || [ "$restart" = "both" ]; then
        su postgres -c "/usr/share/doc/horde/examples/pgsql_cuser.sh $dbuser $dbpass >/dev/null 2>&1" 
        su postgres -c "/usr/bin/psql -q -f /etc/horde/pgsql_create.sql template1 >/dev/null 2>&1" 
        db_set "horde/dbconfigured" "yes"
      else
        echo "Can't setup database because we cannot verify that postgresql"
        echo "was restarted and configured properly"
      fi
    if [ "$restart" = "database" ] || [ "$restart" = "both" ]; then
      /etc/init.d/postgresql restart >/dev/null 2>&1
      sleep 2
    fi
    fi
    ;;

    MySql)
    if [ -f /usr/bin/mysql ]; then
      echo "Trying to configure your MySQL database"
      if [ "$mysqlpass" = "none" ] ; then
        /usr/bin/mysql -f < /etc/horde/mysql_create.sql > /dev/null 2>&1
        /usr/bin/mysql -f -e "flush privileges" > /dev/null 2>&1
      else
        /usr/bin/mysql -f --password=$mysqlpass < /etc/horde/mysql_create.sql > /dev/null 2>&1
        /usr/bin/mysql -f --password=$mysqlpass -e "flush privileges" > /dev/null 2>&1         
      fi
      db_set "horde/dbconfigured" "yes"
    else 
      echo "Not configuring MySQL database because we cannot locate"
      echo "the mysql executable"
    fi
    ;;
  esac
else
  echo "Not configuring database at your request..."
fi
else
  echo "Not configuring database since it's not local..."
fi


# Now let's create the horde.php3 file needed by HORDE
cat > /etc/horde/horde.php3 <<EOF
<?php

/*            DO NOT EDIT           */
/*   AUTOMATICALLY GENERATED FILE   */
/* Edit /etc/horde/horde.php3.in and then */
/* use update-horde to generate this file */

\$default->horde_root_url	= '$horde_root_url';
\$default->horde_graphics_url	= '$horde_graphics_url';
\$default->faq_url		= '$faq_url';
\$default->horde_include_dir	= '$horde_include_dir';

/* Path to sendmail */
\$default->path_to_sendmail	= '$path_to_sendmail';

/* Problem Reporting */
\$default->problem_email	= '$problem_email';
\$default->problem_reporting	= $problem_reporting;

/* Account Signup */
\$default->signup_email		= '$signup_email';
\$default->signup		= $signup;

?>
EOF

# Make sure the perms are right
echo "Fixing file permissions"
chown -R root.root /usr/share/horde >> /dev/null 2>&1
chmod -R 555 /usr/share/horde/lib/*.lib >> /dev/null 2>&1
chmod 000 /usr/share/horde/setup.php3 >> /dev/null 2>&1
chown -R root.$webgroup /etc/horde >> /dev/null 2>&1
chmod 640 `find /etc/horde/ -type f -print ` >/dev/null 2>&1

if [ -f /etc/horde/pgsql_create.sql ]; then
  if [ "$dblocal" = "yes" ]; then
    if [ "$dbtype" = "PostgreSQL" ]; then
      chown postgres /etc/horde/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
    echo "Restarting $mywebtype"
    /etc/init.d/$server restart > /dev/null 2>&1
  done
fi

db_stop

echo "All done"
#DEBHELPER#

exit 0

