#!/bin/sh
#			Copyright (c) 1994 by
#			Advanced Visual Systems Inc.
#			All Rights Reserved
#
#	This software comprises unpublished confidential information of
#	Advanced Visual Systems Inc. and may not be used, copied or made
#	available to anyone, except in accordance with the license
#	under which it is furnished.
#
#	This file is under Perforce control
#	$Id: //depot/express/fcs70/build/xp_make.sh#1 $
#

BATCH=0
MAKE_ARG=
NAME="<default>"
DIRNAME="."

if [ "$MACHINE" = "" ] ; then

   SYS_NAME=`uname -s`

   if [ "$SYS_NAME" = "SunOS" ] ; then
      VERSION=`uname -r | sed -e 's/\..*$//'` 
      if [ "$VERSION" = "5" ] ; then
         MINOR=`uname -r | cut -f2 -d.`
         if [ "$MINOR" = "5" -o "$MINOR" = "6" ] ; then
            MACHINE=s2
         elif [ "$MINOR" = "7" ] ; then
            MACHINE=s7
         else
            MACHINE=s8
         fi
      else
         echo "OS-name: $SYS_NAME is unknown"
      fi
   elif [ "$SYS_NAME" = "HP-UX" ] ; then
      MAJOR_VERSION=`uname -r | sed -e 's/B\.//' -e 's/\..*$//'`
      if [ "$MAJOR_VERSION" = "10" ] ; then
         MACHINE=hp1020
      elif [ "$MAJOR_VERSION" = "11" ] ; then
         MACHINE=hp11
      else
         echo "OS-name: $SYS_NAME is unknown"
      fi
   elif [ "$SYS_NAME" = "IRIX" ] ; then
      MACHINE=sgN32
   elif [ "$SYS_NAME" = "IRIX64" ] ; then
      MACHINE=sg6
   elif [ "$SYS_NAME" = "AIX" ] ; then
      MACHINE=ibm
   elif [ "$SYS_NAME" = "OSF1" ] ; then
      MACHINE=alpha
   elif [ "$SYS_NAME" = "Linux" ] ; then
      if [ `uname --machine` = "ia64" ] ; then
         # Intel Itanium
         MACHINE=linux_ia64
      elif [ `uname --machine` = "x86_64" ] ; then
         # AMD64, Intel EM64T
         MACHINE=linux_64
      else
         # Standard 32-bit x86.
         # Use the compiler to make a guess as to which version of Red Hat
         COMPILER_VERSION=`gcc --version | grep -c 3.2`
         if [ "$COMPILER_VERSION" -ge 1 ] ; then
            echo Red Hat 8.0 or compatible detected
            MACHINE=linux
         else
            COMPILER_VERSION=`gcc --version | grep -c 2.96`
            if [ "$COMPILER_VERSION" -ge 1 ] ; then
               echo Red Hat 7.0 or compatible detected
               MACHINE=linux_rh7
            else
               echo Unknown C compiler, assuming platform Linux Red Hat 8.0 or compatible
               MACHINE=linux
            fi
         fi
      fi
   elif [ "$SYS_NAME" = "Darwin" ] ; then
      MACHINE=darwin
   else
      echo "OS-name: $SYS_NAME is unknown"
   fi
   export MACHINE
fi

# Use 'make' if $MAKE not in environment.  $MAKE is generally already set
# only when this is run by a top-level make, not from the shell.
: ${MAKE:=make}

while [ $# -gt 0 ] ; do
  case $1 in
    -batch)     BATCH=1
                ;;
    -*)		echo "xp_make: unrecognized option: $1"
		;;
    *)          MAKE_ARG="-f $1"
		NAME=$1
		DIRNAME=`dirname $1`
		;;
  esac
  shift
done

#
# if the display is not set, go back to doing it the old fashioned way...
#
if [ "$DISPLAY" = "" ] ; then
   BATCH=1
fi

#
# if there is no default path to XP_XTERM set, we need to look in standard
# places in case our user does not have the path set properly.
#
if [ "$XP_XTERM" = "" ] ; then
   if [ -x /usr/bin/X11/xterm ] ; then 
      XP_XTERM=/usr/bin/X11/xterm
   elif [ -x /usr/openwin/bin/xterm ] ; then
      XP_XTERM=/usr/openwin/bin/xterm
   elif [ -x /usr/X11R6/bin/xterm ] ; then
      XP_XTERM=/usr/X11R6/bin/xterm
   else
      XP_XTERM=xterm
   fi
fi

if [ "$BATCH" = "1" ] ; then
   if [ "$DIRNAME" = "." ] ; then
      :
   else
      echo "xp_make: changing directory to: $DIRNAME"
      cd $DIRNAME
   fi
   $MAKE $MAKE_ARG
else 
   $XP_XTERM -sl 1000 -sb -T "Make Output For $NAME" -e \
     sh -c "echo \"Compile Output for: $NAME\"; cd $DIRNAME; $MAKE $MAKE_ARG; echo '-------- Hit return to close'; read FOO"
fi
