####################################################
# Copyright (C) 2001-2003, CompHEP COllaboration
# Author: Sherstnev Alexandre, A.Kryukov
# Makefile, v1.5 11/01/2003                        
#---------------------------------------------------
# $Id: Makefile,v 1.61 2004/05/10 16:04:06 kryukov Exp $
# 
# $Log: Makefile,v $
# Revision 1.61  2004/05/10 16:04:06  kryukov
# Join with 4.4.2
#
# Revision 1.60  2004/05/10 12:47:50  kryukov
# This version is equal to 4.4.1
#
# Revision 1.59.2.1  2004/04/30 15:08:35  sherstnv
# Old changes, included detween v_4_4_0 and tarball (comphep-4.4.0.tgz)
# from the CompHEP homepage.
#
# Revision 1.58  2003/11/16 20:20:07  kryukov
# Add RELEASE-NOTES file to binary distributive
#
# Revision 1.57  2003/11/09 15:59:17  kryukov
# *** empty log message ***
#
# Revision 1.56  2003/11/09 15:57:19  kryukov
# Incompartablities with 4.2.1
#
# Revision 1.55  2003/10/29 16:26:25  sherstnv
# New version of CompHEP: 4.2.2
#
# Revision 1.54  2003/08/21 10:32:51  sherstnv
# Izmenena sustema nazvaniya bibliotek CompHEP
#
# Revision 1.53  2003/07/20 16:12:22  kryukov
# Remove copy num_batch_pbs.pl
#
# Revision 1.52  2003/07/20 14:32:43  kryukov
# include lib dir into binary distributive
#
# Revision 1.51  2003/07/11 21:14:34  kryukov
# remove mk_tab.exe from COMPHEP_BIN_FILES
#
# Revision 1.50  2003/07/11 20:42:45  kryukov
# Fix spelling
#
# Revision 1.49  2003/07/01 19:15:50  kryukov
# Cleanup lib's for distributive
#
# Revision 1.48  2003/07/01 19:02:04  kryukov
# Remoce CVS dirs from distributive
#
# Revision 1.47  2003/07/01 16:29:30  kryukov
# Change building of dist: add doc file
#
# Revision 1.46  2003/06/27 19:44:51  kryukov
# Missprint
#
# Revision 1.45  2003/06/27 13:04:38  sherstnv
# All libraries are transfered to the lib directroy
#
# Revision 1.44  2003/06/27 12:46:04  sherstnv
# Final and working version ov symb_batch.pl script.
# All necessary changes have been done in s_comphep.c and Makefiles
#
# Revision 1.43  2003/06/27 10:46:50  kryukov
# Change pattern in dist target to: cp -a ${PWD}/[^d]* ${COMPHEP_DISTR};
# It is not good but it works. We better pattern exclude doc directory
# and all CVS directory in distributive.
#
# Revision 1.42  2003/06/26 15:51:42  sherstnv
# symp_batch.pl script is customized to new CompHEP options/new
# n_comphep creation schime.
#
# Revision 1.41  2003/06/25 18:46:41  kryukov
# *** empty log message ***
#
# Revision 1.40  2003/06/25 16:54:12  kryukov
# Bug fix of dist target
#
# Revision 1.39  2003/06/25 16:32:47  kryukov
# Support documentation
#
# Revision 1.38  2003/06/24 12:50:41  kryukov
# Small bug fix concerning OSTYPE
#
# Revision 1.37  2003/06/24 12:07:52  kryukov
# Using pwd command instead of $PWD
#
# Revision 1.36  2003/06/19 07:39:52  kryukov
# Add ../symb/lbl.o to build tab_view
#
# Revision 1.35  2003/06/17 16:43:14  sherstnv
# mk_tab is linked by user in the $WDIR/usr directory.
#
# Revision 1.34  2003/06/15 16:53:22  kryukov
# Include "make usr"
#
# Revision 1.33  2003/06/15 15:48:37  kryukov
# Impove n_comphep compilation
#
# Revision 1.32  2003/05/01 12:16:26  sherstnv
# Sdelana standartnaya peredacha puti k CompHEP v programme
# tab_view.exe
# Sdelan script vizova tab_view iz user area.
#
# Revision 1.31  2003/04/21 20:58:44  kryukov
# A little bit improve output.
#
# Revision 1.30  2003/04/21 20:09:47  kryukov
# Misprints and English improve. Special thanks M.D.
#
# Revision 1.29  2003/04/19 18:51:33  kryukov
# Change in BINDIST_FILES mk_tab -> mk_tab.exe
#
# Revision 1.28  2003/04/18 16:34:10  sherstnv
# Proga mk_tab peredelana pod noviy format zapisi sobitiy.
# Teper' mk_tab mozhet rabotat' s files, v kotorih zapisani
# sobitiya ot neskol'kih podprocessov.
#
# Revision 1.27  2003/04/17 17:08:37  kryukov
# Include logos into package
#
# Revision 1.26  2003/04/13 14:40:12  kryukov
# 1) Put link step here from src/symb/Makefile.
# 2) Include mssmlib into s_comphep to calculate MSSM paramiters.
#
# Revision 1.25  2003/04/09 13:09:23  kryukov
# If WDIR already exist user may cancel setup or overwrite WDIR.
#
# Revision 1.24  2003/04/08 11:15:23  kryukov
# strfun directory use to make binary distribution
#
# Revision 1.23  2003/03/09 10:43:52  sherstnv
# Small change in the Makefile: flag DEBUG is removed
#
# Revision 1.22  2003/02/23 18:00:16  sherstnv
# Very small change in setup goal
#
# Revision 1.21  2003/02/23 16:15:59  kryukov
# Include .pdf files when binary distributive is prepared.
#
# Revision 1.20  2003/01/27 18:01:40  kryukov
# Add istallation from binary distributive target: bindist
#
# Revision 1.19  2003/01/25 19:11:13  kryukov
# Change USER_WORK_DIR -> WDIR
#
# Revision 1.18  2003/01/25 16:39:53  kryukov
# Change target pack->dist; new realization of prepearing of dist
#
# Revision 1.17  2003/01/25 16:38:53  kryukov
# Change target pack->dist; new realization of prepearing of dist
#
####################################################

SHELL=/bin/sh
VERSION=4.4.1
COMPHEP_DISTR=comphep-${VERSION}
PWD=$(shell pwd)
OSTYPE=$(shell uname)
REAL_DIST_NAME=$(shell basename ${PWD})

TMPDIR="/tmp/comphep$(shell id -u)"

#-----------------------------------------
# Correct the value of this variable:
#
# The WDIR variable have to point to the 
# user directory where all necessary files 
# for user install will live.
#-----------------------------------------
WDIR=../comphep_${VERSION}_test

CC=    $(shell more CC)
CFLAGS=$(shell more CFLAGS)
CLIBS= $(shell more CLIBS)

SUBDIR:=getmem chep_crt service2 polynom num plot symb tab mssmlib
CS=src
SUB_SUBDIR:=$(addprefix $(CS)/, $(SUBDIR))

COMPHEP_BIN_FILES=s_comphep tab_view.exe
BINDIST_FILES=${COMPHEP_BIN_FILES} models help usr include lib\
              Makefile INSTALL CC CFLAGS CLIBS launch_n_comphep configure \
	          strfun comphep32.png comphep64.png comphep128.png \
			  RELEASE-NOTES README

DOC_FILES=${PWD}/doc/SYMB_BATCH-HOWTO ${PWD}/doc/NUM_BATCH-HOWTO

.PHONY: precompile all link doc

all: link

link: compile
	$(CC) $(CFLAGS) -o s_comphep -lsymb -lserv \
	src/num/f_c.o -lmssm $(CLIBS) -lg2c

	@echo " "
	@echo "******************************************************************"
	@echo "*        CompHEP-${VERSION} has been successfully built          *"
	@echo "*                                                                *"
	@echo "*  Please create a user working directory using the command      *"
	@echo "*         make setup WDIR=path_to_your_user_work_dir             *"
	@echo "*  NOTE: Do not use '~' to refer to home directory               *"
	@echo "*        Use environment variable HOME instead of                *"
	@echo "******************************************************************"


compile: precompile
	$(MAKE) -C $(CS)/getmem   CHEP=$(PWD) CC=$(CC) CFLAGS='$(CFLAGS)'
	$(MAKE) -C $(CS)/chep_crt CHEP=$(PWD) CC=$(CC) CFLAGS='$(CFLAGS)'
	$(MAKE) -C $(CS)/service2 CHEP=$(PWD) CC=$(CC) CFLAGS='$(CFLAGS)'
	$(MAKE) -C $(CS)/num      CHEP=$(PWD) CC=$(CC) CFLAGS='$(CFLAGS)'
	$(MAKE) -C $(CS)/polynom  CHEP=$(PWD) CC=$(CC) CFLAGS='$(CFLAGS)'
	$(MAKE) -C $(CS)/symb     CHEP=$(PWD) CC=$(CC) CFLAGS='$(CFLAGS)' CLIBS='$(CLIBS)'
	$(MAKE) -C $(CS)/mssmlib  CHEP=$(PWD) CC=$(CC) CFLAGS='$(CFLAGS)'
	$(MAKE) -C $(CS)/plot     CHEP=$(PWD) CC=$(CC) CFLAGS='$(CFLAGS)' CLIBS='$(CLIBS)'
	$(MAKE) -C $(CS)/tab      CHEP=$(PWD) CC=$(CC) CFLAGS='$(CFLAGS)' CLIBS='$(CLIBS)'
       
precompile:
	@if (test ! -f CC); then \
	  echo " ";\
	  echo "*************************************";\
	  echo " 'make' can not find CC file.";\
	  echo " Launch 'configure' script first.";\
	  echo "*************************************";\
	  echo " ";\
	  exit 99; \
	fi
	@if (test ! -f CFLAGS); then \
	  echo " ";\
	  echo "*************************************";\
	  echo " 'make' can not find CFLAGS file.";\
	  echo " Launch 'configure' script first.";\
	  echo "*************************************";\
	  echo " ";\
	  exit 99; \
	fi
	@if (test ! -f CLIBS); then \
	  echo " ";\
	  echo "*************************************";\
	  echo " 'make' can not find CLIBS file.";\
	  echo " Launch 'configure' script first.";\
	  echo "*************************************";\
	  echo " ";\
	  exit 99; \
	fi
	
.PHONY: clean test cleandir
distclean: clean
	${MAKE} -C lib cleanall
	@rm -f $(COMPHEP_BIN_FILES) CC CFLAGS CLIBS
	@echo "Cleaning of CompHEP directory has done..."

test: clean
	@rm -f $(COMPHEP_BIN_FILES)

clean:
	@for dir in $(SUB_SUBDIR); do $(MAKE) -C $$dir clean; done
	${MAKE} -C doc clean
	@rm -f test/results/* test/tmp/* test/core*
	@echo "Cleaning of CompHEP source directories has done..."
	
##############################################################################
.PHONY: install uninstall setup pack

setup:
####### Variables test
	@if test "x$(WDIR)" = "x"; then \
	   echo "***** Undefine WDIR in Makefile!"; \
	   exit 99; \
	fi
	
####### Making directories
	@if (test ! -d $(WDIR)); then \
	    mkdir -p $(WDIR); \
	else \
	  echo " ";\
	  echo "**************************************************************";\
	  echo " The directory WDIR=$(WDIR) exists!";\
	  echo " "; \
	  echo " Overwrite?"; \
	  echo " Press [Enter] to continue or ^C to stop."; \
	  echo "**************************************************************";\
	  read; \
	fi
	@if (test ! -d $(WDIR)/results); then mkdir -p $(WDIR)/results; fi
	@if (test ! -d $(WDIR)/models);  then mkdir -p $(WDIR)/models; fi
	@if (test ! -d $(WDIR)/tmp);     then mkdir -p $(WDIR)/tmp; fi
	@if (test ! -d $(WDIR)/usr);     then mkdir -p $(WDIR)/usr; fi
	
	@sed -e s\\@CHEP@\\'$(PWD)'\\g usr/comphep > $(WDIR)/comphep
	@chmod 744 $(WDIR)/comphep
	@cp usr/comphep.ini $(WDIR)/comphep.ini
	@cp models/*.mdl $(WDIR)/models/.
	@cp usr/userFun* $(WDIR)/usr/.
	@sed -e s\\@CHEP@\\'$(PWD)'\\g usr/Makefile > $(WDIR)/usr/Makefile
	@sed -e s\\@CHEP@\\'$(PWD)'\\g usr/Makefile.results > $(WDIR)/results/Makefile
	@sed -e s\\@CHEP@\\'$(PWD)'\\g usr/n_comphep > $(WDIR)/results/n_comphep
	@chmod 744 $(WDIR)/results/n_comphep
	@sed -e s\\@CHEP@\\'$(PWD)'\\g usr/mk_tab > $(WDIR)/mk_tab
	@chmod 744 $(WDIR)/mk_tab
	@sed -e s\\@CHEP@\\'$(PWD)'\\g usr/tab_view > $(WDIR)/tab_view
	@chmod 744 $(WDIR)/tab_view
	@sed -e s\\@CHEP@\\'$(PWD)'\\g usr/symb_batch.pl > $(WDIR)/symb_batch.pl
	@chmod 744 $(WDIR)/symb_batch.pl
	@cp usr/process.dat $(WDIR)/process.dat

	@cp usr/num_batch.pl $(WDIR)/num_batch.pl
	
	@if [ -e libmathlib.a ]; then \
		sed -e s\\@CERNLIB@\\${PWD}\\g usr/CLIBS.bin >CLIBS; \
	fi; \

	$(MAKE) -C $(WDIR)/usr CC=$(CC) CFLAGS='$(CFLAGS)'

	@echo " "
	@echo " "
	@echo "*****************************************************************"
	@echo " Setup of the user directory for CompHEP-4.2 has been"
	@echo " successfully performed"
	@echo " "
	@echo " To start CompHEP go to the user directory ${WDIR}"
	@echo " and enter the command './comphep'"
	@echo "*****************************************************************"
	

##############################################################################
dist: distclean
	@if [ ${COMPHEP_DISTR} != `basename ${PWD}` ]; then \
		mkdir -p ${TMPDIR}/${COMPHEP_DISTR}; \
		cd ${TMPDIR}; \
		cp -a ${PWD}/[^d]* ${COMPHEP_DISTR}; \
		mkdir -p ${COMPHEP_DISTR}/doc; \
		cp -a ${DOC_FILES} ${COMPHEP_DISTR}/doc; \
		rm -fr `find ${COMPHEP_DISTR} -name CVS`; \
		tar czf ${PWD}/../${COMPHEP_DISTR}.tgz ${COMPHEP_DISTR}; \
		rm -fr ${TMPDIR}/${COMPHEP_DISTR}; \
	else \
		cd ..; \
		tar czf ${COMPHEP_DISTR}.tgz ${COMPHEP_DISTR}; \
	fi
	@cd ${PWD}

##############################################################################
bindist:
	@if [ ! -e ${PWD}/s_comphep ]; then \
	  echo " ";\
	  echo "*************************************";\
	  echo " 'make' can not find 's_comphep' file.";\
	  echo " Please check if you built CompHEP.";\
	  echo "*************************************";\
	  echo " ";\
	  exit 99; \
	else \
	  mkdir -p ${TMPDIR}/${COMPHEP_DISTR}-${OSTYPE}; \
	  cd ${TMPDIR}; \
	  for x in ${BINDIST_FILES}; do \
		cp -a ${PWD}/$$x ${COMPHEP_DISTR}-${OSTYPE}; \
	  done; \
	  mkdir -p ${COMPHEP_DISTR}-${OSTYPE}/doc; \
	  cp -a ${DOC_FILES} ${COMPHEP_DISTR}-${OSTYPE}/doc; \
	  cp -a ${DOC_FILES} ${COMPHEP_DISTR}-${OSTYPE}/doc; \
	  mkdir -p ${COMPHEP_DISTR}-${OSTYPE}/src/num; \
	  cp -a ${PWD}/src/num/include ${COMPHEP_DISTR}-${OSTYPE}/src/num; \
	  if [ -e ${CERNLIB_PATH}/libmathlib.a ]; then \
		cp ${CERNLIB_PATH}/libmathlib.a ${CERNLIB_PATH}/libisajet*.a \
		   ${COMPHEP_DISTR}-${OSTYPE}; \
	  fi; \
	  tar czf ${PWD}/../${COMPHEP_DISTR}-${OSTYPE}.tgz \
	      ${COMPHEP_DISTR}-${OSTYPE}; \
	  rm -fr ${TMPDIR}/${COMPHEP_DISTR}-${OSTYPE}; \
	  cd ${PWD}; \
	fi

doc:
	$(MAKE) -C doc
