#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
# make World -	make the whole world
# make system - make libraries and binaries
# make clean -  clean away objects and junk files
# make depend - make include file dependencies
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

IMAKECMD = imake
IMAKESRC = localbin
IMAKEREF = conf
#
# directories which contain Makefiles which are built from 'imake'
#
IMAKEDIRS = 	arc chase demo.rccl demo.rci doc drivers fastmath \
		lib libCtree localbin locallib programs rccl \
		rci robotTypes robots \
		puma elbow stan scara rtlinux\
		toolbox util robotsim pdb speedtest jls
# MS removed lsi11 and man, added jls
#ifdef uVaxConfig
AUXK_DIR = 	auxkernel
#else
AUXK_DIR =
#endif

VXWLIBDIRS =	chase drivers fastmath libCtree locallib rccl rci \
		robotTypes robots util puma elbow stan scara
VXWBINDIRS =    drivers programs

RCCLIBDIRS =	chase drivers fastmath rccl robotTypes robots util rci pdb \
		puma elbow stan scara
RCCBINDIRS =	arc programs robotsim drivers robots jls rtlinux

#ifdef AixArch
SUPPORT =	bin/xargs bin/packdeps bin/makedepend
#else
SUPPORT =	bin/xargs bin/packdeps
#endif

World:
		@echo ""
		@echo "Building `cat VERSION`"
		@echo ""
		@echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
		@echo "* Be sure you have made any necessary changes to the *"
		@echo "* site configuration file conf/site.conf.            *"
		@echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
		@echo ""
		@echo ""
# MS Remove the rebuild of imake for Linux machines
#ifndef LinuxConfig
		rm -f bin/cc
		cd $(IMAKESRC); $(MAKE) -f Makefile.imake clean; \
		   $(MAKE) -f Makefile.imake ../bin/imake \
		   BOOTSTRAPCFLAGS="$(BOOTSTRAPCFLAGS)"
#endif
		mv Makefile Makefile.bak
		$(MAKE) $(MFLAGS) -f Makefile.bak Makefile
		$(MAKE) $(MFLAGS) Makefiles
		$(MAKE) $(MFLAGS) rciConfig
		$(MAKE) $(MFLAGS) clean
		$(RMCMD) */DependFile /**/
		$(MAKE) $(MFLAGS) depend
		$(MAKE) $(MFLAGS) xcompiledir
		$(MAKE) $(MFLAGS) system
		@echo "make World completed"

P = $$RCCL/lib/		/* prefix for libs in .rcc conf file */

#if UseFastMath
#define FastMathFlag 1
#else
#define FastMathFlag 0
#endif

#if VxwFastMath
#define VxwFastMathFlag 1
#else
#define VxwFastMathFlag 0
#endif

rciConfig:	bin/makeRciConfig
		bin/makeRciConfig h/rciConfig.h rci/rciConfig.c
		$(RMCMD) sys
		ln -s UnixSystemDir sys
	/* build the default .rcc file */
	$(RMCMD) conf/.rcc
	echo 'cc = ' CcCmd >> conf/.rcc
	echo 'begin = $Pbegin.o' >> conf/.rcc
	echo 'end = $Pend.o' >> conf/.rcc
	echo 'ctrl = $PrciCtrl.a' >> conf/.rcc
#ifdef uVaxConfig
	echo 'util = $PrciUser.a $PrciCtrl.a $PlibAux.a \
		     $PlibC.a -lm' >> conf/.rcc
#else
	echo 'util = $PrciUser.a $PrciCtrl.a $PlibC.a -lm' >> conf/.rcc
#endif
	echo 'user = $PrciUser.a $PlibCtree.a' >> conf/.rcc
	echo 'include = $$RCCL/h' >> conf/.rcc
	$(RMCMD) conf/defaultRobot
	echo DefaultRobotName > conf/defaultRobot
	$(RMCMD) conf/$(EMPTY)*.JLS
	( echo '/*** DO NOT DELETE/,/*** AND DO NOT DELETE/c' ; \
	echo '/* *** DO NOT DELETE THIS LINE: system Makefile uses it */' ; \
	echo '#if VXWORKS_CFG' ; \
	echo '#define FASTMATH_ENABLED' VxwFastMathFlag ; \
	echo '#else' ; \
	echo '#define FASTMATH_ENABLED' FastMathFlag ; \
	echo '#endif' ; \
	echo '/* *** AND DO NOT DELETE THIS LINE EITHER */' ; \
	  echo . ; echo w ; echo q ) | ed - h/fastmath.h

xcompiledir:
#if BuildMoperCode
		-rm -f Xcompile
		@if [ \! -f Xcompile -a \! -d Xcompile ]; then \
		    if [ -d XcompileDir ]; then \
			ln -s XcompileDir Xcompile; \
			echo ln -s XcompileDir Xcompile; \
		    else \
			echo "*** ERROR: No Xcompiler on this system ***"; \
			echo "Set 'BuildMoperCode' to NO in site.conf"; \
		    fi; \
		else \
		    echo "*** Assuming Xcompile is set correctly"; \
		fi
#else
		echo "Not building moper code, so no Xcompiler needed"
#endif

system:
		cd libCtree; $(MAKE) $(MFLAGS) libs
		cd locallib; $(MAKE) $(MFLAGS) libs
		cd localbin; $(MAKE) $(MFLAGS) install
#ifdef uVaxConfig		
		cd auxkernel; $(MAKE) $(MFLAGS) install
#endif
		$(MAKE) $(MFLAGS) libs
		$(MAKE) $(MFLAGS) bins
#if VxWorksSupport
		/* Build VxWorks support, if necessary */
		$(MAKE) $(MFLAGS) vxw
#endif
# MS Comment		$(MAKE) $(MFLAGS) manpages
#if BuildMoperCode
		cd lsi11; $(MAKE) $(MFLAGS) install
#endif

depend::	$(SUPPORT)
		@for i in $(IMAKEDIRS) $(AUXK_DIR) ; do\
		  (cd $$i ; echo making depends in $$i -- ; \
		   $(MAKE) $(MFLAGS) depend) ; \
		done ;

libs:
		@(cd lib; echo lib -- ; $(MAKE) $(MFLAGS) libs); \
		for i in $(RCCLIBDIRS) ; do\
		  (cd $$i ; echo $$i -- ; $(MAKE) $(MFLAGS) libs) ; \
		done
bins:		
		@for i in $(RCCBINDIRS) ; do\
		  (cd $$i ; echo $$i -- ; $(MAKE) $(MFLAGS) install) ; \
		done

vxlibs:
		@(cd lib; echo lib -- ; $(MAKE) $(MFLAGS) VXW); \
		for i in $(VXWLIBDIRS) ; do\
		  (cd $$i ; echo $$i -- ; $(MAKE) $(MFLAGS) vxlibs) ; \
		done
vxbins:		
		@for i in $(VXWBINDIRS) ; do\
		  (cd $$i ; echo $$i -- ; $(MAKE) $(MFLAGS) vxbins) ; \
		done

vxw:		vxlibs vxbins
		cd lib; $(MAKE) $(MFLAGS) vxw

manpages:
		cd man; $(MAKE) $(MFLAGS) man;

clean::		
		-@find bin ! \( -name README -o -name bin -o -name packdeps \
			     -o -name imake -o -name rmkmf \) \
		   -exec rm -f {} \;
		@for i in $(IMAKEDIRS) $(AUXK_DIR) gnuMake ; do\
		  (cd $$i ; echo cleaning $$i -- ; \
		   $(MAKE) $(MFLAGS) clean ) ; \
		done ;
		$(RMCMD) lock/$(EMPTY)*	
		(cd conf; $(RMCMD) *.JLS $(JUNKFILES) )
		$(RMCMD) $(JUNKFILES)
#if !VxWorksSupport
		$(RMCMD) -r */VXW
#endif

bin/xargs:	localbin/xargs.c
		cd localbin; $(MAKE) $(MFLAGS) ../bin/xargs

bin/packdeps:	localbin/packdeps
		cp localbin/packdeps bin

#ifdef AixArch
bin/makedepend:	localbin/makedepend
		cp localbin/makedepend bin
#endif

bin/makeRciConfig: localbin/makeRciConfig.c
		cd localbin; $(MAKE) $(MFLAGS) ../bin/makeRciConfig

Makefile:
		@if [ -f Makefile ]; then \
			echo "mv Makefile Makefile.BAK"; \
			mv Makefile Makefile.BAK; \
		else exit 0; fi		
		$(IMAKECMD) -I$(IMAKEREF) -DRCCLRootDir=`pwd`

Makefiles:	bin/rmkmf
		RCCL=`pwd`; \
		export RCCL; \
		for i in $(IMAKEDIRS) $(AUXK_DIR) ; do \
		  (cd $$i ; echo creating Makefile in $$i -- ; ../bin/rmkmf);\
		done
bin/rmkmf:	localbin/rmkmf
		cp localbin/rmkmf bin/rmkmf
		chmod 755 localbin/rmkmf

makeClean()
