# MSDEV=C:/msdev
MSDEV="C:/Program Files/Microsoft Platform SDK"
# MSVC="/usr/Program Files/Microsoft Visual Studio"
MSVC="C:/Program Files/Microsoft Visual Studio"
#BINMODEDIR="D:/Program Files/Microsoft Visual Studio/VC98/LIB"
BINMODEDIR="C:\Program Files\Microsoft Visual Studio\VC98\Lib"
stdout=/tmp/stdo$$
stderr=/tmp/stde$$
args=
libs=
incpath="$INCPATH"
defs="/Dintelnt /DWIN32 /D_VISUALC_"
#defs="/Dintelnt /X /DWIN32"
undefs=
cfiles=
mounts=
echo=0
tmp="$MSDEV\Lib"
# deflibs="/LIBPATH:\"`cygpath -a --windows \"$tmp\"`\""
deflibs="$deflibs $LIBPATH kernel32.lib"
deflibs="$deflibs user32.lib gdi32.lib winspool.lib"
deflibs="$deflibs comdlg32.lib advapi32.lib shell32.lib"
deflibs="$deflibs uuid.lib wsock32.lib opengl32.lib"
deflibs="$deflibs -NODEFAULTLIB:LIBCMT -NODEFAULTLIB:LIBC"
link=1
compile=1
mkdep=0
map=
oset=0
oname=
dllflag="-MD"
dlllib="msvcrt.lib msvcirt.lib"
binmode=
dfile=

cleanup() { 

	for i in $delfiles foo ; do
		if test -f $i ; then
			rm -f $i
		fi
	done

	for i in $cfiles foo ; do
		if test ! "$i" = foo ; then
			file=`echo $i | sed -e "s?\.cpp??"`
			if test -f $file.obj ; then
				mv $file.obj $file.o
			fi
                        file1=`echo $file | sed "s/.*\///"`
                        if test -f $file1.obj ; then
                                mv $file1.obj $file1.o
                        fi
		fi
	done

#	if test -f $oname.exe ; then
#		mv $oname.exe foo
#		mv foo $oname
#	fi
	if test -f $stdout ; then
		rm $stdout
	fi
	if test -f $stderr ; then
		rm $stderr
	fi
	if test -f foo$$ ; then
		rm foo$$
	fi
	if test -f bar$$ ; then
		rm bar$$
	fi
	if test -f foobar$$ ; then
		rm foobar$$
	fi
}

trap 'cleanup; exit 1' 1 2 15

for i in $* ; do
	case $i in 
                -staticRunTime)
                         dlllib="libcmt.lib"
                         dllflag="-MT" ;;
                -dllRunTime)
                         dlllib="msvcrt.lib"
                         dllflag="-MD" ;;
                -o)      # args="$args $i"
                         oset=1 ;;
		-map)	 map="/MAP" ;;
		-M)	 compile=0
			 mkdep=1 ;;
		-g)      args="$args /Z7" ;;
		-O2)	 args="$args /O2" ;;
		-mount*) mounts="$mounts $i" ;;
		-D*)     defs="$defs `echo $i | sed 's?-D?/D?'`" ;; 
		-U*)     undefs="$undefs `echo $i | sed 's?-U?/U?'`" ;;
                -I*)     tmp=`echo $i | sed "s/-I//"`
                         tmp2=`cygpath -a --windows "$tmp"`
                         incpath="$incpath /I\"$tmp2\"";;
                -L*)     tmp=`echo $i | sed "s/-L//"`
                         tmp2=`cygpath -a --windows "$tmp"`
                         libpath="$libpath /LIBPATH:\"$tmp2\"" ;;
		-l*)     libs="$libs `echo $i | sed 's?-l??'`.lib" ;;
		-echo)   echo=1 ;;
		-c)      link=0
			 args="$args $i" ;;
		-E)	 link=0
			 args="$args /E" ;;
		-B)	 LIB="$LIB;$BINMODEDIR"
			 binmode="BINMODE.OBJ" ;;
		*.cpp)	 
			 cfiles="$cfiles $i" ;;
                *.C)     ofname=$i
			 fname=`echo $i | sed "s?\.C?.cpp?"`
                         cp $i $fname
                         delfiles="$delfiles $fname"
			 cfiles="$cfiles $fname" ;;
                *.cc)    fname=`echo $i | sed "s?\.cc?.cpp?"`
                         cp $i $fname
                         delfiles="$delfiles $fname"
			 cfiles="$cfiles $fname" ;;
		-Wp*)	 dfile=`echo $i | sed "s/^.*\(.deps*\)/ \1/"`
			 mkdep=1;;
                *)       if test $oset = 1 ; then
                             oname=$i
                             args="$args /Fe$i"
                             oset=0
			 else
			     args="$args $i"
                         fi ;;
	esac
done


tmp=`cygpath -a --windows "$MSDEV/Include"`
msincs="/I\"$tmp\""
incpath="$incpath $msincs"
#incpath=`echo $incpath | sed 's/\\\\/\\\\\\\\/g'`
#libpath=`echo $libpath | sed 's/\\\\/\\\\\\\\/g'`

if test $mkdep = 1; then
    if test -f foo$$ ; then
	rm foo$$
    fi
    touch foo$$

    MSVC_INCLUDE=\"$MSVC/Vc98/Include\"
    MSVC_COMMON_INCLUDE=\"$MSVC/Common/Include\"
    cmd="makedepend -ffoo$$ -Dintelnt -D_WIN32 -D_M_IX86=500 -Y -I$MSVC_INCLUDE -I$MSVC_COMMON_INCLUDE $fname"
    if test $echo = 1 ; then
	echo $cmd
    fi
    eval $cmd 2>/dev/null
    sed -e "/Microsoft/d"  \
	-e "/DO NOT DELETE/d"  \
	-e "/^$/d" -e "s/^.*://"  \
		< foo$$ > bar$$
    if test -f foo$$ ; then
	rm foo$$
    fi
    if test `cat bar$$ | wc -l` -eq 0 ; then
	echo `echo $fname | sed "s/\.cpp/.o/"`: $ofname > foobar$$
    else
	echo `echo $fname | sed "s/\.cpp/.o/"`: $ofname "\\" > foobar$$
	sed  -e '1,$s/$/ \\/' -e '$s/\\/ /' < bar$$ >> foobar$$
    fi
    if test "$dfile" != ""
    then
	mv foobar$$ $dfile
    else
	cat foobar$$
    fi
    err=0
fi

if test $compile = 1
then

    if test $link = 1 ; then
	cmd="cl  $dllflag -nologo $incpath $defs $undefs $args $cfiles /link $libpath $deflibs $libs $dlllib $map $binmode" 
	# cmd="link -nologo $args $cfiles $libpath $deflibs $libs $dlllib $map $binmode" 
    else
	cmd="cl  $dllflag -nologo $incpath $defs $undefs $args $cfiles"
    fi

    for i in $mounts foo ; do
	if test ! "$i" = foo ; then
	    src=`echo $i | sed -e "s/-mount://" | sed -e "s/=.*//"`
	    dst=`echo $i | sed -e "s/-mount.*=//"`
	    cmd=`echo $cmd | sed "s?$src?$dst?g"`
	fi
    done

    if test $echo = 1 ; then
	echo $cmd
    fi

    if eval $cmd 1> $stdout 2> $stderr ; then
        s=`cat $stdout $stderr | grep "error C[0-9].*:"`
        if test "$s" = "" ; then
	    err=0;
        else
	    err=1;
        fi
    else
        err=1
    fi

    if test $link = 1 ; then
	cat $stdout | grep -v "object file assumed" >&1
	cat $stderr | grep -v "object file assumed" >&2
    else
	cat $stdout | sed "1d" >&1
	cat $stderr | sed "1d" >&2
    fi
fi

cleanup

exit $err
