# Makefile for McStas.
#  
#   This file is part of the McStas neutron ray-trace simulation package
#   Copyright (C) 1997-2006, All rights reserved
#   Risoe National Laborartory, Roskilde, Denmark
#   Institut Laue Langevin, Grenoble, France
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

SHELL = /bin/sh

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
srcdir = @srcdir@
libdir = @libdir@


DEBUG = -DDEBUG=0
mc_libdir = $(libdir)/mcstas
#DEBUG=

CC = @CC@
CFLAGS = @CFLAGS@
LDFLAGS= @LDFLAGS@
DEFS = @DEFS@ $(DEBUG)

PERL = @PERL@

FLEX = flex
FLEXFLAGS=-i

BISON = bison
BISONFLAGS = -v -d

SCILAB = @SCILAB@
MATLAB = @MATLAB@
PGPLOT = @PGPLOT@
VRML = @VRMLVIEW@

WGET = @WGET@

XTERM = @TERM@

INSTALL=@INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@


#
# End of configuration section.
#

OBJECTS=instrument.tab.o lex.yy.o debug.o \
	memory.o list.o symtab.o coords.o cexp.o \
	file.o cogen.o port.o

PERLOBJ=gscan.fixpl mcdisplay.fixpl mcplot.fixpl mcresplot.fixpl mcrun.fixpl \
        mcgui.fixpl mcdoc.fixpl mcstas2vitess.fixpl mcconvert.fixpl mcdaemon.fixpl
PERLBIN=gscan mcdisplay mcplot mcresplot mcrun mcgui mcdoc mcstas2vitess mcconvert mcdaemon
BINOBJS=$(PERLOBJ) mcstas

.SUFFIXES: .c .o .pl .fixpl

.c.o:
	$(CC) -I. -I$(srcdir) -c $(CFLAGS) -DMCSTAS='"'$(mc_libdir)'"' $(DEFS) $<

.pl.fixpl:
	sed -e 's+#! /usr/bin/perl+#! '$(PERL)'+' \
	    -e 's+\$MCSTAS::sys_dir = "/usr/local/lib/mcstas"+\$MCSTAS::sys_dir = "'$(mc_libdir)'"+' \
	    < $(srcdir)/$< > $@
	chmod +x $@


all: $(BINOBJS)

mcstas: $(OBJECTS)
	$(CC) -o mcstas $(CFLAGS) $(LDFLAGS) $(OBJECTS) -lm

clean:
	rm -f $(BINOBJS) $(OBJECTS) $(PERLOBJ) TAGS \
	instrument.tab.c instrument.tab.h instrument.output lex.yy.c \
  config.cache Makefile
TAGS:
	etags *.[chly]

install: installdirs $(BINOBJS)
	$(INSTALL_PROGRAM) mcstas $(bindir)/mcstas
	for pgm in $(PERLBIN); do \
	  $(INSTALL_PROGRAM) $$pgm.fixpl $(bindir)/$$pgm; \
	done
	if [ -d $(mc_libdir) ]; then \
	  echo "Moving your old library dir $(mc_libdir) to $(mc_libdir).`date +%Y%m%d_%H.%M`"; \
	  mv -f $(mc_libdir) $(mc_libdir).`date +%Y%m%d_%H.%M`;\
        fi;
	$(srcdir)/mkinstalldirs $(mc_libdir)
	for file in `cd lib; ls`; do \
	  if [ -d lib/$$file ]; then \
	    $(srcdir)/mkinstalldirs $(mc_libdir)/$$file; \
	    for comp in `cd lib/$$file; ls`; do \
        if [ -f $(mc_libdir)/$$file/$$comp ]; then \
          echo "Renaming old library element version $(mc_libdir)/$$file/$$comp to $(mc_libdir)/$$file/$$comp.old"; \
          mv -f $(mc_libdir)/$$file/$$comp $(mc_libdir)/$$file/$$comp.old; \
        fi; \
	      if [ -d lib/$$file/$$comp ]; then \
          $(srcdir)/mkinstalldirs $(mc_libdir)/$$file/$$comp; \
          for tool in `cd lib/$$file/$$comp; ls`; do \
	      if [ -d lib/$$file/$$comp/$$tool ]; then \
          $(srcdir)/mkinstalldirs $(mc_libdir)/$$file/$$comp/$$tool ; \
	        for subtool in `cd lib/$$file/$$comp/$$tool; ls`; do \
		  if [ -d lib/$$file/$$comp/$$tool/$$subtool ]; then \
		     $(srcdir)/mkinstalldirs $(mc_libdir)/$$file/$$comp/$$tool/$$subtool ; \
	             for subsubtool in `cd lib/$$file/$$comp/$$tool/$$subtool; ls`; do \
                       $(INSTALL_DATA) lib/$$file/$$comp/$$tool/$$subtool/$$subsubtool $(mc_libdir)/$$file/$$comp/$$tool/$$subtool/$$subsubtool; \
                     done \
		  else \
                    $(INSTALL_DATA) lib/$$file/$$comp/$$tool/$$subtool $(mc_libdir)/$$file/$$comp/$$tool/$$subtool; \
		  fi; \
                done \
              else \
                $(INSTALL_DATA) lib/$$file/$$comp/$$tool $(mc_libdir)/$$file/$$comp/$$tool; \
	      fi; \
          done \
        else \
	        $(INSTALL_DATA) lib/$$file/$$comp $(mc_libdir)/$$file/$$comp; \
        fi; \
	    done \
	  else \
	    $(INSTALL_DATA) lib/$$file $(mc_libdir)/$$file; \
	  fi \
	done
	./mcdoc.fixpl --text
	mkdir $(mc_libdir)/tools/perl/modules
	cd support/Proc-Simple-1.19 && $(PERL) Makefile.PL LIB=$(mc_libdir)/tools/perl/modules PREFIX=$(mc_libdir)/tools/perl/modules && make && make install 
	cd support/Tk-CodeText-0.3.4 && $(PERL) Makefile.PL LIB=$(mc_libdir)/tools/perl/modules PREFIX=$(mc_libdir)/tools/perl/modules && make && make install 


installdirs:
	$(srcdir)/mkinstalldirs $(bindir) $(libdir) $(mc_libdir)

instrument.tab.o: mcstas.h
lex.yy.o: mcstas.h instrument.tab.h
debug.o: mcstas.h
memory.o: mcstas.h
symtab.o: mcstas.h
list.o: mcstas.h
coords.o: mcstas.h
cexp.o: mcstas.h
file.o: mcstas.h
cogen.o: mcstas.h
port.o: port.h
mcformat.o: mcstas.h

instrument.tab.c instrument.tab.h: instrument.y
	$(BISON) $(BISONFLAGS) instrument.y

lex.yy.c: instrument.l
	$(FLEX) $(FLEXFLAGS) instrument.l

# Prefer Scilab over Matlab over PGPLOT
config: 
	if [ $(VRML) != no ]; then \
		make vrml; \
	fi;
	if [ $(PGPLOT) != no ]; then \
		make pgplot; \
	fi; \
	if [ $(MATLAB) != no ]; then \
		make matlab; \
	fi; \
	if [ $(SCILAB) != no ]; then \
		make scilab; \
	fi; 

scilab:
	sed "s/PLOTTER => '.*'./PLOTTER => 'Scilab'\,/" lib/tools/perl/mcstas_config.perl > lib/tools/perl/mcstas_config.perl.tmp
	mv lib/tools/perl/mcstas_config.perl.tmp lib/tools/perl/mcstas_config.perl
matlab: 
	sed "s/PLOTTER => '.*'./PLOTTER => 'Matlab'\,/" lib/tools/perl/mcstas_config.perl > lib/tools/perl/mcstas_config.perl.tmp
	mv lib/tools/perl/mcstas_config.perl.tmp lib/tools/perl/mcstas_config.perl
pgplot:
	sed "s/PLOTTER => '.*'./PLOTTER => 'McStas'\,/" lib/tools/perl/mcstas_config.perl > lib/tools/perl/mcstas_config.perl.tmp
	mv lib/tools/perl/mcstas_config.perl.tmp lib/tools/perl/mcstas_config.perl
vrml:
	sed "s/PLOTTER => '.*'./PLOTTER => 'VRML'\,/" lib/tools/perl/mcstas_config.perl > lib/tools/perl/mcstas_config.perl.tmp
	mv lib/tools/perl/mcstas_config.perl.tmp lib/tools/perl/mcstas_config.perl
PGPLOT:
	make pgplot
Matlab:
	make matlab
Scilab:
	make scilab
VRML:
	make vrml

test:
	./mcrun.fixpl --test

# Optional auto install of patched perl-Tk package
tk:
	if [ $(WGET) != no ]; then \
		$(WGET) http://www.mcstas.org/download/Tk-804.027_gtk2_patch.tar.gz &&\
		tar xzvf Tk-804.027_gtk2_patch.tar.gz > /dev/null && \
		cd Tk-804.027_gtk2_patch && $(PERL) Makefile.PL LIB=$(mc_libdir)/tools/perl/modules PREFIX=$(mc_libdir)/tools/perl/modules && make && make install ;\
	fi;
