#!/bin/bash
#
#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.
#
# authors: Amalu Obinna <amaluobinna@aol.com>
#          Brady Miller <brady@sparmy.com>
#
# date:    12/08/09
#
# Debian package pre-installation script steps:
#  1) Create log file
#  2) New install or Upgrade
#    -Install
#      a) If OpenEMR web directory already exist, then attempt an upgrade.
#      b) If MySQL is already installed:
#          -Collect the MySQL root password
#          -Ensure no openemr mysql database or mysql user
#    -Upgrade
#      a) Ensure OpenEMR web directory does exist
#      b) Collect package version and ensure appropriate upgrade
#      c) Collect webpath and sql settings from current version
#      d) Verify the mysql user/database exist
#      e) Backup the current version web folder and mysql database
#      f) Clear the cache directories in current version    
#  3) Create config file
#
#  Output log file:
#   /var/log/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

#constants and paths
LOGDIR=/var/log/openemr
LOG=$LOGDIR/install
CONFIGDIR=/etc/openemr
CONFIG=$CONFIGDIR/openemr.conf
TMPDIR=/tmp/openemr-tmp
WEB=/var/www
OPENEMR=$WEB/openemr
#hardcoded mysql user and database for install (not pertinent for upgrading)
# upgrading can use whatever is found in openemr/library/sqlconf.php
INSTALL_USER=openemr
INSTALL_DATABASE=openemr

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

#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 $1
   echo "`date`: $1" >> $LOG
   echo "EXITING.........."
   echo "`date`: EXITING.........." >> $LOG
   sleep 5
   exit 1
}

#function to check mysql for selected databases
# 1st param is password, 2nd param database, 3rd param is host (optional), 4th param is user (optional)
check_mysql () {
   if [ -n "$3" ]; then
      HOST=$3
   else
      HOST=localhost
   fi
   if [ -n "$4" ]; then
      USE=$4
   else
      USE=root
   fi
   echo `mysql -u "$USE" -h "$HOST" --password="$1" -e 'show databases' 2>/dev/null | awk '{ print $1}' | grep "^$2$"`
}

#function to collect variables from config files
# 1st param is variable name, 2nd param is filename 
collect_var () {
   echo `grep -i "^[[:space:]]*$1[[:space:]=]" $2 | cut -d \= -f 2 | cut -d \; -f 1 | sed "s/[ 	'\"]//gi"`
}

#function to hold the upgrade code (allow more flexible upgrading)
# 1st param is the previous version,
# 2nd param is whether raw or package upgrade
upgrade_function () {

      #Check for /var/www/openemr. If does not exist, then exit.
      if ! [ -d "$OPENEMR" ]; then
         unable_exit "OpenEMR is not installed ($OPENEMR), so can not upgrade the OpenEMR Package."
      fi

      #collect previous version
      OLD_VERSION=$1

      #collect type of upgrade (either package or raw)
      UPGRADETYPE=$2

      #ensure version is appropriate for upgrade (if not, then exit!!)
      #
      # ONLY acceptable for raw is 3.0.0 or 3.0.0.1 or 3.1.0 (need to convert these)
      #
      if [ "$UPGRADETYPE" == "raw" ]; then
         if [ "$OLD_VERSION" == "3.0.0" ]; then
            OLD_VERSION="3.0.1-1"
         elif [ "$OLD_VERSION" == "3.0.0.1" ]; then
            OLD_VERSION="3.0.1-1"
         elif [ "$OLD_VERSION" == "3.0.1" ]; then
            OLD_VERSION="3.0.1-1"
         elif [ "$OLD_VERSION" == "3.1.0" ]; then
            OLD_VERSION="3.1.0-1" 
         else
            unable_exit "Unable to upgrade from $OLD_VERSION version package with $UPGRADETYPE method, so can not upgrade the OpenEMR Package."
         fi
      fi
      #
      # ONLY acceptable package and converted raw is 3.0.1-1 or 3.1.0-1
      #
      if [ "$OLD_VERSION" != "3.0.1-1" ] && [ "$OLD_VERSION" != "3.1.0-1" ]; then
         unable_exit "Unable to upgrade from $OLD_VERSION version package with $UPGRADETYPE method, so can not upgrade the OpenEMR Package."
      fi

      #Collect variables
      #collect openemr mysql data

      SQLLOCATION=$(collect_var \$host $OPENEMR/library/sqlconf.php)
      SQLUSER=$(collect_var \$login $OPENEMR/library/sqlconf.php)
      SQLPASSWORD=$(collect_var \$pass $OPENEMR/library/sqlconf.php)
      SQLDATABASE=$(collect_var \$dbase $OPENEMR/library/sqlconf.php)
      SQLUTFFLAG=$(collect_var \$disable_utf8_flag $OPENEMR/library/sqlconf.php)
      #if SQLUTFFLAG variable is empty, then make it false       
      if [ "$SQLUTFFLAG" == "" ]; then
         SQLUTFFLAG="false"
      fi

      #ensure the mysql database and user exist
      if ! [ "`check_mysql "$SQLPASSWORD" "$SQLDATABASE" "$SQLLOCATION" "$SQLUSER"`" == "$SQLDATABASE" ]; then
         unable_exit "MySQL '$SQLDATABASE' database does not exist, unable to upgrade."
      fi

      #create the tmp directories
      mkdir -p $TMPDIR/openemr_web_$OLD_VERSION
      mkdir -p $TMPDIR/openemr_mysql_$OLD_VERSION

      #backup web directory to tmp
      cp -fr $OPENEMR/* $TMPDIR/openemr_web_$OLD_VERSION/

      #backup mysql database to tmp
      mysqldump -u "$SQLUSER" -h "$SQLLOCATION" --password="$SQLPASSWORD" "$SQLDATABASE" > $TMPDIR/openemr_mysql_$OLD_VERSION/openemr_mysql_$OLD_VERSION.sql

      #clear the temporary openemr cache directories (no need to keep these during upgrade)
      rm -fr $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/compiled/*
      rm -fr $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/cache/*
      rm -fr $OPENEMR/gacl/admin/templates_c/*

      output_both "Upgrading OpenEMR from $OLD_VERSION..."

      #Create the config file string, which will pass info to postinst script
      #to continue upgrade process.
      SETTING="#Optional settings\n\
#(currently empty, plan to use in subsequent versions of OpenEMR)\n\
\n\
#Installation settings\n\
# (DO NOT EDIT below!!!)\n\
process=pending\n\
plan=upgrade\n\
pass=\n\
previous_version=$OLD_VERSION\n\
sqllocation=$SQLLOCATION\n\
sqluser=$SQLUSER\n\
sqlpassword=$SQLPASSWORD\n\
sqldatabase=$SQLDATABASE\n\
sqlutfflag=$SQLUTFFLAG"
      mkdir -p $CONFIGDIR
      echo -e $SETTING > $CONFIG
}


case "$1" in
   install)

      #create the log file directory
      mkdir -p $LOGDIR

      #Check for /var/www/openemr, if exist then see if upgrade is possible
      if [ -d "$OPENEMR" ]; then

         #collect current version
         v1=$(collect_var \$v_major $OPENEMR/version.php)
         v2=$(collect_var \$v_minor $OPENEMR/version.php)
         v3=$(collect_var \$v_patch $OPENEMR/version.php)
         v4=$(collect_var \$v_tag   $OPENEMR/version.php)
         RAWVERSION="$v1.$v2.$v3$v4"

         #attempt upgrade, if user desires
         echo ""
         echo -n "Found a version of openemr at $OPENEMR. Do you want to attempt an upgrade (ensure you have a backup before proceeding)? [y/N]"
         read CONFIRM
         if ! [[ $CONFIRM =~ [Yy]  ]]; then
            unable_exit "You have chosen to not install the OpenEMR package."
         fi
         upgrade_function $RAWVERSION "raw"

         exit 0
      fi

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

      #If mysql is installed, then figure out root password      
      MYSQL=$(dpkg -l |grep mysql-server)
      if [ -n "$MYSQL" ]; then

         #set pass to blank as default
         MPASS=""

## BEGIN MYSQL ROOT PASSWORD GRAB
         if [ "`check_mysql "$MPASS" "mysql"`" != "mysql" ]; then
            #the initial mysql password didn't work, so ask for password
            COUNTDOWN=1
            while true; do
               echo ""
               echo -n "Please enter your MySQL root password:"
               read MPASS 
               echo ""   
               if [ "`check_mysql "$MPASS" "mysql"`" == "mysql" ]; then
                  #the mysql root password works, so can exit loop
                  break
               else
                  #the mysql root password did not work
                  if [ "$COUNTDOWN" -ge "5" ]; then
                     output_both "5 attempts to enter your mysql root password have failed"
                     output_both "Recommend repeating OpenEMR installation when you know your mysql root password"
                     unable_exit "Giving up on OpenEMR package installation."
                  fi
                  echo "The entered MySQL root password did not work."
                  echo "$COUNTDOWN of 5 total attempts."
                  echo "PLEASE TRY AGAIN..."
               fi
               let "COUNTDOWN += 1"
            done
         fi
## END MYSQL ROOT PASSWORD GRAB

         #successfully obtained mysql root password, so place it in the temp config file
         SETTING="$SETTING\npass=$MPASS"

         #now ensure the openemr user and database do not exist, if so then exit
         # Check for openemr database in mysql, if exist then exit
         if [ "`check_mysql "$MPASS" "$INSTALL_DATABASE"`" == "$INSTALL_DATABASE" ]; then
            unable_exit "MySQL '$INSTALL_DATABASE' database already exists"
         fi
         # Check for user openemr in mysql.user, if exist then exit
         USER=$(mysql -s -u root -h localhost --password="$MPASS" -e "SELECT User from mysql.user where User='$INSTALL_USER'")
         if [ "$USER" == "$INSTALL_USER" ]; then
            unable_exit "MySQl user '$INSTALL_USER' already exists"
         fi
      fi

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

      exit 0
   ;;

   upgrade)

      upgrade_function $2 "package"

      exit 0
   ;;

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

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