#! /bin/sh
# set -x

# "Datagard" name originally invented by Sergei Dolmatov.
# Most of the scripts is taken from Debian packages.
# Great thanks to Artur Pietruk <arturp@plukwa.net>
# for tips and hints for sed.

# Datagard - Midgard database installation program
# Copyright (C) 2003 Piotr Pokora, The Midgard Project

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


. /usr/share/midgard/datagard/datagard.conf

# Set default umask
umask 0177

# Backwards compatible tasks
#
# Move the log file to the new location if the new one does not exist
if [ ! -f $MGD_VAR_PATH/log/midgard/datagard.log ] && [ -f $MGD_VAR_PATH/log/datagard.log ]; then
  if [ ! -d $MGD_VAR_PATH/log/midgard ]; then
    mkdir -p $MGD_VAR_PATH/log/midgard
    chmod u=+x $MGD_VAR_PATH
    chmod u=+x $MGD_VAR_PATH/log
    chmod u=+rwx $MGD_VAR_PATH/log/midgard
  fi
  mv $MGD_VAR_PATH/log/datagard.log $MGD_VAR_PATH/log/midgard/
  # And convert the pkgs install info
  if [ ! -d $MGD_VAR_PATH/lib/midgard/datagard ]; then
    mkdir -p $MGD_VAR_PATH/lib/midgard/datagard
    chmod u=+x $MGD_VAR_PATH
    chmod u=+x $MGD_VAR_PATH/lib
    chmod u=+x $MGD_VAR_PATH/lib/midgard
    chmod u=+rwx $MGD_VAR_PATH/lib/midgard/datagard
  fi
  cp -p $MGD_VAR_PATH/log/midgard/datagard.log $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs
  cat $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs | while read LINE; do
    HOST=`echo $LINE | cut -f2 -d","`
    DB=`echo $LINE | cut -f3 -d","`
    FILE=`echo $LINE | cut -f4 -d","`
    LATEST_LINE=`grep ",$HOST,$DB,$FILE," $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs | tail -1`
    grep -v ",$HOST,$DB,$FILE," $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs > /tmp/datagard-installed-pkgs
    echo $LATEST_LINE >> /tmp/datagard-installed-pkgs
    rm -f $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs
    mv /tmp/datagard-installed-pkgs $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs
  done
fi
# Remove possible empty lines from the installed-pkgs file
if [ -f $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs ]; then
  grep -q -v . $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs
  if [ $? -eq 0 ]; then
    grep . $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs > /tmp/datagard-installed-pkgs
    rm -f $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs
    mv /tmp/datagard-installed-pkgs $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs
  fi
fi

 # Enable download?
 DOWNLOAD_ENABLED=1
 if [ "$1" = "download" ] || [ "$2" = "download" ]; then
   DOWNLOAD_ENABLED=0
 fi

 # Enable package selection?
 SELECT_ENABLED=1
 if [ "$1" = "select" ] || [ "$2" = "select" ]; then
   SELECT_ENABLED=0
 fi

 # pear install/update (this MUST function without asking anything)
 # This must be after other switches to have them available in mgd_pear
 # Also, this must be before any logic in this file (there MUST NOT be any output before this)
 if [ "$1" = "pear" ]; then
   mgd_pear $2
   exit $?
 fi

 # Auto import (this MUST function without asking anything)
 # This must be after other switches to have them available in mgd_import
 # Also, this must be before any logic in this file (there MUST NOT be any output before this)
 if [ "$1" = "import" ]; then
   mgd_import $2
   exit $?
 fi

 MYSQL_DB_HOST_ASKED=""

 mgd_config_get_db_login
 mgd_config_db_action
 mgd_get_config_data MGD_DB_ACTION
 MGD_DB_ACTION="$RET"
 
 case $MGD_DB_ACTION in
	( "Install" )
        if [ $DOWNLOAD_ENABLED -eq 0 ]; then
          mgd_config_select_download_app
        fi
        if [ ! -n "$MYSQL_DB_HOST_ASKED" ]; then
          mgd_data_config_db_host
          # Update the mysql command variables
          mgd_config_get_db_login
        fi
	mgd_config_db_setup true
	;;
	( "Update" )
        if [ $DOWNLOAD_ENABLED -eq 0 ] && [ $SELECT_ENABLED -eq 0 ]; then
          mgd_config_select_download_app
        fi
        if [ ! -n "$MYSQL_DB_HOST_ASKED" ]; then
          mgd_data_config_db_host
          # Update the mysql command variables
          mgd_config_get_db_login
        fi
        mgd_config_db_update
        mgd_config_db_user
        mgd_config_sg_login
	;;
        ( "Download" )
        mgd_config_select_download_app
        ;;
	( "Import" )
        if [ $DOWNLOAD_ENABLED -eq 0 ]; then
          mgd_config_select_download_app
          mgd_download_app
          mgd_install_midcom
        fi
        if [ ! -n "$MYSQL_DB_HOST_ASKED" ]; then
          mgd_data_config_db_host
          # Update the mysql command variables
          mgd_config_get_db_login
        fi
        mgd_config_db_select
	mgd_config_select_app import
        mgd_get_config_data MGD_APPS
        MGD_APPS=$RET
        if [ -n "$MGD_APPS" ]; then
	  mgd_config_db_user
          mgd_config_sg_login
        fi
	;;
	( "None" )
  if [ $USE_DEBIAN -eq 1 ];then
    db_stop
  else
    exit 0
  fi       
  ;;
 esac	

## We do now our work according to answers
############################################

 case $MGD_DB_ACTION in
	( "Install" )
        mgd_get_config_data MGD_DB_HOST
        MGD_DB_HOST="$RET"
        mgd_get_config_data MGD_DB_NAME
        MGD_DB_NAME="$RET"
        # Remove install entries from the pkgs file as they are no longer true
        # This should happen only if user has removed db and is now reinstalling
        if [ -f $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs ]; then
          grep -v "$MGD_DB_HOST,$MGD_DB_NAME," $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs > /tmp/datagard-installed-pkgs
          rm -f $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs
          mv /tmp/datagard-installed-pkgs $MGD_VAR_PATH/lib/midgard/datagard/installed-pkgs
        fi

        if [ $DOWNLOAD_ENABLED -eq 0 ]; then
          mgd_download_app
        fi
	mgd_create_db
	mgd_create_repconf install
	mgd_update_db
        mgd_install_midcom
        if [ $SELECT_ENABLED -eq 0 ]; then
          mgd_config_select_app install
        else
          mgd_config_apps
        fi
	mgd_import_app
	;;
	( "Update" )
        if [ $DOWNLOAD_ENABLED -eq 0 ] && [ $SELECT_ENABLED -eq 0 ]; then
          mgd_download_app
        fi
	mgd_create_repconf
	mgd_update_db
        mgd_install_midcom
        if [ $SELECT_ENABLED -eq 0 ]; then
          mgd_config_select_app update
        else
          # There is now easy way to find out what midcom-template is installed
          # This part should force midcom-template. 
          mgd_is_installed "midcom-template"
          if [ ! -n "$RET" ]; then
            mgd_set_config_data MGD_APPS "midcom-template"
          else
            mgd_set_config_data MGD_APPS ""
          fi
        fi
        mgd_import_app
        mgd_apache_update_vhost
	;;
        ( "Download" )
        mgd_download_app
        if [ $? -eq 0 ]; then
          mgd_install_midcom
          mgd_yesno "Import downloaded xml db packages?"
          if [ "$RET" -eq 0 ]; then
            # "Import" select part
            if [ ! -n "$MYSQL_DB_HOST_ASKED" ]; then
              mgd_data_config_db_host
              # Update the mysql command variables
              mgd_config_get_db_login
            fi
            mgd_config_db_select
            if [ $SELECT_ENABLED -eq 0 ]; then
              mgd_config_select_app download
            else
              mgd_config_apps
            fi
            mgd_get_config_data MGD_APPS
            MGD_APPS=$RET
            if [ -n "$MGD_APPS" ]; then
              mgd_config_db_user
              mgd_config_sg_login
              # "Import" run part
              mgd_create_repconf
              # Call update just in case
              mgd_update_db
              mgd_import_app
              mgd_apache_update_vhost
            fi
          fi
        fi
        ;;
	( "Import" )
        if [ -n "$MGD_APPS" ]; then
	  mgd_create_repconf
          # Call update just in case
          mgd_update_db
	  mgd_import_app
          mgd_apache_update_vhost
        fi
	;;
	( "None" )
	exit 0
	;;
 esac


###################################################
# Virtual Host setup.
###################################################

mgd_get_config_data MGD_DB_ACTION
MGD_DB_ACTION="$RET"

 case $MGD_DB_ACTION in
 	( "Update")
	mgd_apache_update_host_conf
	;;
 	( "Vhost")
	mgd_config_vhost_name
	mgd_apache_register_module 
	mgd_apache_create_vhost
    mgd_msg "Your httpd server should be stopped and started again"
    ;;
	( "Install" )
	mgd_config_create_vhost
	mgd_apache_register_module
  mgd_apache_create_vhost
  mgd_apache_update_vhost 
  mgd_info "
  
  
  Congratulations! 
  
  
  Midgard Framework and CMS have now been installed successfully.

  If you installed the \"midgard-welcome\" package,
  point your favorite browser to the Midgard welcome page to get started: 
  http://$MGD_VHOST_NAME:$MGD_VHOST_PORT/midgard

  There are two additional admin interfaces available (if you installed them):
  
  User friendly Aegir
  http://$MGD_VHOST_NAME:$MGD_VHOST_PORT/aegir
   
  and (for advanced users and administrators)
  http://$MGD_VHOST_NAME:$MGD_VHOST_PORT/spider-admin
	
  Default username and password is admin/password
  
  (Your httpd server should be stopped and started again)"
  
  ;;
	( "None" )
	exit 0
	;;
 esac	

echo 
echo
echo "Done (exit status ok)."
echo

exit 0
