#!/bin/sh
#
# $Id: install-script,v 1.1.2.7 2003/12/29 02:12:45 tmcsys Exp $
#
# Script args:
# $0: full path to script
# $1: full path to top level package dir, no trailing slash
# $2: full path to installed location
# $3:  ????? Path to install Volume????
# $4:  ?????

FB_FW="$2/Firebird.framework"
FB_RES="$FB_FW/Versions/A/Resources"
INST_RES="$FB_RES/installer_files"

# Install and start sysv semaphores
if test `uname -r | sed -e 's/\.[0-9]*//g'` -lt 6; then
	if [ -e "SysV Semaphores" ]; then
		mkdir -p /Library/StartupItems
		if [ ! -e "/Library/StartupItems/SysV Semaphores" ]; then
			cp -r "$INST_RES/SysV Semaphores" /Library/StartupItems
			"/Library/StartupItems/SysV Semaphores/SysV Semaphores" start
		fi
	fi
fi


# Setup our services entry, our user account, and our group
#
#
# Start with the service entry
if niutil -list . /services/gds_db 2&>1 /dev/null ; then
	echo -n
else
	niutil -create . /services/gds_db
	niutil -createprop . /services/gds_db port 3050
	niutil -createprop . /services/gds_db protocol tcp tcp
fi

# Now for the group.  If the firebird group already exists, remember the
#  id if we need it to create the firebird user
if niutil -list . /groups/firebird 2&>1 /dev/null; then
	NEW_GID=`niutil -readprop . /groups/firebird gid`
else
	NEW_GID=`nigrep gid . /groups | grep ":  gid " | awk '{ print $4 + 1 } ' | sort -n | tail -1`
	niutil -create . /groups/firebird
	niutil -createprop . /groups/firebird passwd "*"
	niutil -createprop . /groups/firebird gid $NEW_GID
	niutil -createprop . /groups/firebird users firebird
fi

# Now create the firebird user
if niutil -list . /users/firebird 2&>1 /dev/null; then
	echo -n
else
	NEW_UID=`nigrep uid . /users | grep ":  uid " | awk '{ print $4 + 1 } ' | sort -n | tail -1`
	niutil -create . /users/firebird
	niutil -createprop . /users/firebird uid $NEW_UID
	niutil -createprop . /users/firebird home /Library/Frameworks/Firebird.framework
	niutil -createprop . /users/firebird passwd "*"
	niutil -createprop . /users/firebird expire 0
	niutil -createprop . /users/firebird shell /bin/tcsh
	niutil -createprop . /users/firebird change 0
	niutil -createprop . /users/firebird gid $NEW_GID
	niutil -createprop . /users/firebird realname "Firebird SQL Database"
fi

# set the permissions correctly
if [ -f "$FB_FW/Versions/A/Resources/.SuperServer" ]; then
	echo -n
else
	$FB_FW/Resources/bin/fbmgr.bin -shut -user sysdba -password masterkey
	rm -rf /System/Library/StartupItems/Firebird
fi

# Shutdown any existing servers
if [ -x /sbin/service ]; then
	service firebird stop
fi

# Install the framework
# mkdir -p /Library/Frameworks
# cp -r Firebird.framework $FB_FW
#ln -s ../../bin $FB_FW/Versions/A/Resources/English.lproj/var/bin; \
#ln -s Versions/Current/Headers $FB_FW/Headers
#ln -s Versions/Current/Resources $FB_FW/Resources
#ln -s Versions/Current/Firebird $FB_FW/Firebird
#ln -s A $FB_FW/Versions/Current
chown -R firebird "$FB_FW"
chgrp -R firebird "$FB_FW"

# set the permissions correctly
if [ -f "$FB_FW/Versions/A/Resources/.SuperServer" ]; then
    chmod 777 "$FB_FW/Resources/English.lproj/var"
    chmod 777 "$FB_FW/Resources/English.lproj/var/security.fdb"
    chmod 777 "$FB_FW/Resources/English.lproj/var/help/help.fdb"
else
    chmod 777 "$FB_FW/Resources/English.lproj/var"
    chmod 777 "$FB_FW/Resources/English.lproj/var/security.fdb"
    chmod 777 "$FB_FW/Resources/English.lproj/var/help/help.fdb"

#	 chmod a+s "$FB_FW/Resources/bin/gds_drop"
    chmod a+s "$FB_FW/Resources/bin/fb_lock_mgr"
    chmod a+s "$FB_FW/Resources/bin/fb_inet_server"

#	 chown root "$FB_FW/Resources/bin/gds_drop"
    chown root "$FB_FW/Resources/bin/fb_lock_mgr"
    chown root "$FB_FW/Resources/bin/fb_inet_server"
fi

# Install the startup item
if [ -f "$FB_FW/Versions/A/Resources/.SuperServer" ]; then
	if [ -e "$INST_RES/Firebird.startup" ]; then
#		mkdir -p /Library/StartupItems
		rm -rf /System/Library/StartupItems/Firebird
		cp -r "$INST_RES/Firebird.startup" /System/Library/StartupItems/Firebird
	fi
else
	rm -rf /System/Library/StartupItems/Firebird
fi


# Install FBConsole
#if [ -e FBConsole.app ]; then
#	mkdir -p /Applications
#	cp -r FBConsole.app /Applications
#fi

#
# Add gds_db to the services file, if available
#
HUPNEEDED='n'	# Relevant to Classic only
if [ -f /etc/services ]; then
    grep -s gds_db /etc/services  > /dev/null 2>&1
    if test $? != 0 ; then
		HUPNEEDED='y'
    	cat /etc/services > /etc/.interbase.temp.install.services
    	echo "gds_db      3050/tcp                 # Firebird Database Protocol" >> /etc/.interbase.temp.install.services
		cp /etc/services /etc/services.fbsave
		mv /etc/.interbase.temp.install.services /etc/services
    fi
fi

if [ -f /etc/inetd.conf ]; then
	cp /etc/inetd.conf /etc/inetd.conf.fbsave
fi

# Remove all traces of CS first
if [ -f /etc/inetd.conf ]; then
	grep -s gds_db /etc/inetd.conf  > /dev/null 2>&1
	if test $? != 0 ; then
		cat /etc/inetd.conf | grep -v gds_db > /etc/.interbase.temp.install.inetd.conf
		mv /etc/.interbase.temp.install.inetd.conf /etc/inetd.conf
		HUPNEEDED='y'
	fi
fi

if [ -f /etc/xinetd.d/firebird ]; then
	rm /etc/xinetd.d/firebird
	HUPNEEDED='y'
fi

# Install ourselves in the correct place
if [ -f "$FB_FW/Versions/A/Resources/.SuperServer" ]; then
	cp -r $1/Contents/Resources/StartupItem /System/Library/StartupItems/Firebird
else
	if [ -f /var/run/xinetd.pid -a -d /etc/xinetd.d ]; then
		cat > /etc/xinetd.d/firebird <<EOF
service gds_db
{
        disable			= no
        socket_type     = stream
        wait            = no
        user            = firebird
EOF
		echo "server          = $FB_RES/bin/fb_inet_server" >> /etc/xinetd.d/firebird
		cat >> /etc/xinetd.d/firebird << EOF
        groups          = yes
}
EOF
	    HUPNEEDED='y'
	else
		if [ -f /etc/inetd.conf ]; then
            echo "gds_db stream  tcp     nowait  root $FB_FW/Resources/bin/fb_inet_server fb_inet_server" > /etc/.fb.inetd.conf.entry
	    	cat /etc/inetd.conf /etc/.fb.inetd.conf.entry > /etc/.interbase.temp.install.inetd.conf
	    	mv /etc/.interbase.temp.install.inetd.conf /etc/inetd.conf
            rm -f /etc/.fb.inetd.conf.entry
	    	HUPNEEDED='y'
		fi
	fi
fi


# Make sure the hosts.equiv files are set correctly
cp /etc/hosts.equiv /etc/hosts.equiv.fbsave
if grep -x "localhost" /etc/hosts.equiv > /dev/null; then                
	echo -n
else     
    echo "localhost" >> /etc/hosts.equiv                
fi    
if grep -x "localhost.localdomain" /etc/hosts.equiv > /dev/null; then    
	echo -n
else     
    echo "localhost.localdomain" >> /etc/hosts.equiv    
fi    


# Install the saved security database, if any
if test -f /tmp/fb-security-database-update.fdb; then
	mv -f /tmp/fb-security-database-update.fdb "$FB_RES/English.lproj/var/security.fdb"
	rm -f /tmp/fb-security-database-update.fdb
fi


# Tell inetd/xinetd to reload their configuration files.
if [ "$HUPNEEDED" = 'y' ]
then
    #INETPID=`ps -axe | grep inetd | awk '{print $1}'`
	if [ -f /var/run/inetd.pid ]
	then
    	kill -HUP `cat /var/run/inetd.pid`
	fi
	if [ -f /var/run/xinetd.pid ]
	then
    	kill -HUP `cat /var/run/xinetd.pid`
	fi
fi


###
# And last but not least, start the server
if [ -f "$FB_FW/Versions/A/Resources/.SuperServer" ]; then
	#echo Starting Firebird Database Server
	/System/Library/StartupItems/Firebird/Firebird start
fi
