#! /bin/csh -f
#
#  Install GCC for NEMO ; from a source tree compilating and installation
#  would take about XXX mins on a 2.8 GHz P4.
#
#  For 'astromake', a more typical call would be:
#  
#  set version=4.2.3
#  ..../gcc.install src=gcc-$version  prefix=$ASTROMAKE/opt/gcc/$version version=$version fc=fortran
# 
#  For latest versions, see http://gcc.gnu.org/
#
#  new (4.4.0) versions require GMP 4.1+ and MPFR 2.3.2+. to build...????

if ($?NEMO == 0) set NEMO=`pwd`

#			 	.. command line keywords .. fc=f77 or f95 or fortran
set src=$NEMO/local/gcc
set prefix=$NEMO/opt
set version=3.3.5
set fc=f77

#				.. parse command line ..
foreach arg ($*)
   set check = `echo $argv[1] | awk -F= '{print NF}'`
   if ("$check" == 1) break
   echo Setting: $arg
   set $arg
end

set url=`printf ftp://ftp.gnu.org/pub/gnu/gcc/gcc-%s/gcc-%s.tar.bz2 $version $version`
echo URL=$url
set file=$url:t
set src1=$file:r
set src=$src1:r


if (! -e $src) then
  echo GCC source directory src=$src does not exist, try NEMO=
  echo or:
  echo " cd $NEMO/local"
  echo " wget $url"
  echo " tar jxf $file"
  echo " ../src/scripts/gcc.install version=$version"
  echo ""
  echo "Known keywords to this script (and their current defaults):"
  echo "  set src=$src"
  echo "  set prefix=$prefix"
  echo "  set version=$version"
  echo ""
  echo "After this is all installed, the path needs to be changed:"
  echo '  set path=($NEMO/opt/bin $path)'
  echo '  rehash'
  exit 1
endif


cd $src
mkdir build
cd build

echo Building in `pwd`, 
echo reporting the status of 4 steps as we go along.. Be patient.
echo Be sure to change your path and add $prefix/bin before your other path elements.

if ($version:r:r == 3 && $fc != f77) echo "Warning: gcc3 should use fc=f77, not $fc"
if ($version:r:r == 4) then
  # 4.0.x and 4.1.x use f95, 4.2.x use fortran 
  if ($version:r:e < 2 && $fc != f95) then
    echo "Warning: gcc4 should use fc=f95, not $fc"
  else
    echo "Warning: gcc4 should use fc=fortran, not $fc"
  endif
endif

../configure --prefix=$prefix --enable-threads --enable-languages=c,c++,$fc >& build1.log
echo status $status on build1.log

make bootstrap   >& build2.log
echo status $status on build2.log

make             >& build3.log
echo status $status on build3.log

make install     >& build4.log
echo status $status on build4.log

