#!/bin/sh
#
#  This library is part of the Firebird 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: postuninstall.sh.in,v 1.1.2.3 2004/03/29 03:50:04 skidder Exp $
# 

# The post uninstall routine for Firebird Classic.


#------------------------------------------------------------------------
# remove line from config file if it exists in it.

removeLineFromFile() {
    FileName=$1
    oldLine=$2

    if [ ! -z "$oldLine" ] 
      then
        cat $FileName | grep -v "$oldLine" > ${FileName}.tmp
        mv ${FileName}.tmp $FileName
        echo "Updated."

    fi
}


#------------------------------------------------------------------------
# remove the xinetd config file and restart the service

removeXInetDService() {

    if [ -f /etc/xinetd.d/firebird ] 
      then
        rm -f /etc/xinetd.d/firebird
    fi


    if [ -f /var/run/xinetd.pid ]
      then
        kill -USR2 `cat /var/run/xinetd.pid`
    fi
    
}


#------------------------------------------------------------------------
# remove the inetd file and restart the service.

removeInetDService() {
    # Next, lose the gds_db line from /etc/inetd.conf

    FileName=/etc/inetd.conf
    oldLine=`grep "^gds_db" $FileName`
    removeLineFromFile "$FileName" "$oldLine"

    # Get inetd to reread new inetd.conf file
    
    if [ -f /var/run/inetd.pid ]
      then
        kill -HUP `cat /var/run/inetd.pid`
    fi
}


#------------------------------------------------------------------------
#  Delete new user and group


deleteFirebirdUser() {

    userdel firebird
 #   groupdel firebird

}

#------------------------------------------------------------------------
#  removeLinksForBackCompatibility
#  Remove links for back compatibility to InterBase and Firebird1.0 
#  linked systems.

removeLinksForBackCompatibility() {

    if [ -L @libdir@/libgds.so ]
      then
        rm -f @libdir@/libgds.so
    fi
    if [ -L @libdir@/libgds.so.0 ]
      then
        rm -f @libdir@/libgds.so.0
    fi

}


#= Main PostUn ============================================================

# I don't think this is needed anymore.
#    if [ -L @libdir@/libgds.so ] 
#      then 
#        rm @libdir@/libgds.so 
#    fi 

    if [ "$1"=0 ] 
      then 

        # Lose the gds_db line from /etc/services 
        # Leave it there, it was probably there already and does no
        # harm. MOD 7-Nov-2002

        #FileName=/etc/services
        #oldLine=`grep "^gds_db" $FileName`
        #removeLineFromFile "$FileName" "$oldLine"

        removeLinksForBackCompatibility
        
        if [ -d /etc/xinetd.d ]
          then
            removeXInetDService
        elif [ -f /etc/inetd.d ]
          then
            removeInetDService
        fi

    fi
