# Makefile for McStas.
#
#   This file is part of the McStas neutron ray-trace simulation package
#   Copyright (C) 1997-2008, 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; version 3 of the License.
#
#   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

# Available methods for installation
# make perl-modules      	Install perl modules Proc-Simple Tk-CodeText Math-Amoeba
# make perl-PGPLOT      	Install perl modules F77+PGPLOT (require pgplot lib)
# make compile-pgplot     Compile pgplot5 libs
# make compile-scilab     Compile scilab
# make compile-tk         Compile Tk

SHELL = /bin/sh

prefix = @prefix@
srcdir = @srcdir@
mc_libdir = @prefix@/lib/@MCCODE_TARNAME@/
G77  = @G77@

PERL = @PERL@
PERLBIN=`which perl`
PERL5LIB=@prefix@/lib/@MCCODE_TARNAME@/tools/perl/modules:`perl -e "print join(\":\", @INC);"`
PGPLOT_DIR=@prefix@/pgplot
LD_LIBRARY_PATH=${PGPLOT_DIR}
USE_PIC=@USE_PIC@

REL=4.0
SCIREL=scilab-$REL
SCIFILE=$SCIREL.bin.linux-i686.tar.gz
	
perl-modules:
	$(srcdir)/../../mkinstalldirs  $(mc_libdir)/tools/perl/modules
	# install Proc-Simple
	cd $(srcdir)/Perl/Proc-Simple-1.19 && $(PERL) Makefile.PL LIB=$(mc_libdir)/tools/perl/modules PREFIX=$(mc_libdir)/tools/perl/modules && make && make install
	# install Tk-CodeText
	cd $(srcdir)/Perl/Tk-CodeText-0.3.4 && $(PERL) Makefile.PL LIB=$(mc_libdir)/tools/perl/modules PREFIX=$(mc_libdir)/tools/perl/modules && make && make install
	# install Math-Amoeba
	cd $(srcdir)/Perl/Math-Amoeba-0.04 && $(PERL) Makefile.PL LIB=$(mc_libdir)/tools/perl/modules PREFIX=$(mc_libdir)/tools/perl/modules && make && make install
	@echo "Installed Proc-Simple Tk-CodeText Math-Amoeba perl modules in @prefix@/lib/@MCCODE_TARNAME@/tools/perl/modules"

perl-PGPLOT:
	$(srcdir)/../../mkinstalldirs  $(mc_libdir)/tools/perl/modules
	# install F77 
	cd $(srcdir)/Perl/ExtUtils-F77-1.16 && perl Makefile.PL PREFIX=@prefix@/lib/@MCCODE_TARNAME@/tools/perl/modules LIB=@prefix@/lib/@MCCODE_TARNAME@/tools/perl/modules && make && make install
	# install perl-PGPLOT
	cd $(srcdir)/Perl/PGPLOT-2.20
	if test "`basename ${G77}`" = "g77" ; then \
		  sed 's/#use ExtUtils::F77 qw(generic g77);/use ExtUtils::F77 qw(generic g77);/' $(srcdir)/Perl/PGPLOT-2.20/Makefile.PL > $(srcdir)/Perl/PGPLOT-2.20/Makefile.PL.new ; \
		  mv $(srcdir)/Perl/PGPLOT-2.20/Makefile.PL.new $(srcdir)/Perl/PGPLOT-2.20/Makefile.PL; \
	fi
	cd $(srcdir)/Perl/PGPLOT-2.20 && export PERL5LIB=${PERL5LIB} && perl Makefile.PL PREFIX=@prefix@/lib/@MCCODE_TARNAME@/tools/perl/modules LIB=@prefix@/lib/@MCCODE_TARNAME@/tools/perl/modules && make && make install
	@echo "Installed perl-PGPLOT modules in @prefix@/lib/@MCCODE_TARNAME@/tools/perl/modules"

install-pgplot: compile-pgplot perl-PGPLOT
compile-pgplot:
	cd pgplot && make && make install
	@echo "Installed pgplot libraries in ${PGPLOT_DIR}"

install-scilab: compile-scilab
compile-scilab:
	export TMPDIR=`mktemp -d`
	# Get + unpack scilab from the net...
	cd ${TMPDIR} && wget http://www.scilab.org/download/${REL}/${SCIFILE}
	cd ${TMPDIR} && gunzip -c ${SCIFILE} | tar xf -
	cd ${prefix} && tar xzf ${TMPDIR}/${SCIFILE}
	cd ${prefix}/${SCIREL} && make
	echo "Setting up links in ${prefix}/bin"
	ln -s ${prefix}/${SCIREL}/bin/scil* ${prefix}/bin
	# Removing dowloaded archive
	rm -rf ${TMPDIR}

compile-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;
