#!/bin/sh
#set -e -x

SYSTEM_LIBS="/usr/lib"

INSTALL_VERSION="4039"
PACKAGE_NUMBER="02032514"
TARGET_OS="Linux"
USE_RANLIB=""
INSTALL_PRODUCT="Open Server"
INSTALLATION_REQUIRED="NO"
SERVER_TYPE="OS"
SOFTWARE_TYPE=""


BIN_FILES="pbcon pbee pbac PrimeBaseCtl"
SERVER_FILES="pbos pbos_stop smm"
UPDATEABLE_RESOUCES="pbos_plugins.info documentation.html manual setup/plugins start_open_server stop_open_server open_server_console"
INSTALL_RESOUCES="setup pbos.key"

LIB_FILES="libpbvm.so"
INC_FILES=""
PLUGINS=""
SCRIPT_FILES=""
SETUP_FILES="setup/unicode"
COND_SCRIPT_FILES=""
COND_SETUP_FILES=""

INSTALLER_NAME="Install_PrimeBase_OS"

#===========================================================================
#===========================================================================

# Paramaters: 
#	-q : Quiet mode, don't ask for input, use defaults.
#	-d : Dirty mode, don't do a clean up after installation.
dirty=0
quiet=0

for i in $0
do
	case $i in 
		"-d" )
			dirty=1;
			;;
			
		"-q" )
			quiet=1;
			;;
	esac
done

if [ `uname` = "SunOS" ]
then
	whoiam=`/usr/ucb/whoami`
else
	whoiam=`whoami`
fi
 
installer_home=`pwd`

SYSTEM_LIBS="/usr/lib"

INST_LOCATION="."

if [ "$PRIMEBASEHOME" = "" ] 
then
	PRIMEBASEHOME="/usr/local/primebase"
fi

LOG="/tmp/PrimeBase.log"
if [ ! -f $LOG ]
then
	echo "PrimeBase Installer log." > $LOG 
	chmod 777 $LOG 

fi

WDIGLOG="$installer_home/Where_Did_It_GO"
VERSION_FILE="$PRIMEBASEHOME/PBinstaller.info"
VERIFY_VERSION="y"

PB_BIN="$PRIMEBASEHOME/bin"
PB_LIB="$PRIMEBASEHOME/lib"
PB_INC="$PRIMEBASEHOME/include"
PB_SETUP="$PRIMEBASEHOME/primebase.setup"
PB_SCRIPTS="$PRIMEBASEHOME/primebase.setup/scripts"
PB_PLUGINS="$PRIMEBASEHOME/primebase.setup/plugins"

EVERY_THING="\
$BIN_FILES \
$SERVER_FILES \
$UPDATEABLE_RESOUCES \
$INSTALL_RESOUCES \
$LIB_FILES \
$INC_FILES \
$PLUGINS \
$SCRIPT_FILES \
$SETUP_FILES \
$COND_SCRIPT_FILES \
$COND_SETUP_FILES \
$KEY_FILE \
$INSTALLER_NAME \
pbstab \
MacOSX_Startup \
"

echo "PrimeBase Installation" | tee  $LOG
echo "Product: $INSTALL_PRODUCT" | tee -a $LOG
echo "Version: $INSTALL_VERSION" | tee -a $LOG
echo "Package Number: $PACKAGE_NUMBER" | tee -a $LOG
echo "--------------------------------------------------" | tee -a $LOG
echo "" | tee -a $LOG



if [ ! -s $VERSION_FILE ]
then
	echo "PrimeBase installer info file." > $VERSION_FILE
	echo "This file contains a list of the currently installed PrimeBase products." >> $VERSION_FILE
	echo "Please do not modify this file or the PrimeBase installer may not work correctly." >> $VERSION_FILE
	echo "---------------------------------------------------------------------------------" >> $VERSION_FILE  	
fi
    
 

#----------------------------------------------------
yesno() {
    str=$1;
    if [ $quiet -eq 1 ]
    then
		yes=$2;
		return;
    fi
    
    reply="Huh?";
    while [ 1 ]
    do
        echo "$str:[y/n]"  | tee -a $LOG
        read reply;

        if [ "$reply" = "y" -o "$reply" = "Y" ]
        then
            yes=1;
            return;
        fi

        if [ "$reply" = "n" -o "$reply" = "N" ]
        then
            yes=0;
            return;
        fi
    done;
}

#----------------------------------------------------
check_version() {
    file_name=$1;

    check_version_ok=0;
    
	check_version_ok=1;
    old_info=`grep _$file_name $VERSION_FILE`
 	if [ $? -eq 0 ]
 	then
 		set $old_info
 	else
 		old_info=""
  		set x x 0
	fi
 	
    if [ "$old_info" != "" -a $# -eq 3 -a "$1" = "_$file_name" -a $PACKAGE_NUMBER -lt $3 ]
    then
    	echo "A newer version of \"$file_name\" already exists."   | tee -a $LOG
    	echo "Existing files version \"$2\" and build number \"$3\"."  | tee -a $LOG
    	echo "Installer files version \"$INSTALL_VERSION\" and build number \"$PACKAGE_NUMBER\"."  | tee -a $LOG
   		yesno "Do you want to overwrite the existing version of $file_name" 1
    	check_version_ok=$yes
     fi
	
}

#----------------------------------------------------
set_version() {
    file_name=$1;

    check_version_ok=0;
    
 	mv $VERSION_FILE $VERSION_FILE.tmp
 	grep -v _$file_name $VERSION_FILE.tmp > $VERSION_FILE 
 	echo "_$file_name $INSTALL_VERSION $PACKAGE_NUMBER" >> $VERSION_FILE 
 	rm -f $VERSION_FILE.tmp
 	
	
}

#----------------------------------------------------
install_file() {
    file_src=$1;
    file_dst=$2;
    
    file_name=`basename $file_src`
	file_dst="$file_dst/$file_name"
	
    if [ ! -s $file_src ]
    then
   		# It may already be installed.
	    old_info=`grep -s _$file_name $VERSION_FILE`
	 	if [ $? -eq 0 ]
	 	then
	 		set $old_info
	 	else
 			old_info=""
  			set x x 0
	 	fi
	    
	    if [ "$old_info" = "" -o $# -ne 3 -o "$1" != "_$file_name" -o $PACKAGE_NUMBER -ne $3 ]
	    then
	    	echo ""  | tee -a $LOG
	    	echo "!!!!! Missing File !!!!!!"  | tee -a $LOG
	    	echo "File \"$file_name\" is missing from your package and could not be installed."  | tee -a $LOG
	    	echo ""  | tee -a $LOG
	     fi

		return
	fi
	
    if [ -s $file_dst ]
    then
    	if [ $NO_OVERWRITE -eq 1 ]
    	then
    		return
    	fi
    		
	   	if [ "$VERIFY_VERSION" = "y" ]
	    then
	    	check_version $file_name
	    	if [ $check_version_ok -eq 0 ]
	    	then
	    		return
	    	fi
	    fi
	    rm -rf $file_dst
    fi
    
    cp -r $file_src $file_dst
	if [ $? -ne 0 ]
	then
		echo "cp $file_src $file_dst"  | tee -a $LOG
		echo "Failed!"  | tee -a $LOG
		echo "Please check the permissions."  | tee -a $LOG
		exit
	else
    	set_version $file_name
    fi
     
}
#----------------------------------------------------
#stop_start_server: cmd dstype location run_as
stop_start_server() {

	
	server_was_running=0
	
	PID_FILE="$3/.$2.pid"
	
	if [ -f $PID_FILE ]
	then
                pid=`cat $PID_FILE`
                if [ `uname` = "Rhapsody" ]
                then
                        ps $pid | grep $pid >> /dev/null
                else
                        ps -p$pid | grep $pid >> /dev/null
                fi
		if [ $? -eq 0 ]
		then
			server_was_running=1
		fi
	fi

	if [ $server_was_running -eq 1 -a $1 = "Start" ]
	then
		return
	fi
	
	if [ $server_was_running -eq 0 -a $1 = "Stop" ]
	then
		return
	fi
	
	startstopscript=""
	
	case $2 in
		"DS" )
			if [ $1 = "Start" ]
			then
				echo "Starting Data Server"  | tee -a $LOG
				startstopscript="start_data_server"
			else
				startstopscript="stop_data_server"
			fi
		;;
		
		"AS" )
			if [ $1 = "Start" ]
			then
				echo "Starting Standard Application Server"  | tee -a $LOG
				startstopscript="start_standard_server"
			else
				startstopscript="stop_application_server"
			fi
		;;
		
		"OS" )
			if [ $1 = "Start" ]
			then
				echo "Starting Open Server"  | tee -a $LOG
				startstopscript="start_open_server"
			else
				startstopscript="stop_open_server"
			fi
		;;
	esac
	
	if [ "$startstopscript" != "" ]
	then
		if [ "$whoiam" = "$4" ]
		then
			$3/$startstopscript
		else
			su - $4 -c $3/$startstopscript
		fi
	fi
}


#----------------------------------------------------
update_server() {
    dstype=$1;
    location=$2;
 
 	home=`pwd`
    cd $location   
    
	INST_LOCATION=$location
	
 	awk_cmd='{found = 0; if (( NF == 5) && ( found == 0) && ( $1 == "'$dstype'" ) && ( $3 == "'$location'" ) ) {print $2; print $4;found = 1;}}'
	list=`awk "$awk_cmd" $PRIMEBASEHOME/pbstab`

	set $list
	ver=$1
	start_as=$2
	
 	# update the version number
 	awk_cmd='{found = 0; if (( NF == 5) && ( found == 0) && ( $1 == "'$dstype'" ) && ( $3 == "'$location'" ) ) {print $1, "'$INSTALL_VERSION'", $3, $4, $5; found = 1;} else {print $0;}}'

	mv $PRIMEBASEHOME/pbstab $PRIMEBASEHOME/pbstab.tmp
    awk "$awk_cmd" $PRIMEBASEHOME/pbstab.tmp > $PRIMEBASEHOME/pbstab
    rm $PRIMEBASEHOME/pbstab.tmp 
	

   stop_start_server "Stop" $dstype $location $start_as
   
    
    if [ ! -d $ver.saved ]
    then
    	mkdir $ver.saved
    fi
    
    # Save the old version
    for i in $UPDATEABLE_RESOUCES $SERVER_FILES
    do
    	if [ -s $i ]
    	then
    		mv $i $ver.saved
    	fi
    done
    
    mv *.txt readme.* $ver.saved
    
    # Get the new version
    for i in $UPDATEABLE_RESOUCES $SERVER_FILES
    do
    	cp -r $installer_home/$i .
    done

    cp $installer_home/*.txt $installer_home/readme.* .

	chown -R  $start_as $location
    
    #Restart the server if it was running.
    if [ $server_was_running -eq 1 ]
    then
    	stop_start_server "Start" $dstype $location $start_as
    fi
    
    cd $home
}

#----------------------------------------------------
# PrimeBase Autostart installer.

install_boot_script() {

BOOT_TYPE=`uname`

case $BOOT_TYPE in
	
	"Linux" | "SunOS" )
		init_locations="/etc /etc/rc.d /sbin "
		
		for i in $init_locations
		do
			if [ -d $i/init.d ] 
			then
				INITD=$i
				break
			fi
		done
		
		if [ "$INITD" = "" ]
		then
			echo 'Could not find "init.d" directory on this system.'  | tee -a $LOG
			echo 'PrimeBase Auto Start could not be installed.' | tee -a $LOG
		else
			if [ -s $INITD/init.d/PrimeBaseCtl ]
			then
				cp /usr/local/primebase/bin/PrimeBaseCtl $INITD/init.d
			else
				cp /usr/local/primebase/bin/PrimeBaseCtl $INITD/init.d
				chmod +x $INITD/init.d/PrimeBaseCtl
				ln -s $INITD/init.d/PrimeBaseCtl $INITD/rc3.d/S99PrimeBaseCtl 

				ln -s $INITD/init.d/PrimeBaseCtl $INITD/rc0.d/K99PrimeBaseCtl
				ln -s $INITD/init.d/PrimeBaseCtl $INITD/rc1.d/K99PrimeBaseCtl
				ln -s $INITD/init.d/PrimeBaseCtl $INITD/rc2.d/K99PrimeBaseCtl 
				if [ -d $INITD/rcS.d ]
				then
					ln -s $INITD/init.d/PrimeBaseCtl $INITD/rcS.d/K99PrimeBaseCtl 
				fi
			fi
		fi
		
	;;
	
	"AIX" )
		
		lsitab PrimeBase >> /dev/null
		if [ $? -eq 0 ]
		then
			echo 'PrimeBase Auto Start was already installed.' | tee -a $LOG
		else
			mkitab "PrimeBase:2:wait:sh /usr/local/primebase/bin/PrimeBaseCtl start"
		fi
		
	;;

	"Darwin" )
		
		if [ ! -d /Library/StartupItems/PrimeBase ]
		then
			if [ ! -d /Library/StartupItems ]
			then
				mkdir /Library/StartupItems
			fi

			cp -r $installer_home/MacOSX_Startup /Library/StartupItems/PrimeBase
			chmod -R 775 /Library/StartupItems/PrimeBase
		fi
	;;

	"Rhapsody" )
		
		if [ ! -s /etc/startup/2000_PrimeBase ]
		then
			cp  $installer_home/MacOSX_Startup/PrimeBase /etc/startup/2000_PrimeBase
			chmod -R 775  /etc/startup/2000_PrimeBase
		fi
	;;


esac	

}

#----------------------------------------------------
install_server() {
    dstype=$1;
    location=$2;
    home=`pwd`
    
    echo "Installing the server into: $location"  | tee -a $LOG
    echo "The server was installed into: $location"  >> $WDIGLOG
    
    cd $location
	INST_LOCATION=$location
	
    # Get the new version
    for i in $UPDATEABLE_RESOUCES $SERVER_FILES $INSTALL_RESOUCES
    do
    	cp -r  $installer_home/$i .
    done
    
    # Create links to the files placed in the bin directory.
    for i in  $BIN_FILES
    do
    	if [ $i != "PrimeBaseCTL" ]
    	then
    		ln -s  $PB_BIN/$i .
    	fi
    done
    
    cp $installer_home/*.txt $installer_home/readme.* .
    
	yesno "Do you want the server to be started at boot time?" 1
	if [ $yes -eq 1 ]
	then
		install_boot_script
		bootme="Yes"
	else
		bootme="No"
	fi
	
	if [ $quiet -eq 1 ]
	then
		run_as=$whoiam
	else
		run_as=""
	fi
	
    while [ "$run_as" = "" ]
    do
    	reply=""
        echo "What user do you want the server to be run as (default: $whoiam):"  | tee -a $LOG
        read reply;

        if [ "$reply" = "" ]
        then
            run_as=$whoiam
		else
         	chown -R $reply $location
            run_as=$reply
       fi

    done;
	    
	
	# Add the server to the list of installed servers.
	echo "$dstype $INSTALL_VERSION $location $run_as $bootme" >> $PRIMEBASEHOME/pbstab

	yesno "Do you want the server to be started now?" 1
	if [ $yes -eq 1 ]
    then
    	stop_start_server "Start" $dstype $location $run_as
    fi
	
	
    cd $home
    
}


#=============================================================
#=============================================================
now=`date`
echo "============================================================="  | tee -a $LOG | tee -a $WDIGLOG
echo "DATE: $now"  | tee -a $LOG | tee -a $WDIGLOG
echo "Installing: \"$INSTALL_PRODUCT\" Version:  \"$INSTALL_VERSION\" Build: \"$PACKAGE_NUMBER\" "  | tee -a $LOG | tee -a $WDIGLOG
echo "As user: $whoiam"  | tee -a $LOG | tee -a $WDIGLOG
echo ""  | tee -a $LOG | tee -a $WDIGLOG
echo ""  | tee -a $LOG 
echo "The PrimeBase components will be installed into: $PRIMEBASEHOME"  | tee -a $LOG 
echo ""  | tee -a $LOG 
echo ""  | tee -a $LOG 

echo "The PrimeBase components were installed into: $PRIMEBASEHOME"  >> $WDIGLOG | tee -a $WDIGLOG

#-----------------
# PRIMEBASEHOME is required regardless of if the package is installed or not.

if [ ! -d $PRIMEBASEHOME ] 
then
	mkdir $PRIMEBASEHOME
	if [ $? -ne 0 ]
	then
		echo "mkdir $PRIMEBASEHOME"  | tee -a $LOG
		echo "Failed!"  | tee -a $LOG
		echo "Please check the permissions."  | tee -a $LOG
		echo "$PRIMEBASEHOME is required regardless of if the package is installed or not."  | tee -a $LOG
		exit
	fi
	
	chmod 777 $PRIMEBASEHOME
fi

echo "$PATH" | grep "$PB_BIN" >> /dev/null
if [ $? -ne 0 ]
then
	echo ""  | tee -a $LOG
	echo "IMPORTANT!"  | tee -a $LOG | tee -a $WDIGLOG
	echo "You must add \"$PB_BIN\" to your search path to access the installed binaries."  | tee -a $LOG | tee -a $WDIGLOG
	echo ""  | tee -a $LOG
fi


if [ "$INSTALLATION_REQUIRED" = "NO" ]
then
	echo "#############################################################"  | tee -a $LOG
	echo ""  | tee -a $LOG
	echo "This product can be used as is with out doing an installation."  | tee -a $LOG
	echo ""  | tee -a $LOG
	echo "The advantage of doing an installation is that all users on this"  | tee -a $LOG
	echo "machine will be able to access the installed files and everyone will"  | tee -a $LOG
	echo "be using the same version."  | tee -a $LOG
	echo ""  | tee -a $LOG
	echo "If you do not want to make this product globaly available on this machine"  | tee -a $LOG
	echo "then do not install it and just use it as it is in the currrent directory."  | tee -a $LOG
	echo "You can always install it later. This is a good idea if this is a new version"  | tee -a $LOG
	echo "and you do not want to disturb a currently installed version."  | tee -a $LOG
	echo ""  | tee -a $LOG
	echo "#############################################################"  | tee -a $LOG
	yesno "Do you want to continue with the installation?" 1
	if [ $yes -eq 0 ]
	then
		exit
	fi
fi

if [ "$SERVER_FILES" != "" -a  "$whoiam" != "root" ]
then
	echo ""  | tee -a $LOG
	echo ""  | tee -a $LOG
	echo "If you want to configure servers to be started at boot time then you "  | tee -a $LOG
	echo 'must run the installer as user "root".'  | tee -a $LOG
	yesno "Do you want to stop now and restart the installer as root?" 0
	if [ $yes -eq 1 ]
	then
		exit
	fi
	echo ""  | tee -a $LOG
	
fi

echo ""  | tee -a $LOG 
echo "Installing the PrimeBase Product: \"$INSTALL_PRODUCT\" Version:  \"$INSTALL_VERSION\" Build: \"$PACKAGE_NUMBER\" "  | tee -a $LOG
echo "Into $PRIMEBASEHOME"  | tee -a $LOG
echo ""  | tee -a $LOG


#-------------------------------------------------------------
# Setup the environment if required.
#

#-----------------
if [ ! -d $PB_SETUP ] 
then
	mkdir $PB_SETUP
	if [ $? -ne 0 ]
	then
		echo "mkdir $PB_SETUP"  | tee -a $LOG
		echo "Failed!"  | tee -a $LOG
		echo "Please check the permissions."  | tee -a $LOG
		exit
	fi
    chmod 777 $PB_SETUP
fi

#-----------------
if [ ! -d $PB_PLUGINS ] 
then
	mkdir $PB_PLUGINS
	if [ $? -ne 0 ]
	then
		echo "mkdir $PB_PLUGINS"  | tee -a $LOG
		echo "Failed!"  | tee -a $LOG
		echo "Please check the permissions."  | tee -a $LOG
		exit
	fi
    chmod 755 $PB_PLUGINS
fi

#-----------------
if [ ! -d $PB_SCRIPTS ] 
then
	mkdir $PB_SCRIPTS
	if [ $? -ne 0 ]
	then
		echo "mkdir $PB_SCRIPTS"  | tee -a $LOG
		echo "Failed!"  | tee -a $LOG
		echo "Please check the permissions."  | tee -a $LOG
		exit
	fi
	chmod 755 $PB_SCRIPTS
fi

#-----------------
if [ ! -d $PB_BIN ] 
then
	mkdir $PB_BIN
	if [ $? -ne 0 ]
	then
		echo "mkdir $PB_BIN"  | tee -a $LOG
		echo "Failed!"  | tee -a $LOG
		echo "Please check the permissions."  | tee -a $LOG
		exit
	fi
	chmod 755 $PB_BIN
fi

#-----------------
if [ ! -d $PB_LIB ] 
then
	mkdir $PB_LIB
	if [ $? -ne 0 ]
	then
		echo "mkdir $PB_LIB"  | tee -a $LOG
		echo "Failed!"  | tee -a $LOG
		echo "Please check the permissions."  | tee -a $LOG
		exit
	fi
	chmod 755 $PB_LIB
fi


#-----------------
if [ ! -d $PB_INC ] 
then
	mkdir $PB_INC
	if [ $? -ne 0 ]
	then
		echo "mkdir $PB_INC"  | tee -a $LOG
		echo "Failed!"  | tee -a $LOG
		echo "Please check the permissions."  | tee -a $LOG
		exit
	fi
	chmod 755 $PB_INC
fi


#-------------------------------------------------------------
if [ "$BIN_FILES" != "" ]
then
	NO_OVERWRITE=0
	echo "Installing: $BIN_FILES"  | tee -a $LOG | tee -a $WDIGLOG
	echo "Into: $PB_BIN"  | tee -a $LOG | tee -a $WDIGLOG
	echo ""  | tee -a $LOG | tee -a $WDIGLOG
	
	for i in $BIN_FILES
	do
		install_file $i  $PB_BIN
	done
fi

#-------------------------------------------------------------
if [ "$LIB_FILES" != "" ]
then
	NO_OVERWRITE=0
	echo "Installing: $LIB_FILES"  | tee -a $LOG | tee -a $WDIGLOG
	echo "Into: $PB_LIB"  | tee -a $LOG | tee -a $WDIGLOG
	echo ""  | tee -a $LOG | tee -a $WDIGLOG
	
	ln_retry=""
	ln_failed="N"
	
	for i in $LIB_FILES
	do
		install_file $i $PB_LIB
		bname=`basename $i .a`
		bname="$bname.a"
		if [ "$USE_RANLIB" != ""  -a "$i" = "$bname" ]
		then
			ranlib $PB_LIB/$i
		fi
		
		if [ ! -f "$SYSTEM_LIBS/$i" ]
		then
			if [ "$ln_failed" = "N" ]
			then
			
				echo "Creating a symbolic link from $PB_LIB/$i to $SYSTEM_LIBS/$i"  | tee -a $LOG
				ln -s  $PB_LIB/$i $SYSTEM_LIBS/$i
				if [ $? -ne 0 ]
				then
					echo ""  | tee -a $LOG
					echo "Failed to create symbolic link."  | tee -a $LOG
					echo "Probable cause is that you are not the user 'root'."  | tee -a $LOG
					echo "If the library isn't in $SYSTEM_LIBS then you must add $PB_LIB"  | tee -a $LOG
					echo "to system variable LD_LIBRARY_PATH in order for the dynamic loader"  | tee -a $LOG
					echo "to find it."  | tee -a $LOG
					echo ""  | tee -a $LOG
					ln_failed="Y"  | tee -a $LOG
					echo "ln -s  $PB_LIB/$i $SYSTEM_LIBS/$i " > link_libs
				fi
			else
				echo "ln -s  $PB_LIB/$i $SYSTEM_LIBS/$i " >> link_libs
				
			fi
			
		fi
		

	done
	
	if [ "$ln_failed" = "Y" ]
	then
		chmod +x link_libs
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"  | tee -a $LOG  | tee -a $WDIGLOG
		echo "After installation login as root and execute the file 'link_libs'."  | tee -a $LOG | tee -a $WDIGLOG
		echo "This will create the symbolic link that failed to be created above."  | tee -a $LOG | tee -a $WDIGLOG
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"  | tee -a $LOG | tee -a $WDIGLOG
		echo ""  | tee -a $LOG | tee -a $WDIGLOG
		
	fi
fi

if [ "$INC_FILES" != "" ]
then
	NO_OVERWRITE=0
	echo "Installing: $INC_FILES"  | tee -a $LOG | tee -a $WDIGLOG
	echo "Into: $PB_INC"  | tee -a $LOG | tee -a $WDIGLOG
	echo ""  | tee -a $LOG | tee -a $WDIGLOG
	
	for i in $INC_FILES
	do
		install_file $i $PB_INC
	done
fi


if [ "$PLUGINS" != "" ]
then
	NO_OVERWRITE=0
	echo "Installing: $PLUGINS"  | tee -a $LOG | tee -a $WDIGLOG
	echo "Into: $PB_PLUGINS"  | tee -a $LOG | tee -a $WDIGLOG
	echo ""  | tee -a $LOG | tee -a $WDIGLOG
	
	for i in $PLUGINS
	do
		install_file $i  $PB_PLUGINS
	done
fi


if [ "$SCRIPT_FILES" != "" ]
then
	NO_OVERWRITE=0
	echo "Installing: $SCRIPT_FILES"  | tee -a $LOG | tee -a $WDIGLOG
	echo "Into: $PB_SCRIPTS"  | tee -a $LOG | tee -a $WDIGLOG
	echo ""  | tee -a $LOG | tee -a $WDIGLOG
	
	for i in $SCRIPT_FILES
	do
		install_file $i $PB_SCRIPTS
	done
fi


if [ "$COND_SCRIPT_FILES" != "" ]
then
	NO_OVERWRITE=0
	echo "The following files will be installed if they do not already exist"  | tee -a $LOG | tee -a $WDIGLOG
	echo "Installing: $COND_SCRIPT_FILES"  | tee -a $LOG | tee -a $WDIGLOG
	echo "Into: $PB_SCRIPTS"  | tee -a $LOG | tee -a $WDIGLOG
	echo ""  | tee -a $LOG | tee -a $WDIGLOG
	
	for i in $COND_SCRIPT_FILES
	do
		install_file  $i $PB_SCRIPTS
	done
fi


if [ "$SETUP_FILES" != "" ]
then
	NO_OVERWRITE=0
	echo "Installing: $SETUP_FILES"  | tee -a $LOG | tee -a $WDIGLOG
	echo "Into: $PB_SETUP"  | tee -a $LOG | tee -a $WDIGLOG
	echo ""  | tee -a $LOG | tee -a $WDIGLOG
	
	for i in $SETUP_FILES
	do
		install_file $i $PB_SETUP
	done
fi

if [ "$COND_SETUP_FILES" != "" ]
then
	NO_OVERWRITE=1
	echo "The following files will be installed if they do not already exist"  | tee -a $LOG | tee -a $WDIGLOG
	echo "Installing: $COND_SETUP_FILES"  | tee -a $LOG | tee -a $WDIGLOG
	echo "Into: $PB_SETUP"  | tee -a $LOG | tee -a $WDIGLOG
	echo ""  | tee -a $LOG | tee -a $WDIGLOG
	
	for i in $COND_SETUP_FILES
	do
		install_file  $i $PB_SETUP
	done
fi

if [ -f "Makefile.tmpl" ]
then
        sed "s:<PB_HOME_TAG>:$PRIMEBASEHOME:" Makefile.tmpl | \
        sed 's:<CC>:cc:' | \
        sed 's:<CFLAGS>:-g -I$(PB_INC):' | \
        sed 's/<SO_OPTS>/-g -shared  -fpic/' | \
        sed 's:<APP_OPTS>:-L. -L$(PB_LIB) -g -dynamic :'   > Makefile

	rm -f Makefile.tmpl
fi

# Install the server 
if [ "$SERVER_TYPE" != "" ]
then

	if [ ! -d $PRIMEBASEHOME/$SERVER_TYPE ] 
	then
		mkdir $PRIMEBASEHOME/$SERVER_TYPE 
		if [ $? -ne 0 ]
		then
			echo "mkdir $PRIMEBASEHOME/$SERVER_TYPE"  | tee -a $LOG
			echo "Failed!"  | tee -a $LOG
			echo "Please check the permissions."  | tee -a $LOG
			exit
		fi
	fi
	
	if [ -f $PRIMEBASEHOME/pbstab ]
	then
		awk_cmd='{if (( NF == 5) && ( $1 == "'$SERVER_TYPE'" ) ) print $3;}'
		installed_servers=`awk "$awk_cmd" $PRIMEBASEHOME/pbstab`
	else
		cp $installer_home/pbstab $PRIMEBASEHOME
		installed_servers=""
	fi
	
	for i in $installed_servers
	do
		echo "Do you want to update the server in location: $i"  | tee -a $LOG
		echo "This will involve restarting the server if it is already running."  | tee -a $LOG
		yesno "Do you want to update the server" 1
		if [ $yes -eq 1 ]
		then
			update_server $SERVER_TYPE $i
		fi
	done

	
	if [ "$installed_servers" != "" ]
	then
		yesno "Do you want a new installation of the server" 1
		if [ $yes -eq 1 ]
		then
			installed_servers=""
		fi
		
	fi
	
	if [ "$installed_servers" = "" ]
	then
		cnt=1;
		location="$PRIMEBASEHOME/$SERVER_TYPE/Server$cnt"
		while [ -d $location ]
		do
			cnt=`expr $cnt + 1`
			location="$PRIMEBASEHOME/$SERVER_TYPE/Server$cnt"
		done
		
		mkdir $location
		if [ $? -ne 0 ]
		then
			echo "mkdir $location"  | tee -a $LOG
			echo "Failed!"  | tee -a $LOG
			echo "Please check the permissions."  | tee -a $LOG
			exit
		fi

		install_server  $SERVER_TYPE $location
	fi
	
		
fi

have_manuals="No"
if [ -d manual ]
then
	if [ ! -d $PRIMEBASEHOME/manual ]
	then
		mkdir $PRIMEBASEHOME/manual
	fi
	
	cp -r manual/* $PRIMEBASEHOME/manual
	have_manuals="Yes"
	
fi

echo ""  | tee -a $LOG | tee -a $WDIGLOG
echo "============================================================================="  | tee -a $LOG | tee -a $WDIGLOG
echo ""  | tee -a $LOG | tee -a $WDIGLOG
echo "Installation Completed."  			| tee -a $LOG | tee -a $WDIGLOG
echo ""  									| tee -a $LOG | tee -a $WDIGLOG


list=`ls readme.* *.txt 2>> /dev/null` 


echo "DOCUMENTATION:"  						| tee -a $LOG | tee -a $WDIGLOG
echo "-----------------------------------"  | tee -a $LOG | tee -a $WDIGLOG

if [ -s $INST_LOCATION/documentation.html ]
then
	echo "Documentation for this package can be found in:"  | tee -a $LOG | tee -a $WDIGLOG
	echo ""  									| tee -a $LOG 	| tee -a $WDIGLOG
	echo "$INST_LOCATION/documentation.html"  	| tee -a $LOG | tee -a $WDIGLOG
	echo ""  
fi
									
if [ "$list" != "" ]
then
	echo "Be sure to read the files:" 	| tee -a $LOG 	| tee -a $WDIGLOG 
	echo ""  								| tee -a $LOG 	| tee -a $WDIGLOG
	for i in $list
	do
		echo "$INST_LOCATION/$i"				| tee -a $LOG 	| tee -a $WDIGLOG
	done
	fi
echo ""  									| tee -a $LOG | tee -a $WDIGLOG
echo "-----------------------------------"  | tee -a $LOG | tee -a $WDIGLOG
echo ""  									| tee -a $LOG | tee -a $WDIGLOG


# Do cleanup.
if [ $dirty -eq 0 ]
then
	for i in $EVERY_THING
	do
		if [ -s $i ]
		then
			rm -rf $i
		fi
	done
fi

