############################################################
# Copyright (C) 2001-2009, CompHEP Collaboration            
# Author: A. Sherstnev                                      
############################################################

.PHONY: all link clean man
CHEP=@CHEP@
INS:=-I$(CHEP)/include
CC=    $(shell cat $(CHEP)/CC)
CFLAGS=$(shell cat $(CHEP)/CFLAGS)
CLIBS= $(shell cat $(CHEP)/CLIBS)
RANLIB= $(shell cat $(CHEP)/RANLIB)
FORLIBS= $(shell cat $(CHEP)/FORLIBS)

F_TARGET=$(shell cat ffile_0)
F_OBJ=$(subst .c,.o,$(F_TARGET))

D_TARGET=$(shell ls d*.c)
D_OBJ=$(subst .c,.o,$(D_TARGET))

cobjects=$(F_OBJ) $(D_OBJ) sqme.o service.o

echo F_TARGET=${F_TARGET}

all: sqme.o service.o makemodeltmp link
	@echo "Numerical CompHEP has been created!"

man: man_ffiles man_d_compile sqme.o service.o makemodeltmp link
	@echo "Numerical CompHEP has been created!"

link:
	@$(RANLIB) f_0.a
	@make -C ../usr
	$(CC) $(CFLAGS) -o n_comphep.exe $(CHEP)/src/num/n_comphep.o sqme.o service.o \
	../usr/userFun.o -lnum -levents -lpdf -lsymb -lserv f_*.a -lmssm -lexternal $(CLIBS) -L$(CHEP)/lib $(FORLIBS)

clean:
	@mv  Makefile  .Makefile
	@mv  n_comphep .n_comphep
	@mv  diag_view .diag_view
	@rm -fr *
	@mv .Makefile  Makefile
	@mv .n_comphep n_comphep
	@mv .diag_view diag_view

#############################################################################################################
.PHONY: makemodeltmp f_compile d_compile man_d_compile man_ffiles man_f_compile

makemodeltmp:
	@if (test -d tmp); then \
	  rm -r tmp; \
	fi
	@if (test -d models); then \
	  rm -r models; \
	fi
	@cp -rf ../models .
	@cp -rf ../tmp .

f_compile:
	@tar r --remove-files -f diags.tar $(F_TARGET)
	@$(AR) -r f_0.a $(F_OBJ)
	@rm -f $(F_OBJ)

d_compile:
	@tar r --remove-files -f diags.tar $(D_TARGET)
	@$(AR) -r f_0.a $(D_OBJ)
	@rm -f $(D_OBJ)

man_d_compile: $(D_OBJ)
	@tar r --remove-files -f diags.tar $(D_TARGET)
	@$(AR) -r f_0.a $(D_OBJ)
	@rm -f $(D_OBJ)

man_ffiles:
	@make man_f_compile
	@rm -f ffile_0
	@for x in `ls ffile_*`; do \
		mv -f $$x ffile_0; \
		make man_f_compile; \
	done
	@if [ -x diags.tar ]; then gzip diags.tar; fi
	rm -f ffile_*

man_f_compile: $(F_OBJ)
	@echo $(F_OBJ)
	@tar r --remove-files -f diags.tar $(F_TARGET)
	@ar -r f_0.a $(F_OBJ)
	@rm -f $(F_OBJ)
	@ranlib f_0.a

#############################################################################################################
$(cobjects): %.o: %.c
	$(CC) -c $(CFLAGS) $(INS) $< -o $@
