#--------------------------------------------------------------------
# The symbol SWITCHES is used to active any of the following switches
# to enable custom versions of the simulation program.
STD_OPTIONS = -DCERNLIB_TYPE 
CASCADE_OPTIONS = -DUSE_UPWGHT_AS_REPEAT_COUNT -DTUNL_BACKSTREAMING_CASCADE_FACTOR=1000 -DEDHS_BACKSTREAMING_CASCADE_FACTOR=1
# Enable the following line to turn off writing out hddm data 
#IO_OPTIONS = -DDISABLE_OUTPUT
#--------------------------------------------------------------------

SWITCHES = $(STD_OPTIONS) $(IO_OPTIONS) $(NTUPLE_OPTIONS) $(CASCADE_OPTIONS)

OStype = $(shell uname)
ARCHtype = $(shell uname -m)
BINDIR = ../bin.$(OStype)

ifeq ($(OStype),Linux)
	ifeq ($(ARCHtype),alpha)
		CC	:= gcc
		CPP	:= g++
		F77	:= g77
		NETLIB := -lnsl
		XLIBS  := -L/usr/X11R6/lib -lXpm -lSM -lXm -lXt -lICE -lXext -lX11 -lXp -ldl
		COPTS = -g $(SWITCHES)
		FOPTS = -g -Wno-globals $(SWITCHES)
		GLIBS 	:= -L/usr/lib/gcc-lib/alpha-redhat-linux/egcs-2.91.66/ -lg2c 
	else
		CC	:= gcc
		CPP	:= g++
		F77	:= g77
		NETLIB := -lnsl
		XLIBS  := -L/usr/X11R6/lib -lXpm -lSM -lXm -lXt -lICE -lXext -lX11 -lXp -ldl
		COPTS = -g $(SWITCHES) -I$(HALLD_HOME)/src/include
		FOPTS = -g -Wno-globals $(SWITCHES)
		GLIBS	:= -lXmu -lSM -lICE -lpthread
		EXPORTDYNAMIC = -Wl,-export-dynamic
		STATIC = -Wl,-Bstatic
	endif
endif
ifeq ($(OStype),OSF1)
	CC	:= cc
	CPP	:= g++
	F77	:= f77
	NETLIB := 
        EXPORTDYNAMIC :=
	STATIC	:=
	XLIBS  := -L/usr/lib -lXm -lSM -lICE -lXt -lX11 -lm -lPW -ldnet_stub
	COPTS = -g -D_Tru64 $(SWITCHES)
	FOPTS = -g -fpe4 $(SWITCHES)
	LOPTS = -g -non_shared -fpe4
	GLIBS 	:= -L/r5d1/applications/gcc3/lib/gcc-lib/alphaev5-dec-osf4.0f/ -lg2c -L/r5d1/applications/gcc3/lib/gcc-lib/alphaev5-dec-osf4.0f/3.3/ -lgcc
endif
ifeq ($(OStype),SunOS)
	CC	:= cc
	CPP	:= CC
	F77	:= f77
	NETLIB	:= 
	EXPORTDYNAMIC :=
	STATIC	:=
	XLIBS	:= -L/usr/lib -lXm -lSM -lICE -lXt -lX11 -lm 
	COPTS	:= -g $(SWITCHES)
	FOPTS	:= -g $(SWITCHES)
	LOPTS	:= -g
	GLIBS	:=
endif
ifeq ($(OStype),Darwin)
	CC	:= gcc
	CPP	:= g++
	F77	:= g77
	NETLIB 	:=
	XLIBS  	:= -L/usr/X11R6/lib -lXpm -lSM -lXm -lXt -lICE -lXext -lX11 -lXp -ldl
	COPTS = -g $(SWITCHES)
	FOPTS = -g -Wno-globals $(SWITCHES)
	GLIBS	:= -L$(CERN_ROOT)/lib -lcompat /usr/lib/libgcc.a
endif

HDDS = ../hdds
HDDM = ../hddm

ifeq ($(OStype),OSF1)
 	MISC_FIXES := vunit.o gsrotm.o fint.o gthion.o gltrac.o gsstak.o
else
	MISC_FIXES := gltrac.o gsstak.o
endif

AUXF = guhadr.o gukine.o guout.o guphad.o gustep.o guxcs.o \
       gelhad/gtgama.o gelhad/caspim.o gelhad/caspip.o gpairg.o \
       hdds/hddsGeant3.F goptimize.o cobrems.o
AUXC = hddmInput.o hddmOutput.o hddm_s.o \
       bintree.o memcheck.o trapfpe.o

GXFIXED = gxcs.o gxphys.o

CERNLIB = $(CERN_ROOT)/bin/cernlib
GELHAD = gelhad/libgelhad.a

default: hdds gxtwist++

hdds:	force
	make -C hdds
force: ;

gxtwist++: gxint.o uginit.o uglast.o $(AUXF) $(AUXC) $(GXFIXED) \
           $(MISC_FIXES)
	$(F77) -g -o $@ gxint.o uginit.o uglast.o -I$(CERN_ROOT)/include \
	   $(AUXF) $(AUXC) $(GXFIXED) $(MISC_FIXES) \
           -Lgelhad -lgelhad \
	   `cernlib -v $(CERN_LEVEL) geant321 pawlib graflib/Motif packlib mathlib` \
	   $(EXPORTDYNAMIC) $(GLIBS)

gxtwist: gxtwist.o uginit.o uglast.o $(AUXF) $(AUXC) $(MISC_FIXES)
	$(F77) -g $(LOPTS) -o $@ -I$(CERN_ROOT)/include $(STATIC) \
	   gxtwist.o uginit.o uglast.o $(AUXF) $(AUXC) $(MISC_FIXES) \
	   -Lgelhad -lgelhad \
	  `$(CERNLIB) geant321 mathlib graflib grafX11` \
	   $(NETLIB) $(XLIBS) $(GLIBS)

install: gxtwist gxtwist++
	cp $^ $(HALLD_HOME)/bin/$(OStype)

.F.o:
	$(F77) $(FOPTS) -I. -I$(CERN_ROOT)/include -DCERNLIB_MOTIF -c -o $@ $<

.f.o:
	$(F77) $(FOPTS) -I. -I$(CERN_ROOT)/include -c -o $@ $<

.c.o:
	$(CC) $(COPTS) -I. -I../include -I$(CERN_ROOT)/include -c -o $@ $<

$(AUXC): hddm_s.h bintree.h geant3.h hddmOutput.h 

.cpp.o:
	$(CPP) $(COPTS) -I. -I../include -c -o $@ $<

hddm_s.h:
	$(HDDM)/hddm-c $(HDDM)/event.xml

hddm_s.c:
	$(HDDM)/hddm-c $(HDDM)/event.xml

hddm_s.o: 	hddm_s.c
	$(CC) $(COPTS) -I. -I../include -c -o $@ $<

cdcdump: cdcdump.c hddm_s.o
	$(CC) $(COPTS) -I. -I../include -o $@ $^ 

bcal2nt: bcal2nt.c hddm_s.o
	$(CC) $(COPTS) -I. -I../include -o $@ $^ \
	  -L$(CERN_ROOT)/lib -lpacklib -lkernlib -lg2c

$(GELHAD):
	$(MAKE) -C gelhad

clean:
	rm -f *.o core last.kumac* paw.metafile