#!/bin/sh
#
#  This library is part of the FirebirdSQL 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.
#  You may obtain a copy of the Licence at
#  http://www.gnu.org/licences/lgpl.html
#  
#  As a special exception this file can also be included in modules
#  with other source code as long as that source code has been 
#  released under an Open Source Initiative certificed licence.  
#  More information about OSI certification can be found at: 
#  http://www.opensource.org 
#  
#  This module 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 Licence for more details.
#  
#  This module was created by members of the firebird development 
#  team.  All individual contributions remain the Copyright (C) of 
#  those individuals and all rights are reserved.  Contributors to 
#  this file are either listed below or can be obtained from a CVS 
#  history command.
# 
#   Created by:  Mark O'Donohue <mark.odonohue@ludwig.edu.au>
# 
#   Contributor(s):
#  
# 
#   $Id: preinstall.sh.in,v 1.4.2.2 2003/12/23 23:21:17 skywalker Exp $
# 


# The pre install routine for Firebird Classic


#------------------------------------------------------------------------
# Prompt for response, store result in Answer

Answer=""

AskQuestion() {
    Test=$1
    DefaultAns=$2
    echo -n "${1}"
    Answer="$DefaultAns"
    read Answer
}


#------------------------------------------------------------------------
#  stop super server if it is running 
#  Also will only stop firebird, since that has the init script


stopServerIfRunning() {

    checkString=`ps -efww| egrep "(fbserver|fbguard)" |grep -v grep`

    if [ ! -z "$checkString" ] 
      then
        if [ -f /etc/init.d/firebird ]
          then
            /etc/init.d/firebird stop
        elif [ -f /etc/rc.d/init.d/firebird ]
          then
            /etc/rc.d/init.d/firebird stop
        fi
    fi
}

#------------------------------------------------------------------------
#  stop server if it is running


checkIfServerRunning() {


    stopServerIfRunning

# Check is server is being actively used.

    checkString=`ps -efww| egrep "(fbserver|fbguard)" |grep -v grep`

    if [ ! -z "$checkString" ] 
      then
        echo "An instance of the Firebird Super server seems to be running." 
        echo "(the ibserver or ibguard process was detected running on your system)"
        echo "Please quit all Firebird applications and then proceed"
        exit 1 
    fi

    checkString=`ps -efww| egrep "(fb_inet_server|gds_pipe)" |grep -v grep`

    if [ ! -z "$checkString" ] 
      then
        echo "An instance of the Firebird classic server seems to be running." 
        echo "(the gds_inet_server or gds_pipe process was detected running on your system)"
        echo "Please quit all Firebird applications and then proceed." 
        exit 1 
    fi


    # The following check for running interbase or firebird 1.0 servers.

    checkString=`ps -efww| egrep "(ibserver|ibguard)" |grep -v grep`

    if [ ! -z "$checkString" ] 
      then
        echo "An instance of the Firebird/InterBase Super server seems to be running." 
        echo "(the ibserver or ibguard process was detected running on your system)"
        echo "Please quit all Firebird applications and then proceed"
        exit 1 
    fi

    checkString=`ps -efww| egrep "(gds_inet_server|gds_pipe)" |grep -v grep`

    if [ ! -z "$checkString" ] 
      then
        echo "An instance of the Firebird/InterBase classic server seems to be running." 
        echo "(the gds_inet_server or gds_pipe process was detected running on your system)"
        echo "Please quit all Firebird applications and then proceed." 
        exit 1 
    fi


#  This one is commented out, since it usually works out ok, we have
#  checked that no procesers are active, but inetd/xinetd is still listening
#  the best thing would be to turn the service off, but I don't have time
#  to do all the xinetd/inetd stuff, see the preunistall.sh script for details
#
#    checkString=`netstat -an | egrep '3050.*LISTEN'`
#
#    if [ ! -z "$checkString" ] 
#      then
#        echo "An instance of the Firebird/InterBase server seems to be running." 
#        echo "(netstat -an reports a process is already listening on port 3050)"
#        echo "Please quit all Firebird applications and then proceed." 
#        exit 1 
#    fi


# Stop lock manager if it is the only thing running.

    for i in `ps -efww | egrep "[gds|fb]_lock_mgr" | awk '{print $2}' `
     do
        kill $i
     done

}

#------------------------------------------------------------------------
# Run process and check status


runAndCheckExit() {
    Cmd=$*

#    echo $Cmd
    $Cmd

    ExitCode=$?

    if [ $ExitCode -ne 0 ]
      then
        echo "Install aborted: The command $Cmd "
        echo "                 failed with error code $ExitCode"
        exit $ExitCode
    fi
}


#------------------------------------------------------------------------
#  Display message if this is being run interactively.


displayMessage() {

    msgText=$1
    if [ ! -z "$InteractiveInstall" ]
      then
        echo $msgText
    fi
}


#------------------------------------------------------------------------
#  Archive any existing prior installed files.
#  This is mainly for the .tar.gz install since rpm packages take care of
#  thier own.



archivePriorInstallSystemFiles() {


    if [ ! -f manifest.txt ]
      then
        return
    fi

    archiveFileList=""

    for i in `cat manifest.txt`
      do
        if [ ! -d /$i ]  # Ignore directories 
          then
            if [ -e /$i ]
              then
                archiveFileList="$archiveFileList $i"          
            fi
        fi
      done



    for i in usr/sbin/rcfirebird etc/init.d/firebird etc/rc.d/init.d/firebird
      do
        DestFile=./$i
        if [ -e /$DestFile ]
          then
            archiveFileList="$archiveFileList $DestFile"
        fi
      done

    deleteFileList="$archiveFileList"


    DestFile="$FBRootDir"

    if [ ! -z "$archiveFileList" ]
      then
        cat <<EOF

** Warning **
    Some existing files have been detected from a prior install of 
    FirebirdSQL (or InterBase).  These files will be archived in
    file : ${ArchiveMainFile}
    and then deleted.

EOF

        if [ ! -z "$InteractiveInstall" ]
          then
            AskQuestion "Press return to continue or ^C to abort"
        fi

    fi


    if [ ! -z "$archiveFileList" ]
      then

        displayMessage "Archiving..."

        runAndCheckExit "tar -C / -czf $ArchiveMainFile $archiveFileList"


        displayMessage "Done."
        displayMessage "Deleting..."


        oldPWD=`pwd`
        cd /
        for i in $deleteFileList
          do
            rm -f $i
          done

        cd $oldPWD

        displayMessage "Done."

    fi
}


#------------------------------------------------------------------------
#  Check for installed RPM package

# (Pavel I've left this in since originally I could test for other packages,
# even if they did not conflict with current ones, ie we can test InterBase
# and CS/SS  The package manager does not allow it currently but I've left
# this in in case that sort of thing gets allowed again

checkForRPMInstall() {
    PackageName=$1

    rpm -q $PackageName
    STATUS=$? 
    if [ $STATUS -eq 0 ]
      then 
        echo "Previous version of $PackageName is detected on your system." 
        echo "this will conflict with the current install of Firebird"
        echo "Please unistall the previous version `rpm -q $PackageName` and then proceed." 
        exit $STATUS 
    fi 

}



#= Main Pre ================================================================

    if  [ -z "$FirebirdInstallPrefix" ]
       then
        FirebirdInstallPrefix=@prefix@
    fi
    FBRootDir=$FirebirdInstallPrefix

    FBBin=$FBRootDir/bin
    ArchiveDateTag=`date +"%Y%m%d_%H%M"`
    ArchiveMainFile="${FBRootDir}_${ArchiveDateTag}.tar.gz"



# Ok so any of the following packages are a problem
# these don't work at least in the latest rpm manager, since it 
# has the rpm database locked and it fails.

#    checkForRPMInstall InterBase
#    checkForRPMInstall FirebirdCS
#    checkForRPMInstall FirebirdSS


    checkIfServerRunning

# Failing that we archive any files we find

    archivePriorInstallSystemFiles


