#!/bin/sh
# /*ident	"@(#)cls4:CC    1.3" */
###############################################################################
#
# C++ source for the C++ Language System, Release 3.0.  This product
# is a new release of the original cfront developed in the computer
# science research center of AT&T Bell Laboratories.
#
# Copyright (c) 1991 AT&T and UNIX System Laboratories, Inc.
# Copyright (c) 1984, 1989, 1990 AT&T.  All Rights Reserved.
#
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE of AT&T and UNIX System
#	
# Laboratories, Inc.  The copyright notice above does not evidence
# any actual or intended publication of such source code.
#
###############################################################################
# IBM Level 3.0.1 - 12 Oct 92- Based upon the RS/6000 version of the CC shell
#set -x
# This is the AT&T C++ driver.

# PT environment
PTPATH=$PATH
# end PT
PATH=/bin:/usr/bin

# NOTE: To properly use the CC command you should check the values of the
# following variables to be sure they are the locations of the
# appropriate translator files on your system.

# directory containing cfront, c++filt, and patch/munch
CCROOTDIR=${CCROOTDIR-/usr/bin/c++/3.0/bin}

# directory containing the C++ libraries
CCLIBDIR=${CCLIBDIR-/usr/bin/c++/3.0/lib}

# colon-separated list of standard C++ include directories
I=${I-/usr/bin/c++/3.0/include}

# set for +a0 (classic C style declarations) or +a1 (ANSI C style declarations)
AON=${AON-+a1}

# Set to "+L" to generate source line number information using the format
# "#line %d" instead of "# %d".  Some cpp's have trouble when this is set,
# so the default is not to set the variable.
# (MRL) Added +L
LINE_OPT=${LINE_OPT-+L}

# option passed to cc to tell linker to look for libraries in another
# directory.  For example, -Wl,-L.
LOPT=${LOPT--Wl,-L}

# 0 to disable linker output demangling
DEMANGLE=${DEMANGLE-1}

# id of C++ runtime library
LIB_ID=${LIB_ID-C}

# C++ runtime library
LIBRARY=${LIBRARY--l${LIB_ID}}

if test "$CCROOTDIR" != "" ;then
	CCROOTDIR="$CCROOTDIR/"
fi

# location of cfront
cfrontC=${cfrontC-${CCROOTDIR}cfront}

# top level makefile will set this to MUNCH (default) or PATCH
PM_FLAG=PATCH

if test "$PM_FLAG" = "PATCH"
then
        # location of patch
        patchC=${patchC-${CCROOTDIR}patch}
else
        # location of munch
        munchC=${munchC-${CCROOTDIR}munch}
        # location of nm
        NM=${NM-nm}
        # whatever extra options you always want passed to nm
	# MRL Added -e -h
        NMFLAGS=${NMFLAGS-"-e -h"}
fi

# location of c++filt
cplusfiltC=${cplusfiltC-${CCROOTDIR}c++filt}

# location of C compiler
ccC=${ccC-cc}

# whatever extra options you always want passed
# to cpp, for example, -D-+ for fowler cpp
CPPFLAGS=${CPPFLAGS-"-D_AIX -Dps2 -DAIX -DNLS -D_MBCS -Daiws -D_U386 -D_I386"}

# location of preprocessor
# warning: cppC can get overridden by the -Y switch
cppC=${cppC-/lib/cpp}


# The following two environment variables are hooks for library providers

# 1 if CC -fs option is available
FS=${FS-0}

# whatever string the command line argument "-l++" should
# automatically expand into
LPPEXPAND=${LPPEXPAND-"-l++"}

# PT setup
ptrdef="./ptrepository"			# default repository
ptr=${ptrdef}				# repository set by user
ptdmap="defmap"
ptroot=${CCROOTDIR}
ptcompC=${ptcompC-${ptroot}ptcomp}
ptlinkC=${ptlinkC-${ptroot}ptlink}
ptdef=
ptflags=
ptflags2=
ptflags3=
ptrlist=
ptscnt=0
ptT=
ptn=0
clg=0
ptrflag=0
# end PT

#############################################################
# You shouldn't have to change anything below this line
#############################################################
# (MRL) Removed alliant code
if test "$CCLIBDIR" = "/usr/lib"
then LLIBPATH=""
else LLIBPATH="$LOPT$CCLIBDIR"
fi

# sys.fix will set SYS to the designated system
# it will then be defined for each CC invocation
SYS=-DSYSV

export CCROOTDIR
export cppC

# for Ansi compatibility, c_plusplus => __cplusplus
# for backward compatibility, retain c_plusplus for 2.0
CPLUS=-Dc_plusplus=1
cPLUS=-D__cplusplus=1

IDIRS=
while [ "$I" != "" ]; do
	# (MRL) Add comment - Strip up to ':' from $I
	idir=`echo $I | sed 's/:.*$//'`
	if [ "$idir" = "" ]; then
		idir=.
	fi
	# (MRL) Add comment - Add what was stripped to $IDIRS
	IDIRS="$IDIRS -I$idir"
	# (MRL) Add comment - Remove what was stripped from $I for next pass
	case "$I" in
	*:*)	I=`echo $I | sed 's/^[^:]*://'`
		;;
	*)	I=
	esac
done

# (MRL) Changed $O to $CfrontOpt
#		$X to $CompileList
#		$Y to $CPPList
#		$Z to $Linklist
#	Added NextTok
TMPDIR=${TMPDIR-/tmp}
TEMPDIR=$TMPDIR/CC.$$
C= EE=0 FON= XON= CfrontOpt=
PON= R= SUF= CompileList= DASHR= PLUSI= STRIP=
Linklist= E=0 OF= P= NextTok="None"
NOLOAD= NLO=
AFAIL=0
OO="a.out"
FINALNAME=$OO
ISUF=".c"
MVLIST=
APASS=0
defmem=D deflist=
CPPList="$CPPFLAGS"

rm -fr $TEMPDIR
trap 'rm -fr $TEMPDIR; exit' 1 2 3 13 15
mkdir $TEMPDIR
E=$?
if test $E -ne 0
then
     echo "CC: error: cannot create temporary directory in $TEMPDIR: stop" 1>&2
     exit $E
fi
for A do
	case $NextTok in
	OX)     OO=$A
		FINALNAME=$OO
		OF="$OF -o $A"
		NextTok="None"
		continue
		;;
	ION)    CPPList="$CPPList -I$A"
		NextTok="None"
		continue
		;;
	LON)    Linklist="$Linklist -L$A"
		CompileList="$CompileList -L$A"
		NextTok="None"
		continue
                ;;
	XON)    CfrontOpt="$CfrontOpt +x$A"
		NextTok="None"
		continue
		;;
	None)   ;;
	esac

	case $A in
	# PT option parsing 1
	-ptr*)	if [ "$A" = "-ptr" ]
		then
			echo "CC[pt] warning: no repository specified after $A" 1>&2
		else
# (MRL) Changed 999 to 511, the maximum on the AIX
			x=`echo $A | cut -c5-511`
			ptrflag=1
			if [ "$ptrlist" = "" ]
			then
				ptr=$x
				ptrlist=" "
			else
				ptrlist="$ptrlist $x"
			fi
		fi
		;;
	-ptv)	ptflags="$ptflags $A"
		;;
	-pta)	ptflags2="$ptflags2 $A"
		;;
	-pts)	ptflags2="$ptflags2 $A"
		;;
	-ptt)	ptflags2="$ptflags2 $A"
		;;
	-ptn)	ptn=1
		;;
	-pto*)	if [ "$A" = "-pto" ]
		then
			echo 'CC[pt] warning: no pathname specified after -pto' 1>&2
		else
			ptflags2="$ptflags2 $A"
		fi
		;;
	*.[Cc])	if [ $ptscnt -eq 0 ]
		then
			ptscnt=1
		elif [ $ptscnt -eq 1 ]
		then
			ptscnt=2
		fi
		;;
	+T*)	if [ "$A" = "+T" ]
		then
			echo "CC[pt] warning: no pathname specified after +T" 1>&2
		elif [ "$ptT" != "" ]
		then
			echo "CC[pt] warning: second +Tfile illegal, ignored" 1>&2
		else
			ptT=$A
		fi
		;;
	# end PT
	# (MRL) Ordered the rest of the options in alphabetical order
	-c*)	if test "$NOLOAD" != ".i"
		then
			if test "$NOLOAD" != ".s"
			then
				NOLOAD=".o"
				NLO="-c"
			fi
		fi
		;;
	-fs)    if [ $FS -eq 1 ]
		then	CPPList="$CPPList -fs"
		else    echo "CC: -fs not available (ignoring -fs)" 1>&2
		fi
		;;
	-g)	Linklist="$Linklist $A"
		CompileList="$CompileList $A"
		CfrontOpt="$CfrontOpt +g"
		;;
	-l++)	Linklist="$Linklist $LPPEXPAND"
		CompileList="$CompileList $LPPEXPAND"
                ;;  
	-o*)    if test "$A" != "-o"
		then
			OO=`expr $A : '-o\(.*\)'`
			FINALNAME=$OO
			OF="$OF -o $OO"
		#	Linklist="$Linklist -o $OO"
		else
			NextTok="OX"
			continue
		fi
		;;
	-r)	DASHR=1
		Linklist="$Linklist -r"
		;;
	-s)	STRIP=1;
		;;
	-C)	CPPList="$CPPList $A"
		;;
		# (MRL) Added CU code 
	-D*)	eval $defmem='"$A"'
		deflist="$deflist \"\$$defmem\""
		defmem=${defmem}x
		# PT option parsing 2
		ptdef="$ptdef $A"
		# end PT
		;;
	-E)     PON=1
		;;
	-F)	FON=1
		;;
	-Fc)	FON=1
		;;
	-H)	CPPList="$CPPList $A"
		;;
	-I*) 	if test "$A" != "-I" 
		then
		  	CPPList="$CPPList $A"
		else
			NextTok="ION"
			continue
		fi
		;;
	-L*)    if test "$A" != "-L"
		then
			Linklist="$Linklist $A"
		else
			NextTok="LON"
			continue
		fi
		;;
	-P)	PON=1			# pre-process only
		CPPList="$CPPList $A"
		SUF=".i"
		NOLOAD=".i"
		;;
	-S*)	if test "$NOLOAD" != ".i"
		then
			NOLOAD=".s"
			NLO="$A"
		fi
		;;
	-U*)	CPPList="$CPPList $A"
		;;
	-4*)	deflist="$deflist $A"
		;;
	-Yp,*)	cppC=`expr $A : '-Yp,\(.*\)'`/`basename ${cppC}`
		;;
	-YI*)	CompileList="$CompileList $A"
		;;
	-Y0*)	CompileList="$CompileList $A" 
		;;
	-Y2*)	CompileList="$CompileList $A"
		;;
	-Ya*)	CompileList="$CompileList $A"
		;;
	-Y*)	Linklist="$Linklist $A"
		;;
	-.c)	echo "CC: bad suffix option: -.c (ignored)" 1>&2
		;;
	+a?)	AON=$A
		;;
	+x*)    if test "$A" != "+x"
		then
			CfrontOpt="$CfrontOpt $A"
		else
			NextTok="XON"
			continue
		fi
		;;		
	+S)	echo "CC: +S no longer accepted (ignored)" >&2
		;;
	+V)	echo "CC: +V no longer accepted (ignored)" >&2
		;;
	 *)	;;
	esac
done 

CPP()   {
	#echo "$cppC $CPPList -I$I $A " 1>&2
	eval '$cppC' '$CPPList' $deflist '$CPLUS' '$cPLUS' '$SYS' '$IDIRS' '$A' $TARGET
	E=$?
	if test $E -ne 0
	then
		echo "CC: cpp failure: $E" 1>&2
		AFAIL=1
	fi
	}

CFRONT(){
	#echo "$cfrontC ${LINE_OPT} $TempOpt $AON $CfrontOpt $A"
	$cfrontC ${LINE_OPT} +f$A $TempOpt $CfrontOpt $AON <$SOURCE >$TARGET
	E=$?
	AFAIL=1
	case $E in
	0)      AFAIL=0
		;;
	127)    echo "Too many compilation errors" 1>&2
		;;
	999)    echo "CC argument error" 1>&2
		;;
	1)      echo "1 error" 1>&2
		;;
	*)	echo "$E errors" 1>&2
		;;
	esac
	}

TRANSLATE() 
	{
		TARGET='>$TEMPDIR/cpptmp'
		CPPList="-C $CPPList"
		CPP
		if test "$AFAIL" = 1
		then
			break
		fi
		# PT compile-time actions

		# set up for directed mode
		if [ "$ptT" = "" -a $ptscnt -eq 1 -a "$NLO" != "-c" -a $ptn -eq 0 ]
		then
			clg=1
		fi
		if [ $clg -eq 1 ]
		then
			x=$TEMPDIR/dirinst
			echo '@all' > $x
			x="+T$x +s"
		elif [ "$ptT" != "" ]
		then
			x="$ptT +s"
		else
			x=$TEMPDIR/dirinst
			echo '@none' > $x
			x="+T$x +s"
		fi
		$cfrontC ${LINE_OPT} +f$A $x +t$TEMPDIR/ptcf $CfrontOpt $AON <$TEMPDIR/cpptmp >$TEMPDIR/$B$ISUF
		E=$?
		R=1
		C="$C $TEMPDIR/$B$ISUF"
		case $E in
		0)	CompileList="$CompileList $B$ISUF"
			if test "$NLO" != ".c" 
			then
				Linklist="$Linklist $TEMPDIR/$B.o"
			fi
			MVLIST="$MVLIST $B"
			OLDAPASS=$APASS
			APASS=1

			# see if any templates encountered
			err=0
			grep '^[tf] ' $TEMPDIR/ptcf >/dev/null 2>&1
			err2=$?

			# create repository if have to
			if [ $err2 -eq 0 -a ! -d $ptr ]
			then
				mkdir $ptr 2>/dev/null
				err=$?
				if [ "$ptr" = "$ptrdef" ]
				then
					ptflags3="$ptflags3 -ptd"
				fi
			fi
			if [ $err -ne 0 ]
			then
				echo "CC[ptcomp] warning: could not create repository $ptr, ignoring type information" 1>&2
			elif [ -d $ptr -a ! -w $ptr ]
			then
				echo "CC[ptcomp] warning: repository $ptr is not writable, ignoring type information" 1>&2

			# update repository
			elif [ $err2 -eq 0 -o -d $ptr ]
			then
				sort -u $TEMPDIR/ptcf >$TEMPDIR/ptcf2
				if [ "$ptT" != "" ]
				then
					ptflags3="-ptl"
				fi
				# get include list
				ptincl=$TEMPDIR/ptincl
				> $ptincl
				for pti in $CPPList $IDIRS
				do
					echo $pti
				done > $ptincl
				PATH=$PTPATH $ptcompC $PTOPTS $ptflags $ptflags3 $ptr $TEMPDIR/ptcf2 $ptdmap $A $ptincl
				if [ $? -ne 0 ]
				then
					AFAIL=1
					APASS=$OLDAPASS
				fi
			fi
			# end PT
			;;
		127)    echo "Too many compilation errors" 1>&2
			AFAIL=1
			;;
		999)	echo "CC argument error" 1>&2
			AFAIL=1
			;;
		1)	echo "1 error" 1>&2
			AFAIL=1
			;;
		*)	echo "$E errors" 1>&2
			AFAIL=1
			;;
		esac
	}

# (MRL) The only option which should be processed in this second loop are 
# "position-dependant", i.e. they apply only to those files which appear
# later in the command line, not to the entire command line.

for A do
	case $A in
	# PT option parsing 3
	-ptr*)	;;
	-ptv)	;;
	-pta)	;;
	-ptt)	;;
	-pts)	;;
	-ptn)	;;
	-pto*)	;;
	+T*)	;;
	# end PT
	-c*)	;;
	-fs)	;;
	-g) 	;;
	-l++)	;;
	-o*)	if test "$A" = "-o"
		then
		  	SKIP=1
		fi
		;;
	-p|-pg)	P="$P $A"
		;;
	-r)	;;
	-s)	;;
	-C)	;;
	-D*)	;;
	-E)	;;
	-F)	;;
	-Fc)	;;
	-H)	;;
	-I*)	if test "$A" = "-I"
		then
		  	SKIP=1
		fi
		;;
	-L*)	if test "$A" = "-L"
		then
		  	SKIP=1
		fi
		;;
	-P)	;;
	-S*)	;;
	-U*)	;;
	-Y*)	;;
	+a?)	;;
	+i)	PLUSI=1
		;;
	+S)	;;
	+V)	;;
	+x)	if test "$A" != "+x"
		then
		  	SKIP=1
		fi
		;;
	+*)	CfrontOpt="$CfrontOpt $A"
		;;
	-.c)	;;
	-.*)	SUF=`expr "$A" : '-\(.*\)'`
		;;
	# (MRL) Modified to accept the following file extensions: .c .C .cpp 
	# .cxx .CPP and .CXX as valid source files 
	# (i.e. foo.cxx is now a valid C++ source name)
	*.cpp|*.cxx|*.CPP|*.CXX|*.[Cc])
		if	test -f $A
		then
			case $A in
				*.c) B=`basename $A .c` ;;
				*.C) B=`basename $A .C` ;;
				*.cpp) B=`basename $A .cpp` ;;
				*.cxx) B=`basename $A .cxx` ;;
				*.CPP) B=`basename $A .CPP` ;;
				*.CXX) B=`basename $A .CXX` ;;
			esac

			if	test $PON
			then
				if test $SUF
				then
					TARGET='>$B$SUF'
				else 
					TARGET=
				fi
				CPP;
			elif	test $FON
			then
				TARGET='>$TEMPDIR/cpptmp'
				CPPList="-C $CPPList"
				CPP;
				if test "$AFAIL" = 0
				then
					TempOpt=
					SOURCE=$TEMPDIR/cpptmp
					if test $SUF
					then
						TARGET=$B$SUF
					else
						TARGET=
					fi
					CFRONT
				fi
			else
				TRANSLATE;
			fi
		else
			echo "CC: $A not found" 1>&2
			EE=1
		fi
		;;
	*.h)	if	test -f $A
		then

			B=`basename $A .h`

			if	test $PON
			then
				if test $SUF
				then
					TARGET='>$B$SUF'
				else
					TARGET=
				fi
				CPP
				if test "$AFAIL" = 1
				then
					break
				fi
			else
				echo "CC: error: file with unknown suffix: $A" 1>&2
			fi
		else
			echo "CC: $A not found" 1>&2
			EE=1
		fi
		;;
	*.i)    if test -f $A
		then

			if test $PON
			then
				echo "CC -E $A incompatible: ignoring $A" 1>&2
			elif test $FON
			then
				SOURCE=$A
				TARGET=
				CFRONT
  			else	
				B=`basename $A .i`
				SOURCE=$A
				TARGET=$TEMPDIR/$B$ISUF
				CFRONT
				R=1
				C="$C $TEMPDIR/$B$ISUF"
				if test "$AFAIL" = 0
				then
					CompileList="$CompileList $B$ISUF"
					if test "$NLO" != "-c" 
					then
						Linklist="$Linklist $TEMPDIR/$B.o"
					fi
					MVLIST="$MVLIST $B"
					APASS=1
				fi
			fi

		else
			echo "CC: $A not found" 1>&2
			EE=1
		fi
		;;
	*.s)	# add to list to be compiled in $TEMPDIR, set flag to go on
		case $A in
		[\/]*)  # Absolute name given
			B=$A
			;;
		*)	# Relative name given
			B=`pwd`/$A
			;;
		esac
		CompileList="$CompileList $B"
		APASS=1

		if test "$NLO" != "-c" 
		then
			# add object entry to the load list
			Linklist="$Linklist $B"
		fi
		;;
	*.o)	if test "$SKIP" = "1"
		then
			SKIP=
			continue
		fi
		Linklist="$Linklist $A"
		CompileList="$CompileList $A"
		APASS=1 # setting APASS to 1 causes link step to execute
		;;
	*)	if test "$SKIP" = "1"	
		then
			SKIP=
			continue
		fi
		case $A in
		-*)     # This is an option - don't prepend the path
			B=$A
			;;
		[\/]*)  # Absolute name given
			B=$A
			;;
		*)	# Relative name given
			B=`pwd`/$A
			;;
		esac
		Linklist="$Linklist $B"
		CompileList="$CompileList $B"  # ??? or $A
		;;
	esac
done

case $APASS in
0)	;;
*)					# generate code
	if test "$PLUSI"		#leave ..c's lying around
	then
		#remove #line's from the ..c's for sdb
		for f in $C
		do
			sed "/^#/d" $f >$TEMPDIR/temp
			mv $TEMPDIR/temp $f 2> /dev/null
		done
	fi
	
	#if one file failed in cpp or cfront then do not create a.out
	if test $AFAIL -eq 1
	then
		if test -z "$NOLOAD"
		then
			NOLOAD=".o"
			NLO="-c"
		fi
	fi 

	if test "$NOLOAD"
	then
		# (MRL) 9-Jan-82 First check to see if $A is a an option
		ANY_OPT=`expr "x$CompileList" : 'x-'`
 		# make sure there are some .c, .s, or .i files in CompileList
 		ANY_CS=`expr "$CompileList" : '.*\(\.[CcSsi]\).*'`
		# (MRL) 9-Jan-82 Then only compile if there is a filename match,
		#    and it is isn't an option
 		if test "$ANY_CS" -a "$ANY_OPT" = 0
 		then
			echo "$ccC $P $NLO $CompileList" 1>&2
   			# contortions to cope with bug handling path in cc
			#compile, no load
   			(cd $TEMPDIR; $ccC $P $NLO $CompileList)
 			EE=$?
   			if test $EE = 0
   			then
 				# move products back into current directory
   				mv $TEMPDIR/*$NOLOAD . 2> /dev/null

   			fi
 		fi
	else
		echo "$ccC $P $LLIBPATH $OF $CompileList $LIBRARY" 1>&2
 		
		# run cc on the cfront-generated files.
		# first have to remove .o's from the argument list
		# passed to cc, since some cc's don't like .o's with -c
		cfiles=
		xxx=
 		for x in $CompileList
 		do
 			case $x in
 			*${ISUF})	cfiles="$cfiles $x"
 				;;
			*.o)	;;
			*.a)	;;
			*)	xxx="$xxx $x"
				;;
 			esac
 		done
 		if [ "$cfiles" != "" ]
 		then
			#echo "$ccC $P -c $xxx $cfiles in $TEMPDIR" 1>&2
 	  		(cd $TEMPDIR; $ccC $P -c $xxx $cfiles)  # compile, no load
 			EE=$?	# don't link if there was an error
 		else
 			EE=0	# okay to link
 		fi
 		
		if test $EE = 0
		then    # load
			#echo "$ccC $P $LLIBPATH $Linklist $LIBRARY"
			# PT link-time actions
			PTEE=0
			grep "@de[cf]" $ptr/$ptdmap >/dev/null 2>&1
			x=$?
			if [ $x = 0 -o $ptrflag = 1 ]
			then
				ptincl=$TEMPDIR/ptincl
				ptin=$TEMPDIR/ptin
				ptout=$TEMPDIR/ptout
				ptccopt=$TEMPDIR/ptccopt
				ptrlistx=$TEMPDIR/ptrlistx
				> $ptincl
				> $ptin
				> $ptout
				> $ptccopt
				> $ptrlistx

				# get include list
				for pti in $CPPList $ptdef $CPLUS $cPLUS $SYS $IDIRS
				do
					echo $pti
				done > $ptincl

				# build list of input files and options
				skipflag=0
				for pti in $P $Linklist $OF $LIBRARY
				do
					if [ $skipflag -eq 1 ]
					then
						skipflag=0
						continue
					fi
					x=`echo $pti | cut -c1-1`
					if [ "$x" != "-" -a "$x" != "+" ]
					then
						echo $pti
					elif [ "$pti" = "-o" -o "$pti" = "-I" -o "$pti" = "-D" -o "$pti" = "-U" ]
					then
						skipflag=1
					else
						x=`echo $pti | cut -c1-2`
						xx=`echo $pti | cut -c3-99`
						if [ "$x" = "-l" -a -f $CCLIBDIR/lib${xx}.a ]
						then
							echo $CCLIBDIR/lib${xx}.a
						elif [ "$x" != "-I" -a "$x" != "-D" -a "$x" != "-U" ]
						then
							echo $pti 1>&2
						fi
					fi
				done >$ptin 2>$ptccopt
				echo $AON >>$ptccopt

				# deal with standard library
				if [ "$LIBRARY" = "-l${LIB_ID}" ]
				then
					grep "$CCLIBDIR/libC.a" $ptin >/dev/null 2>&1
					if [ $? -ne 0 ]
					then
						echo $CCLIBDIR/libC.a >> $ptin
					fi
				else
					echo $LIBRARY >> $ptin
				fi

				# deal with +i
				pi=
				if [ "$PLUSI" -eq 1 ]
				then
					pi="-pti"
				fi
	
				# make list of repositories
				for pti in $ptrlist
				do
					echo $pti
				done >$ptrlistx

				# and we're off to the races
				PATH=$PTPATH $ptlinkC $PTOPTS $pi $ptflags $ptflags2 $ptr $TEMPDIR $0 $ptincl $ptin $ptout $ptdmap $ptccopt $ptrlistx "$cppC"
				PTEE=$?

				# add in any instantiated objects
				Linklist="`cat $ptout` $Linklist"
			fi

			if [ $PTEE -eq 0 ]
			# end PT
			then
				DO_LINK1=1
				DO_LINK2=1
				DO_NM=1

				if test "$DASHR" = 1
				then
					DO_LINK1=1
					DO_LINK2=0
					DO_NM=0
				fi

				if test $DO_LINK1 = 1 -a $EE = 0
				then
					$ccC $P $LLIBPATH $Linklist $OF $LIBRARY >$TEMPDIR/__err 2>&1
					EE=$?
					(test $DEMANGLE = 1 &&
				 	$cplusfiltC <$TEMPDIR/__err 2>/dev/null |
				 	sed "s/^[ 	]*\(..*\)::__vtbl.*$/   Virtual table for class '\1': first non-inline virtual function in '\1' is not defined./") ||
					cat $TEMPDIR/__err
					if test $EE = 0
					then
						chmod 644 $FINALNAME	    # Make it non-executable until after the final link is done
					fi
				fi
			else
				EE=$PTEE
			fi

			if test $EE = 0  -a "$DO_NM" = 1
			then
                                case $PM_FLAG in
                                PATCH)
                                        $patchC $OO
                                        EE=$?
                                        if test $EE != 0
                                        then
                                        echo "CC: error: patch failed in handling static constructors or destructors" 1>&2
                                        fi
                                        ;;
                                *)
					CTDT=__ctdt$$
					TCTDT=${TEMPDIR}/${CTDT}
					$NM $NMFLAGS $FINALNAME | $munchC > ${TCTDT}.c
					if test -s ${TCTDT}.c
					then
						$ccC -c ${TCTDT}.c && \
							$ccC $P $LLIBPATH ${CTDT}.o $Linklist $OF $LIBRARY
						EE=$?
						if test $EE != 0
						then
						echo "CC: error: munch failed in handling static constructors or destructors" 1>&2
						fi
						rm ${CTDT}.o
					
					fi
					;;
				esac
				if test $EE != 0
				then
					chmod 644 $FINALNAME       #Make it non-executable in case the module produced was bad.
				else
					chmod 755 $FINALNAME
				fi

			fi

			if test "$STRIP" -a "$DASHR" != 1 -a $EE = 0
			then
				strip $OO
			fi
			
			COUNT=0 
			for A in $CompileList
			do 
			   COUNT="`expr $COUNT + 1`"
			done
			if test $COUNT -gt 1
			then
			   mv $TEMPDIR/*.o . 2> /dev/null
			fi
		fi
	fi
esac

if test "$R"
then
   if test "$PLUSI"
   then
 	for A in $MVLIST
 	do
 		mv $TEMPDIR/$A${ISUF} $A.${ISUF} 2> /dev/null
 	done
   fi
fi

rm -fr $TEMPDIR

if test $AFAIL -ne 0
then
	exit $AFAIL
fi

case $E in
0)	exit $EE
	;;
*)	exit $E
esac

