#!/bin/bash -e
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
# Copyright 2011
# authors: Brady Miller <brady.g.miller@gmail.com>
#          Amalu Obinna <amaluobinna@aol.com>
#
# Debian package pre-installation script steps:
#  1) Create log dir/file
#  2) Ensure git-OpenEMR web directory was not previously installed.
#  3) Create config dir/file
#
# Upgrading is not an option here, since this is for testing/evaluation
# of the most current unstable development version of OpenEMR.
#
#  Output log file:
#   /var/log/git-openemr/install
#
#
# summary of how this script can be called:
#        * <new-preinst> `install'
#        * <new-preinst> `install' <old-version>
#        * <new-preinst> `upgrade' <old-version>
#        * <old-preinst> `abort-upgrade' <new-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

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

#constants and paths
LOGDIR=/var/log/git-openemr
LOG=$LOGDIR/install
CONFIGDIR=/etc/git-openemr
CONFIG=$CONFIGDIR/git-openemr.conf
WEB=/var/www
OPENEMR=$WEB/git-openemr
SITEDIR=$OPENEMR/sites/default
INSTALL_USER=gitopenemr
INSTALL_DATABASE=gitopenemr

#Standardized echo function to send to only log file
#  requires one parameter (string)
log_only () {
   echo "`date`: $1" >> $LOG
}

#Standardized exit function to be used when unable to install the openemr package
#  requires one parameter (string with reason for exiting)
unable_exit () {
   echo "`date`: $1" >> $LOG
   echo "`date`: EXITING.........." >> $LOG
   exit 1
}

#function to prompt for input
# 1st param is name, 2nd param is priority, 3rd param is where result gets sent back in
# return the input
prompt_input () {
   db_set "$1" ""
   db_fset "$1" seen false
   db_input "$2" "$1" || true
   db_go || true
   db_get "$1"
   local input_value="$RET"
   db_set "$1" ""
   db_fset "$1" seen false
   local __result=$3
   eval $__result="'$input_value'"
}

case "$1" in
   install)

      #create the log file directory
      mkdir -p $LOGDIR

      #Check for /var/www/git-openemr, if exist then exit
      if [ -d "$OPENEMR" ]; then
         prompt_input git-openemr/www_path_already_exists critical ret_result
         unable_exit "OpenEMR is already installed ($OPENEMR), so can not install the OpenEMR Package."
      fi

      #Begin the config file string
      # this is so the postinst script can follow installation
      #  variables:
      #   process states whether installation is pending or complete
      #   plan states install
      #   pass temporarily holds the mysql root password
      SETTING="#Optional settings\n\
#(currently empty, plan to use in subsequent versions if needed)\n\
\n\
#Installation settings\n\
# (DO NOT EDIT below!!!)\n\
process=pending\n\
plan=install"

      #ready to install package, so create the config directory and files
      mkdir -p $CONFIGDIR
      echo -e $SETTING > $CONFIG

      exit 0
   ;;

   upgrade)

      #upgrade is not available with the git-openemr package
      echo ""
      log_only "Upgrading is not available with the git-openemr package"
      log_only "This package is for testing most recent development (unstable) OpenEMR version"
      prompt_input git-openemr/upgrade_not_supported critical ret_result
      unable_exit "To upgrade to most recent development version, recommend removing/reinstalling package"
   ;;

   abort-upgrade)
      echo "preinst asked to do abort-upgrade"
      exit 0
   ;;

   *)
      echo "preinst called with unknown argument \`$1'" >&2
      exit 1
   ;;
esac
