:
# --------------------------------------------------------------------
# FUNCTION:
# Installation of nls2
# --------------------------------------------------------------------
# REFERENCE:
#  html/install.html 
# --------------------------------------------------------------------
# USAGE:
# ./INSTALL  [-h | --help ] 
# or: 
# ./INSTALL [-host <Splus|R|nls2C> ]
#           [-load <0|1|2>] 
#           [-system <unix|linux>]
#           [-command <path-command>] 
#           [-nosrc]
# PREREQUISITES:
# -See  "SPECIAL CONFIGURATIONS" and "EXAMPLES below
# --------------------------------------------------------------------
# Default, i.e without option: Install a version working with R on Linux.
# --------------------------------------------------------------------
#
# ARGUMENTS:
# -h | --help: Display how to use this script
#
# -host <R|Splus|nls2C>
#  Install a version working with
#  Splus, R, nls2C,  respectively.
#
# -load <0|1|2> 
#    0: Compilation and link are done by CMD COMPILE and CMD SHLIB
#    1: Compilation and link are done by the system compilators 
# and linkeditor defined in the file R_HOME/etc/Makeconf
#    2: Compilation and link are done by using the variables
#  defined in the file ./src/MyMakevars.
# IN THAT CASE, CUSTOMIZE THE FILE ./src/MyMakevars BEFORE using
# INSTALL.
# Notes:
# *** See ./src/MyMakevars.SunOS for installation on 
# sparc - sunos 2.9 (64bits) with GNU compilers.
# *** This option is automatically set to 2  when -host is not R.
#
# -system <unix|linux>
#  Install a version working on Unix or Linux, respectively.
#
# -command <path-command> 
# where <path-command> is the pathname of the command Splus or R on your site.
# The default command is  "R" or, when -host is Splus, "Splus".
#
# -nosrc
# When set, the directory 'src' is deleted after installation. 
# If you choose this option, installation of nls2C 
# will not be possible.
# --------------------------------------------------------------------
# NOTES:
# Compilation warnings can be ignored.
# Examples are executed by the script INSTALL. This may require a few minutes.
# --------------------------------------------------------------------
# SPECIAL CONFIGURATIONS:
#
# - On 64 bits: 
# Compilation should be done with the -fPIC option. 
# Customize the file src/MyMakevars to your own libraries 
# and install by:    INSTALL -load 2
#
# - On Linux Ubuntu, or R 2.7:
# If the locations of the directory 'share' or/and the file 'Makeconf'of R
# are not RHOME/share and RHOME/etc/Makeconf respectively,
# modify the file 'configure': search for UBUNTU in this file
#
# - When the Lex library is not libfl.a, 
#  replace "-lfl" by the correct
# library reference, for example "-ll", in the file
# src/Makevars.in and, when '-host' is not R, in the file
# src/MyMakevars 
#
# - When the suffix of the shared-libraries is not '.so':
# change the pattern '.so'  to the appropriate suffix in this
# current  file (e.g the file INSTALL),
# and, when '-host' is not R, in the files src/MyMakevars
# and R/loadnls2.s.in
#
# - When '-system' is linux and the file ieee754.h is not available
# on your site or,
# when '-system' is  unix and the file ieeefp.h is not available
# on your site:
# change the pattern 'ieee754.h' ('ieeefp.h' respect.)
# in the file configure to the name
# of a file which contains the definition of the function 'finite(x)'
# (a C function which returns 0 when its argument is not a finite
# number)
#
# - See ./src/MyMakevars.SunOS for installation on 
# sparc - sunos 2.9 (64bits) with GNU compilers.

# ----------------------------------------------------------------
# EXAMPLES:
# ---------
# 1/ Installation of a R version under Linux.
#         ./INSTALL > install.output
# '> install.output' means that the installation messages are
# redirected into a file called  "install.output". 
# Have a careful look at this file.
#
# 2/ Installation by using the same compilation tools as R:
#    ./INSTALL -load 1 > install.output
#    Compilation and link-edition are done by the system 
# compilators and  linkeditor defined in the file 
# R_HOME/etc/Makeconf. 
#
# 3/ Installation by using your compilation tools:
#    ./INSTALL -load 2 > install.output
#    Compilation and link-edition are done by using the variables
# defined  in the file ./src/MyMakevars.
# You should customize it previously.
#
# 4/ Installation of a Splus  version under Unix:
#          ./INSTALL -host Splus  -system unix > install.output
#
# 5/ Installation of the part required for nls2C under Linux.
#          ./INSTALL -host nls2C  > install.output
# 
# ---------------------------------------------------------
# RESERVED USAGE:
# (Also available for partial installation: 
#  configure  func compile dataex  testex demoex )
# ------------------------------------------------------------------
# configure: 
#       invoke the shell-script 'configure', i.e modify the files of the
#       package according to its pathname, and to the options on the command-line:
#       see DETAILS in the file 'configure'
# func: 
# 	load the functions under Splus or R
# compile: 
#	compile the C and Fortran programmes by execution of the src/Makefile
# dataex: 
# 	load under Splus or R the example dataframes
# 	in the directory 'data'
# testex:
#	execute the examples in a directory called 'tests', 
#       (created by configure).
# demoex:
#	execute the examples in the directory called 'demo'
# -----------------------------------------------------------------
# Last release: 2003
# --------------------------------------------------------------------
# DETAILS: (french)
# Appel par l'utilisateur, comme alternative  R CMD INSTALL, ce
# script doit raliser la mme chose; en plus, on lui fait
# excuter les dmos, pour vrifier l'installation.
# Ce qu'il fait:
# - appelle 'configure' avec les options ncessaires
# - charge les fonctions (sauf dans le cas nls2C)
# - compile par cd src; make 
# Les excutables (crInv, analDer, etc...) sont alors dans le
# directory 'inst' (il est ncessaire de les mettre dans un dir
# appel ainsi,  cause du R CMD INSTALL qui dtruit tout ce qui n'est
# pas 'standard',  moins que ce soit dans ce dir.
# Comme  R CMD INSTALL appelle aussi 'configure' et les 'Makefiles',
# on a crit ces fichiers de faon  ce que les excutables soient
# dans 'inst'.
# - comme le fait R CMD INSTALL, dplace le dir 'inst' d'un niveau suprieur
# et les fichiers src/*.so dans 'libs'
#  ce stade, 'libs' contient les librairies partages (*.so) et
# 'nls2libs' contient les compils (*.o) et les librairies statiques (*.a)
# Puis, sauf dans le cas nls2C:
# - fait excuter tous les exemples du dir. 'tests'
# (ce qu'est cens faire aussi R CMD INSTALL): si elle n'existe pas dj, cr
# la fonction 'is.R' (elle est native dans R)
# - sur l'option 'demoex', fait excuter tous les exemples du dir. 'demo'
#
# --------------------------------------------------------------------
# Definition of  function erreur()
# Print an error message and exit
# --------------------------------------------------------------------
erreur() 
  {
echo ""
   echo "usage : $0 [-h|--help] | [-host <Splus|R|nls2C>] [-load <0|1|2>] [-system <unix|linux>] [-command <path-command>]  [-nosrc]"
echo " See the comments included in the file INSTALL"
echo " By default -host R -system linux -command R"
echo ""
echo ""
 exit 1 
  }
# --------------------------------------------------------------------
# Function executing the example $exfic
# --------------------------------------------------------------------
faireex()
{
	exlocal=`basename $exfic`
        test -f $exlocal || (cp $exfic .)
   echo "Execute $exlocal"
#   time $cde BATCH $exlocal $exlocal"out"
# Pas en batch, sinon ca lance tous les ex en meme temps
# et avec Splus, les fichiers resultats ne sont pas crs tout de suite
# bien qu'on ait la main, ce qui fait qu'on ne peut pas comparer avec
# les fichiers de demo dans le meme script
if [ $lhost = "Splus" ]
then
	 time $cde < $exlocal > $exlocal"out" 2>&1 
else
         time $cde --no-save --quiet --slave < $exlocal > $exlocal"out"  2>&1 
fi
}

# --------------------------------------------------------------------
# Function executing all the examples in the directory $exdir
# --------------------------------------------------------------------
fairelesex()
{
test -d $exdir || (mkdir $exdir)
cd ./$exdir; 
if [ $lhost = "Splus" ]
then
	test -d .Data && (rm -r .Data)
	$cde CHAPTER
	$cde < ../R/is.R
fi


echo ""
echo "Execution  of all the examples in the $exdir directory: wait!!"
for exfic in `ls *.R`
do
case $exfic in
	*ej.R | *mag.R | *simul.R) 
		echo "$exfic needs Nag library:";
		echo "if you have it, modify the call to loadnls2 in $exfic";
		echo "and run the example by executing $exfic in a $lhost session";;
	*)
		faireex ;;
esac
done

rm lib*.so
# Verify the results with "Execution halted"
echo ""
echo "Verification of the results-files which contain the string Execution halted:"
grep halted **
# Verify the results with "Command terminated"
echo ""
echo "Verification of the results-files which contain the string Command terminated:"
grep "Command terminated" **
cd ..
} # fin fairelesex


# --------------------------------------------------------------------
# Main script:
# --------------------------------------------------------------------

# Initialisations:
#------------------
lhost="R"
yahost=0
los="linux"
yasystem=0
lacde=0
yacde=-1
optconfig="" # les options  passer  configure
tout=1
config=0
func=0
compile=0
dataex=0
testex=0
demoex=0
load=0
yaload=-1
nosrc=0

# --------------------------------------------------------------------
# Decode the arguments on the command-line:
# --------------------------------------------------------------------
for i in $*
             do
               case $i in
		 -h|--help)erreur; exit;;
                 -host)  yahost=1; optconfig=$optconfig" "$i;;
		  -load) load=1; yaload=1; optconfig=$optconfig" "$i;;
		 -system)  yasystem=1; optconfig=$optconfig" "$i;;
		 -command) lacde=1; yacde=1; optconfig=$optconfig" "$i;;
		  -nosrc) nosrc=1; optconfig=$optconfig" "$i;;
                 configure) tout=0;  config=1;;
                 func) tout=0;  func=1;;
                 compile) tout=0;  compile=1;;
		 dataex) tout=0;  dataex=1;;
                 testex) tout=0;  testex=1;;
                 demoex) tout=0;  demoex=1;;
                 *)  if [ $yacde -gt 0 ]
			then
				cde=$i
				yacde=0
				optconfig=$optconfig" "$i;
			else if [ $yaload -eq 1 ]
		       then
                         load=$i;
                         yaload=0;
			optconfig=$optconfig" "$i;
		       else if [ $yahost -eq 1 ]
		       then
                         lhost=$i;
                         yahost=0;
		         optconfig=$optconfig" "$i;

   		       else if [ $yasystem -eq 1 ]
		       then
                         los=$i;
                         yasystem=0
		         optconfig=$optconfig" "$i;
			else
  	            		echo "Invalid option: $i" ; erreur 
			fi; fi; fi;
			fi;;
                esac
             done

if [ $yacde -ne 0 ]
then
# cas o on n'a pas fourni de cde
	if [ $lhost = "Splus" ]
	then
		cde="Splus"
	else
		cde="R"
	fi
fi
optconfig=$optconfig" -command "$cde

# --------------------------------------------------------------------
# Verifications
# --------------------------------------------------------------------
echo "Options:"
echo "optconfig=$optconfig"

if [ $lhost = "R" ]
then
# Verify the existence of RHOME
	${R_HOME=`$cde RHOME`}
	${R_HOME?"Could not determine R_HOME"}
fi

if [ $lacde -eq 1 -a $yacde  -ne 0 ]
	then
	echo "command missing after the -command option"
	erreur
fi
if [ $yaload -eq 1 ]
	then
	echo "load value missing after the -load option"
	erreur
fi

if [  $yahost  -ne 0 ]
	then
	echo "host missing after the -host option"
	erreur
fi

if [  $yasystem  -ne 0 ]
	then
	echo "system missing after the -system option"
	erreur
fi


# --------------------------------------------------------------------
# Set the default when $lhost is "nls2C"
# --------------------------------------------------------------------

if [ $lhost = "nls2C" ]
then
	func=0; dataex=0;  testex=0; demoex=0;
	if [ $tout -eq 1 ]
	then
	  config=1; compile=1; tout=0
	fi
fi
# --------------------------------------------------------------------
# Move the directory doc/html one level above
# --------------------------------------------------------------------
mv doc/html .

	
# --------------------------------------------------------------------
# Execution of the script 'configure'
# --------------------------------------------------------------------

if [ $tout -eq 1 -o $config  -eq 1 ]
then
 echo ""
 echo "Execution of ./configure $optconfig"
 ./configure $optconfig
fi


# --------------------------------------------------------------------
# Load the functions in the directory nls2:
# --------------------------------------------------------------------
if [ $tout -eq 1 -o $func  -eq 1 ]
then
echo ""
echo "Load the functions in the directory nls2:";
#$cde BATCH R/source.fun  ./source.fun.Rout
# pas en batch, car il faut que ca soit fini avant de continuer
if [ $lhost = "Splus" ]
then
	test -d .Data && (rm -r .Data)
	$cde CHAPTER
	 time $cde < R/source.fun 
else
 time $cde --save  < R/source.fun 
fi
fi


# --------------------------------------------------------------------
# Compile the C and Fortran programs: 
# --------------------------------------------------------------------
if [ $tout -eq 1 -o $compile  -eq 1 ]
then
echo ""
echo "Compile the C and Fortran programs:"
(cd ./src; make)

# --------------------------------------------------------------------
# Copy the contents of the 'inst' directory at one level above
# as R CMD INSTALL should have done if we had used it
# --------------------------------------------------------------------
echo "Copy the contents of the 'inst' directory at one level above"
test -d inst || (echo "Error of compilation? cannot found directory 'inst'"; exit 1)
for unfic in `ls inst`
do
# We move the files  from 'inst' toward one level above,
# REMOVE, 21/02/2012 if they do not already exist at this level, only.
# REMOVE, 21/02/2012  (test -d $unfic || test -f $unfic ) ||  mv inst/$unfic .
  mv inst/$unfic .
# We delete the files of 'inst' which had not been transfered,
# (i.e the files that already existed  at the above level)
  test -d inst/$unfic && (rm -r inst/$unfic)
  test -f inst/$unfic && (rm inst/$unfic)
done
rmdir inst

# --------------------------------------------------------------------
# The  R CMD INSTALL should have put the shared librairies 
# in the directory 'libs': we do idem
# --------------------------------------------------------------------
# (Note that the other required compiled files are put in the 'libs' directory
# and the executable commands in the up-level by the file src/Makefile)
test -d libs || (mkdir libs)
mv ./src/*.so ./libs
fi


# --------------------------------------------------------------------
# Load the data-example-files used in the data directory: 
# --------------------------------------------------------------------
if [ $tout -eq 1 -o $dataex -eq 1 ]
then
cd ./data
echo ""
echo "Load the data-example-files used in the data directory:"
echo `pwd`;
# pas en batch, car il faut que ca soit fini avant de continuer
# $cde BATCH all.R all.Rout

if [ $lhost = "Splus" ]
then
	test -d .Data && (rm -r .Data)
	$cde CHAPTER
	$cde < all.R 
else
  	 $cde  --save  < all.R 
fi
cd ..
fi


# --------------------------------------------------------------------
# Execution of all the examples in the tests directory: 
# --------------------------------------------------------------------
if [ $testex -eq 1 ]
then
  exdir=tests
  fairelesex
fi



# --------------------------------------------------------------------
# Execution of all the examples in the demo directory: 
# --------------------------------------------------------------------
if [  $tout -eq 1 -o $demoex -eq 1 ]
then
  exdir=demo
  fairelesex
fi

# --------------------------------------------------------------------
echo "End of $0"
echo "---------------"
# --------------------------------------------------------------------





