#!/bin/ksh
#
# Script for running the C/C++ compiler when building python modules
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2004-2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

MYNAME=`basename $0`

# name of the compiler executable
CCEXE='cc'
# name of the GNU compiler executable
GCCEXE='gcc'
# name of the programming language
CLANG='C'
# name of the env variable for setting the compiler path
CVAR='CC'

if [ "x$MYNAME" = xpyCC ]; then
    CCEXE='CC'
    GCCEXE='g++'
    CLANG='C++'
    CC="$CXX"
    CVAR='CXX'
fi

# check if the CC env variable is set
if [ "x$CC" != x ]; then
    case "$CC" in
	/* )
	    # $CC is an absolute path name
            # check if $CC exists
	    if [ ! -e "$CC" ]; then
		echo "WARNING: pycc: $CC not found" 1>&2
		CC=
	    else
        # check if $CC is an executable
		if [ ! -x "$CC" -o ! -f "$CC" ]; then
		    echo "WARNING: pycc: $CC is not an executable" 1>&2
		    CC=
		fi
	    fi
	    ;;
	* )
	    # try to find $CC in the PATH
	    IFS=:
	    NEW_CC=
	    for dir in $PATH; do
		if [ -x "$dir/$CC" ]; then
		    NEW_CC="$dir/$CC"
		    break
		fi
	    done
	    if [ "x$NEW_CC" = x ]; then
		echo "WARNING: pycc: $CC not found" 1>&2
		CC=
	    else
		CC="$NEW_CC"
	    fi
	    ;;
    esac
fi

if [ "x$CC" == x ]; then
    # Look for the Sun Studio compiler in the PATH
    IFS=:
    for dir in $PATH; do
	if [ -x "$dir/$CCEXE" ]; then
	    CC="$dir/$CCEXE"
	    break
	fi
    done
fi

if [ "x$CC" == x ]; then
    # Look for gcc in the PATH
    IFS=:
    for dir in $PATH; do
	if [ -x "$dir/$GCCEXE" ]; then
	    CC="$dir/$GCCEXE"
	    break
	fi
    done
fi

if [ "x$CC" == x ]; then
    # Check for Sun Studio in /opt/SUNWspro (default install location)
    if [ -x /opt/SUNWspro/bin/$CCEXE ]; then
	CC=/opt/SUNWspro/bin/$CCEXE
    fi
fi

if [ "x$CC" == x ]; then
    # Check for the GNU compiler in /usr/sfw/bin
    if [ -x /usr/sfw/bin/$GCCEXE ]; then
	CC=/usr/sfw/bin/$GCCEXE
    fi
fi

if [ "x$CC" == x ]; then
    # Cannot continue without a C compiler
    echo "ERROR: no $CLANG compiler not found; update your PATH or set the $CVAR env variable" 1>&2
    exit 1
fi

exec "$CC" "${@}"
