#!/bin/sh
# The contents of this file are subject to the Interbase Public
# License Version 1.0 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy
# of the License at http://www.Inprise.com/IPL.html
#
# Software distributed under the License is distributed on an
# "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
# or implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code was created by Inprise Corporation
# and its predecessors. Portions created by Inprise Corporation are
# Copyright (C) Inprise Corporation.
#
# All Rights Reserved.
# Contributor(s): ______________________________________.
# Shell script to perform special optimization functions.  It can
# force optimization, prevent optimization, or do nothing.
if [ "$1" = '?' ]; then
    echo 'Usage: special_opt [YES] compile_line'
    echo '                   [NO ]'
fi
#
SYSTEM=$(SYSTEM)
#
# Set default optimize and debug flags
FLAGS_OPT=-O
FLAGS_DEBUG=-g
#
# Set system specific optimize and debug flags
if [ "$SYSTEM" = 'NXT' ]; then
#   On NeXT, optimization and debugging are NOT mutually exclusive
    FLAGS_DEBUG=
fi
if [ "$SYSTEM" = 'WIN_NT' -o "$SYSTEM" = 'OS2' -o "$SYSTEM" = 'WIN_NTS' ]; then
    PATH="$PATH;$Path"
    export PATH
#   On Windows NT, optimization and debugging are NOT mutually exclusive
    FLAGS_OPT=-Ob2gtp
    FLAGS_DEBUG=
fi
#
# If the first argument isn't YES or NO, just execute the command, as is
if [ "$1" != 'YES' -a "$1" != 'NO' ]; then
    set -x
    $@
    exit $?
fi
#
OPTIMIZE_CASE=$1
shift 1
#
if [ "$OPTIMIZE_CASE" = 'YES' ]; then
    REMOVE=$FLAGS_DEBUG
    INCLUDE=$FLAGS_OPT
else
    REMOVE=$FLAGS_OPT
    INCLUDE=
fi
#
COMPILE_LINE=
PRIOR=
INCLUDED=0
for i
do
    COMPILE_LINE="$COMPILE_LINE $PRIOR"
    if [ "$i" != "$REMOVE" ]; then
	PRIOR=$i
	if [ "$i" = "$INCLUDE" ]; then
	    INCLUDED=1
	fi
    else
	PRIOR=
    fi
done
#
if [ $INCLUDED -eq 1 ]; then
    INCLUDE=
fi
COMPILE_LINE="$COMPILE_LINE $INCLUDE $PRIOR"
set -x
eval $COMPILE_LINE
exit $?
