#! /bin/csh -f
#
#  5-mar-2004     for 1st nbody-school                             PJT 
# 20-apr-2004     minor things for fam04                           pjt
# 24-jul-2005     many changes for 2nd nbody-school MODEST5c       PJT
# 21-jul-2006     various changes to go with the times (mexico)        pjt
#                 and switchover to the new configure based install    
# 23-jul-2006     make sure the manybody_pkg tree is treated R/O       pjt
#                 changed order of tools for gh2006 
# 17-aug-2006     no more falcON option, fix root if manybody= not used		PJT
#
#  Removed any local directory called manybody and installs a new manybody tree
#  The default should be executed from the home directory, such that
#  MANYBODY becomes $HOME/manybody
#
#  Example usage:
#  manybody_pkg/manybody/install_manybody cvs=0 pkg=`pwd`/manybody_pkg > & LOG1
#
#  CVS: $Id$

#  things that are 0 or 1
set pgplot=1
set cvs=0
set check=1
set clean=0

#  things that already have versions if non-zero, else won't be installed
set gcc=0
set intel=0

#  start label if only a subset is warrented
set start=start

#  things that have versions and could in theory change; typically setting
#  something to 0 will disable it since the version does not exist
set gsl=1.8
set fewbody=0.21
set glstarview=0.6-devel
set hnbody=1.0.3-linux-x86
set acs=lite-1.2.3
set ruby=1.8.4
set fltk=1.1.4
set ez=3_0_4
set tioga=1.1.2
set tipsy=2.2.3
set cosmics=1.04

#  root directory where all the tar ball packages live from which to install
set pkg=`pwd`/manybody_pkg
set sys=`uname -s | tr '[A-Z]' '[a-z]'`
set manybody=`pwd`/manybody

#  poor mans command line parser
foreach _a ($*)
  set $_a
end

# hello world
set version='$Id$'
echo "$0 : version $version"

# sanity check
if (! -e $pkg/aarseth) then
  set pkg0=$0:h
  echo -n "pkg=$pkg does not seem to be a manybody_pkg directory, trying $pkg0 ... "
  if (! -e $pkg0/aarseth) then
    echo " FAILED"b
    echo "I give up. Try giving the pkg= parameter on the commandline"
    exit 1
  else
    echo "OK"
    set pkg=$pkg0
  endif
else
  echo `date` Using pkg=$pkg 
endif

# remove the old manybody, and create a new one; but be nice
# don't do it if the old one exists..
# also, $manybody must be an absolute path now!!

set slash=`echo $manybody | cut -c1`
if (\`$slash\` != \`"/"\`) then
  set manybody=`pwd`/$manybody
  echo "Warning: setting absolute path manybody=$manybody"
endif

if ($clean) rm -rf $manybody
if (-e $manybody) then
  echo "$manybody exists, remove it or use clean=1"
  exit 1  
endif
mkdir $manybody
cd $manybody

#                             default root = $MANYBODY
setenv MANYBODY $manybody
set root=$MANYBODY
mkdir -p $root/tmp
mkdir -p $root/opt

set path=(. $root/opt/bin $path)
rehash

#                                                  copying shell startup files
echo " `date` Installing startup shell scripts in local install"
cp -r $pkg/manybody install
pushd install >& /dev/null
  set log=$root/tmp/configure.log
  ./configure --with-manybody=$manybody >& $log
  cp manybody_start.*sh $root 
  if ($?CC) then
    echo CC=$CC
  endif
  if ($?CXX) then
    echo CXX=$CXX
  endif
  if ($?F77) then
    echo F77=$F77
  endif
popd >& /dev/null


#PJT
goto $start

start:

gcc:
#                                                   gcc compiler
if ($gcc != 0) then
  echo `date` Installing gcc $gcc
  if ($?CC) then
    echo "   Warning: CC=$CC defined, but gcc=$gcc was specified, so some things will be broken"
  endif
  set log=$root/tmp/gcc.log
  tar jxf $pkg/gcc/gcc-$gcc.tar.bz2
  pushd gcc-$gcc  >& /dev/null
    # a simple gcc. note f77 is replaced by g95 starting gcc4
    set gcc4=`echo $gcc | cut -d. -f1`
    if ($gcc4 == 4) then
      set copts=(--prefix=$root/opt  --enable-threads --enable-languages=c,c++,f95)
    else
      set copts=(--prefix=$root/opt  --enable-threads --enable-languages=c,c++,f77)
    endif
    (mkdir _build; cd _build; ../configure $copts; make bootstrap; make; make install) >& $log
  popd >& /dev/null
  rehash

  # now that the compiler has changed.... need to re-encode that in the startup scripts

  pushd $root/install >& /dev/null
    setenv CC  `which gcc`
    setenv CXX `which g++`
    if ($gcc4 == 4) then
      setenv F77 `which gfortran`
    else
      setenv F77 `which g77`
    endif
    set log=$root/tmp/configure.log
    ./configure --with-manybody=$manybody >>& $log
    cp manybody_start.*sh $root 
  popd >& /dev/null

endif

intel:
#                                                   intel compiler
if ($intel != 0) then
  echo `date` Installing intel 8.1
  set log=$root/tmp/intel.log
  tar zxf $pkg/intel/intel81.tar.gz
  pushd intel  >& /dev/null
    tar zxf $pkg/intel/config81.tar.gz
    pushd config >& /dev/null
      ./configure >& $log
    popd >& /dev/null
  popd >& /dev/null
  # need to add the compiler to the path
  source intel/bin/iccvars.csh
  rehash
endif

#                                                   cvsutils
echo  `date` Installing cvsutils
tar zxf $pkg/tools/cvsutils-0.2.3.tar.gz
pushd cvsutils-0.2.3 >& /dev/null
  set log=$root/tmp/cvsutils.log
  ./configure --prefix=$root/opt   >& $log
  make                            >>& $log
  make install                    >>& $log
popd >& /dev/null
rehash

nemo:
#                                                   nemo
echo  `date` Installing NEMO
tar zxf $pkg/nemo/nemo_cvs.tar.gz
ln -s nemo_cvs nemo
pushd nemo_cvs >& /dev/null
  set log=$root/tmp/nemo.log 
  if ($cvs) cvs up >>& $log
  source AAA_SOURCE_ME  >& $log
  make bins >>& $log
  echo "See also install.log in the nemo_cvs directory" >> $log
  echo NEMO-COUNT: `ls $NEMOBIN|wc -l`
popd >& /dev/null
rehash

#                                                    firstn
echo  `date` Installing firstn
pushd $NEMO/usr/aarseth/firstn >& /dev/null
  set log=$root/tmp/firstn.log 
  make firstn >& $log
  if ($status) echo ERRORS compiling firstn, check $log  
  cp firstn $NEMOBIN
popd >& /dev/null
rehash

#                                                    nbody6 (source in NEMO)
echo  `date` Installing nbody6
mkdir $NEMO/usr/aarseth/nbody6
pushd $NEMO/usr/aarseth/nbody6 >& /dev/null
  set log=$root/tmp/nbody6.log 
  tar zxf $pkg/aarseth/nbody6/nbody6.tar.Z
  (cd Ncode ; make nbody6 F77=$F77; cp nbody6 $NEMOBIN) >& $log
  if ($status) echo ERRORS compiling nbody6, check $log  
popd >& /dev/null
rehash

#                                                   GalactICS
echo  `date` Installing galactICS
pushd $NEMO/usr/kuijken/GalactICS-exp/src >& /dev/null
   set log=$root/tmp/gal.log
   make -f Makefile.nemo all install >& $log
   mknemo mkkd95 >>& $log
popd >& /dev/null
rehash

#                                                   CGS
echo  `date` Installing CGS
pushd $NEMO/usr/trenti >& /dev/null
   set log=$root/tmp/cgs.log
   mknemo CGS >>& $log
popd >& /dev/null
rehash

ds9:
#                                                   ds9
if ($sys == linux) then
echo  `date` "Installing ds9 - for linux only"
pushd $NEMOBIN >& /dev/null
  tar zxf $pkg/ds9/ds9.tar.gz
popd >& /dev/null
rehash
endif

#                                                    xyz stuff
echo  `date` Installing xyz from NEMO
  set log=$root/tmp/xyz.log 
  $NEMO/src/scripts/nemo.vogl  >& $log
  if ($status) echo ERRORS compiling vogl, check $log
  mknemo xyzview snapxyz      >>& $log
  if ($status) echo ERRORS compiling xyz, check $log

tipsy:
#                                                    tipsy
echo  `date` Installing tipsy
tar zxf $pkg/tipsy/tipsy-$tipsy.tar.gz
ln -s tipsy-$tipsy tipsy
pushd tipsy-$tipsy/code >& /dev/null
  set log=$root/tmp/tipsy.log
  mkdir -p $root/opt/bin
  ./configure --prefix=$root/opt >& $log
  make >>& $log
  make install prefix=$root/opt >>& $log
popd >& /dev/null
rehash

cosmics:
#                                                    cosmics
echo  `date` Installing cosmics
tar zxf $pkg/grafic/cosmics-$cosmics.tar.gz
pushd cosmics-$cosmics  >& /dev/null
  set log=$root/tmp/cosmics.log
  mkdir -p $root/opt/bin
  #  note we're hardcoding g77 here, should check the real name of the compiler!!!
  make linux F77=g77 >>& $log
  cp bin/* $root/opt/bin >>& $log
popd >& /dev/null
rehash
  

gadget:
#                                                    gadget
echo  `date` Installing gadget
mkdir gadget
pushd gadget >& /dev/null
  set log=$root/tmp/gadget.log 
  $NEMO/src/scripts/gadget.install prefix=$MANYBODY/opt bench=0 manybody=$pkg >>& $log
popd >& /dev/null
rehash


ruby:
#                                                   ruby
echo  `date` Installing ruby
tar zxf $pkg/ruby/ruby-$ruby.tar.gz
pushd ruby-$ruby >& /dev/null
  set log=$root/tmp/ruby.log
  ./configure --prefix=$root/opt   >& $log
  make                            >>& $log
  make install                    >>& $log
popd >& /dev/null
rehash

acs:
#                                                   acs-lite
echo  `date` Installing acs
tar zxf $pkg/acs/acs-$acs.tgz
pushd acs >& /dev/null
  tar xf $pkg/acs/acscodes.tar
  set log=$root/tmp/acs.log
  setenv ACSROOT `pwd`
  source $ACSROOT/bin/acsstart.cshrc
  make acscode >& $log
  if (0) then
    # the long long path to make documentation
    # also assumes we have a local ruby
    cd $ACSROOT/src/rdoc*
    ruby install.rb
    cd $ACSROOT
    make
  endif
popd >& /dev/null
rehash

echo "***"
echo "***"
echo "***"
echo "         the GH2006 part of manybody is now complete, you could interrupt with ^C now"
echo "***"
echo "***"
echo "***"

starlab:
#                                                   starlab (+cvs)
echo  `date` Installing starlab
tar zxf $pkg/starlab/starlab_cvs.tar.gz
ln -s starlab_cvs starlab
pushd starlab_cvs >& /dev/null
  set log=$root/tmp/starlab.log
  set opts=(`pwd`/usr --without-f77 --enable-sdyn3 --enable-sdyn)
  if ($cvs) cvs up >>& $log
  ./install-starlab $opts >>& $log
  echo "See also install.log in the starlab_cvs directory" >> $log
  if (-e starlab_start.csh) then
    source starlab_start.csh
    echo STARLAB-COUNT: `ls $STARLAB_INSTALL_PATH/bin|wc -l`
  else
    echo STARLAB: failed to build properly
  endif
popd >& /dev/null
rehash

#                                                   starcluster
if (0) then
echo  `date` Installing starcluster
tar zxf $pkg/starcluster/starcluster_cvs.tar.gz
ln -s starcluster_cvs starcluster
pushd starcluster_cvs >& /dev/null
  set log=$root/tmp/starcluster.log
  if ($cvs) cvs up >>& $log
  make >>& $log
  if ($status) echo ERRORS compiling starcluster, check $log
  make install prefix=$STARLAB_INSTALL_PATH >>& $log
  if ($status) echo ERRORS installing starcluster, check $log
  echo STARLAB+CLUSTER-COUNT: `ls $STARLAB_INSTALL_PATH/bin|wc -l`
popd >& /dev/null
rehash
endif

partiview:
#                                                   fltk
echo  `date` Installing fltk 
tar zxf $pkg/fltk/fltk-$fltk-source.tar.gz
pushd fltk-$fltk >& /dev/null
  set log=$root/tmp/fltk.log
  ./configure --prefix=$root/opt >& $log
  make >>& $log
  if ($status) echo ERRORS compiling fltk, check $log
  make install >>& $log
  if ($status) echo ERRORS installing fltk, check $log
popd >& /dev/null
rehash

#                                                   partiview
echo  `date` Installing partiview
tar zxf $pkg/partiview/partiview_cvs.tar.gz
ln -s partiview_cvs partiview
pushd partiview_cvs/src >& /dev/null
  set log=$root/tmp/partiview.log 
  if ($cvs) cvs up >& $log
  ./configure --with-fltk=$root/opt >>& $log
  make >>& $log
  if ($status) then
    echo ERRORS compiling partiview, check $log
  else
    cp partiview $MANYBODY/opt/bin
  endif
popd >& /dev/null

gsl:
#                                                   GSL
echo  `date` Installing gsl
tar zxf $pkg/gsl/gsl-$gsl.tar.gz
pushd gsl-$gsl >& /dev/null
  set log=$root/tmp/gsl.log 
  ./configure --prefix=$MANYBODY/opt  >& $log
  if ($status) echo ERRORS 1 compiling gsl, check $log
  make >>& $log
  if ($status) echo ERRORS 2 compiling gsl, check $log
  make install >>& $log
  if ($status) echo ERRORS 3 compiling gsl, check $log
popd >& /dev/null
rehash

#                                                   fewbody, glstarview (source in NEMO)
echo  `date` Installing fewbody,glstarview
pushd $NEMO/usr/fregeau >& /dev/null
  set log=$root/tmp/fewbody.log 
  tar zxf $pkg/fregeau/fewbody-$fewbody.tar.gz
  pushd fewbody-$fewbody >& /dev/null
    make all  \
	CFLAGS="-I$MANYBODY/opt/include -Wall -O3" \
	LIBFLAGS="-L$MANYBODY/opt/lib -lgsl -lgslcblas -lm" >& $log
    if ($status) echo ERRORS compiling fewbody, check $log
    cp cluster triplebin binbin binsingle $NEMOBIN
  popd >& /dev/null
  tar zxf $pkg/fregeau/glstarview-$glstarview.tar.gz
  pushd glstarview-$glstarview >& /dev/null
    make -f Makefile.rh9 all >>& $log
    if ($status) echo ERRORS compiling glstarview, check $log
    cp glstarview $NEMOBIN
  popd >& /dev/null
popd >& /dev/null
rehash

#                                                   HNbody (binary only)
if ($sys == linux) then
echo  `date` Installing hnbody- for linux only
pushd $NEMO/usr >& /dev/null
  tar zxf $pkg/hnbody/hnbody-$hnbody.tgz
  ln -s hnbody-$hnbody hnbody
  foreach bin (ems  hnarith  hnbody  hnconvert  hnpaste  hnstats  hntac  lunar)
    mv hnbody/bin/$bin $NEMOBIN
  end
popd >& /dev/null
rehash
endif

ez:
#                                                       EZ
echo  `date` Installing EZ
  set log=$root/tmp/ez.log 
  tar zxf $pkg/EZ/EZ_$ez.tar.gz
  pushd EZ_$ez >& /dev/null
    if (0) then
       ./QUICK_INSTALL >& $log
    else
       # use: intel: ifort -w -132
       # or:  gnu:   g95/f95 -ffixed-line-length-132
       which ifort >& status
       if ($status) then
         which f95 >& status
	 if ($status) then
	    which g95 >& status
	    if ($status) then
	      echo "ERROR: no known fortran compiler for EZ available (ifort, f95 g95)"
	    else
	       echo EZ: using g95
	       (cd make; make EZ FC="g95 -ffixed-line-length-132") >& $log
	       if ($status) echo ERRORS compiling EZ, check $log
	    endif
	 else
	     echo EZ: using f95
            (cd make; make EZ FC="f95 -ffixed-line-length-132") >& $log
            if ($status) echo ERRORS compiling EZ, check $log
	 endif
       else
	  echo EZ: using ifort
          (cd make; make EZ FC="ifort -w -132") >& $log
          if ($status) echo ERRORS compiling EZ, check $log
	  # running example bit expensive in time
	  if (0) then
	    (cd run; ./EZ) >>& $log
	  endif
       endif
    endif
  popd >& /dev/null

#                                                                      Tioga
echo  `date` Installing Tioga
  set log=$root/tmp/tioga.log
  tar zxf $pkg/Tioga/tioga-$tioga.tar.gz
  pushd tioga-$tioga >& /dev/null
    (cd split; ruby extconf.rb ; make; make install) >& $log
    (cd tests; ruby ts_Tioga.rb) >>& $log
  popd >& /dev/null
rehash

#                                                                      MMAS
echo  `date` Installing MMAS
  set log=$root/tmp/mmas.log
  tar zxf $pkg/mmas/mmas1.6.tar.gz 
  pushd mmas1.6 >& /dev/null
    make >& $log
    if ($status) echo ERRORS compiling mmas, check $log
  popd >& /dev/null
rehash

#                                                          FFTW v2, needed for starcrash (but now installed via gadget)
if (0) then
echo  `date` Installing FFTW
  set log=$root/tmp/fftw.log
  tar zxf $pkg/fftw/fftw-2.1.5.tar.gz
  pushd fftw-2.1.5 >& /dev/null
    ./configure --prefix=$root/opt   >& $log
    make >>& $log
    if ($status) echo ERRORS compiling fftw, check $log
    make install >>& $log
    if ($status) echo ERRORS installing fftw, check $log
  popd >& /dev/null
rehash
else
  # StarCrash depends on this version, see FFTDIR= below
  ln -s gadget/fftw-2.1.5
endif

#                                                                   StarCrash
echo  `date` Instaling StarCrash
  set log=$root/tmp/starcrash.log
  tar zxf $pkg/StarCrash/starcrash_serial.tar.gz 
  pushd serial >& /dev/null
    make testinput b2pos b2pos2 sphagr \
	FFTDIR=$root/fftw-2.1.5  FC=g77 CC=gcc  >& $log
    if ($status) echo ERRORS compiling starcrash, check $log
  popd >& /dev/null
rehash

end:
  chmod -R +r $manybody
# -- end of install --
echo  `date` Done.
