default: compile @PWD@/bin/pargap.sh @GAPPATH@/bin/@GAPARCH@
	echo "DONE"
	@ if [ ! -d @GAPPATH@/bin/@GAPARCH@ ]; then \
	  echo ""; \
	  echo "WARNING:  Directory, @GAPPATH@/bin/@GAPARCH@, not found."; \
	  echo "Please check dirs in:  @PWD@/bin/pargap.sh."; \
	fi;

@PWD@/bin/pargap.sh: @GAPPATH@/bin/gap.sh
	( cd @GAPPATH@/bin/@GAPARCH@; \
	  rm -f pargapmpi; ln -s gap pargapmpi )
	( cd @GAPPATH@/bin; rm -f pargap.sh; \
	  chmod a+x gap.sh; \
	  echo "#!/bin/sh" > pargap.sh; \
	  echo "# THIS FILE GENERATED AUTOMATICALLY BY:" >> pargap.sh; \
	  echo "#       @PWD@/Makefile" >> pargap.sh; \
	  echo "#" >> pargap.sh; \
	  sed -e '/GAP_PRG=.*gap$$/ s#/gap$$#/pargapmpi#g' gap.sh >> \
	        pargap.sh; \
	  chmod a+x pargap.sh; \
	  rm -f @PWD@/bin/pargap.sh; \
	  mv pargap.sh @PWD@/bin/; \
	)

#HACK:  @PWD@/ needed when making gapmpi.o below.  When that goes away,
#  @PWD@/ can go away.
# Also, copying gapmpi.c into @GAPPATH@/src/ for backward compatibility
#   with an old Makegap.in
@GAPPATH@/src/gapmpi.c @GAPPATH@/src/gapmpi.h: \
	    @PWD@/src/gapmpi.c @PWD@/src/gapmpi.h
	cp @PWD@/src/gapmpi.c @PWD@/src/gapmpi.h @GAPPATH@/src/

# @GAPPATH@/lib/masslave.g @GAPPATH@/lib/slavelist.g: \
#		lib/masslave.g lib/slavelist.g
#	cp lib/masslave.g lib/slavelist.g @GAPPATH@/lib/

bin/@GAPARCH@/libmpi.a: mpinu/*.c
	( cd mpinu; make clean; make libmpi.a )
	if [ ! -d bin/@GAPARCH@ ];  then mkdir bin/@GAPARCH@;  fi
	mv mpinu/libmpi.a bin/@GAPARCH@/libmpi.a

compile: bin/@GAPARCH@/libmpi.a @GAPPATH@/src/gapmpi.c \
	  @GAPPATH@/src/gapmpi.h @PWD@/lib/masslave.g @PWD@/lib/slavelist.g
	( cd @GAPPATH@/bin/@GAPARCH@ ; \
	  rm -f gap.o; \
	  $(MAKE) \
	    COPTS="$(COPTS) -DGAPMPI -DPARGAPMPI -DPARGAP" \
	    MPILIBS="@PWD@/bin/@GAPARCH@/libmpi.a @SOCKETLIBS@" \
	    MPIINCLUDEDIR=@PWD@/mpinu/. \
	    GAPMPI_OBJ=gapmpi.o ; \
	    rm -f gap.o ; \
	)
	# If successful:
	if [ ! -f @PWD@/../ALLPKG ] ; then \
	  echo "" > @PWD@/../ALLPKG; \
	fi
	if ( grep '^pargap$$' @PWD@/../ALLPKG > /dev/null ); then echo ""; \
	else echo "pargap" >> @PWD@/../ALLPKG; \
	fi

clean:
	rm -f @GAPPATH@/bin/@GAPARCH@/pargap
	rm -f @GAPPATH@/bin/@GAPARCH@/pargapmpi
	rm -f @GAPPATH@/bin/@GAPARCH@/gapmpi
	rm -f bin/pargapmpi.sh
	rm -f bin/pargap.sh
	rm -f bin/gapmpi.sh
	rm -f bin/@GAPARCH@/libmpi.a
	if [ -d bin/@GAPARCH@ ];  then rmdir bin/@GAPARCH@;  fi
	for file in bin/* ; do \
	  if [ -d $$file ]; then rm -ir $$file; fi; \
        done
	( cd mpinu; make clean )

distclean: clean
	rm -f bin/procgroup
	rm -f Makefile

dist: distclean
	( cd @PWD@/..; \
	  tar cvf pargap-new.tar ./pargap; \
	  gzip pargap-new.tar )
