# -*-shell-script-*-s
# Prerequisites: sed, GNU make, libtool, wget
#
# (and for autogening: GNU m4, GNU autoconf, GNU automake)
#
# libz is optional for the moment
#
script_version=1.1
# Set the host and the OS.
# Fairly fundamental.
#
OS=`uname`
HOST=`hostname`


# This is where the compiled binaries/libraries/data get installed:
#
AUTOBUILD_INSTALLED=$HOME/coot/autobuild/${OS}-hercules
# or perhaps, for the adventurous:
#AUTOBUILD_INSTALLED=$CCP4_MASTER

# This is where the actual build (compilation) master directory is:
# a temporary or scratch directory would be sensible.
# 
# AUTOBUILD_BUILD=$HOME/autobuild
AUTOBUILD_BUILD=$HOME/coot/autobuild


# use the right (GNU) tar and provide the path to wget (needed
# for downloads) and to GNU make - (which is necesary for python
# at least).
#
PATH=$PATH:$AUTOBUILD_INSTALLED/bin
PATH=$PATH:/usr/local/bin:/usr/sbin:/usr/bsd:/sbin:/usr/bin:/bin
PATH=$PATH:/etc:/usr/etc
export PATH

# This is where the build logs go:
#
HTMLLOGS=$HOME/coot/autobuild/build-logs/${OS}-hercules
#
# Make sure that that directory exists, if it doesn't, then make it.
# This should be a function. And/or use mkdir -p.
# 
# A better way would be some clever use of "dirname" so that
# we don't have to alter this when we change the above directory.
# Fix it next round.
#
#
#if [ ! -e $HOME/public_html ] ; then
	#mkdir  $HOME/public_html
#fi
#if [ ! -e $HOME/public_html/build-logs ] ; then
	#mkdir  $HOME/public_html/build-logs
#fi
#if [ ! -e $HOME/public_html/build-logs/${OS}-${HOST} ] ; then
	#mkdir  $HOME/public_html/build-logs/${OS}-${HOST}
#fi
 
mkdir -p $HTMLLOGS


# get build specials, e.g. change the compiler or the compiler
# options (e.g build for debugging), extra libraries etc
# setup LD_LIBRARY_DIR (or whatever) to include the autobuild 
# library dir so 
# that intermediate (configure compile) programs run
# and the addition to the path of GNU make and wget.
# 
# use this to specify config_extras
# 
# options are: GL_prefix, e.g. SunOS has Mesa Graphics - this is 
# where to find them.o
#              glut_prefix, optionally can use freeglut
#
specs=$HOME/coot/hercules-specials

# GLUT:  Where is glut?  Could be anywhere. 
# For example, on dogtanian and bubbles it's in /usr, on batman 
# it in AUTOBUILD_INSTALLED.
# 
# It is a build special.

# glut_prefix="--with-glut-prefix=$AUTOBUILD_INSTALLED"

# Do we want CCTBX? By default we don't - so we can ignore this:
#
# Which version of cctbx do we want?
# We set CVS_CCTBX when we want to use the cvs version, otherwise
# we use the download version.  If set, it is set to the value 
# of the directory which contains cctbx, i.e. the directory
# you did the cvs checkout in.
# 
# old stuff
# CVS_CCTBX=$HOME/autobuild
#

# make nightly binary tarballs?
NIGHTLY_DEST_DIR=/y/people/emsley/public_html/software/binaries/nightlies
do_nightlies=0

# local tinkering because our sgi runs out of room on /tmp on compiling
if [ $HOST = batman ] ; then
   TMPDIR=/y/work/emsley/tmp
   export TMPDIR
fi

# ----------------------------------------------------------------
#   End of tinkering with parameters.
# ----------------------------------------------------------------

# latest version (without .tar.gz extension)
coot_version=coot-0.0.0

shared_static_flag="--disable-shared"

# malloc.h business, Darwin does not have malloc.h
# used in the gtk_canvas build
have_malloc=1
if test "$OS" = "Darwin" ; then
   have_malloc=0
   fix_ulong=1
   shared_static_flag="--disable-static"
fi

# redirect the output.
#
exec > $HTMLLOGS/$HOST.log  2> $HTMLLOGS/$HOST.err

echo This is script version $script_version

date
#
if [ -e $specs ] ; then
	echo running these extras:
	echo . --------------------------------
	cat $specs
	. $specs
	echo . --------------------------------
fi

echo INFO:: glut_prefix is $glut_prefix

#initially unset:
systype=unknown 

if test $OS = Linux ; then 
   if test -e /etc/redhat-release ; then 
      grep 7.2 /etc/redhat-release
      status=$?
      if test $status = 0 ; then
         systype=Red-Hat-Linux-7.2
      fi
      grep 8.0 /etc/redhat-release
      status=$?
      if test $status = 0 ; then
         systype=Red-Hat-Linux-8
      fi
      grep 9 /etc/redhat-release
      status=$?
      if test $status = 0 ; then
         systype=Red-Hat-Linux-9
      fi
   fi
   if test -e /etc/fedora-release ; then 
      grep "release 1" /etc/fedora-release
      status=$?
      if test $status = 0 ; then
         systype=Fedora-Core-1
      fi
      grep "release 2" /etc/fedora-release
      status=$?
      if test $status = 0 ; then
         systype=Fedora-Core-2
      fi
   fi
fi

if test $OS = Darwin ; then
   systype=MacOSX
fi

if test $OS = IRIX64 ; then
   osversion=`uname -r`
   systype=${osversion}-sgi
fi

if test $OS = IRIX ; then
   osversion=`uname -r`
   systype=${osversion}-sgi
fi

echo systype: $systype

# set the build prefix for installed stuff.
#
generic_prefix="--prefix=$AUTOBUILD_INSTALLED ${config_extras:-}"
scons_prefix="prefix=$AUTOBUILD_INSTALLED"

# give us some diagnostic shell information:
# (what extra things did $specs give us?)
#
set


# We want to add some compiler info in the directory name.
# If CC is set, use that,
# If not, try gcc, 
# if not, try cc
#
# Similarly for the c++ compiler info
# If CXX is set, use that
# if not, try g++
# if not, try c++

if [ -n "$CC" ] ; then
	# CC was set
	v=$CC-`$CC --version`
	if [ $? -ne 0 ] ; then
	   # but not gcc 
	   v=$CC-`$CC -v`
	      if [ $? -ne 0 ] ; then
	    	 v="missing_version"
	      fi
	fi
else
	# CC not set
	v=gcc-`gcc --version | head -1`
        if [ $? -ne 0 ] ; then
		# not gcc
	        # try cc
		v=cc-`cc -v`
		if [ $? -ne 0 ] ; then
		   v="missing_version"
		fi
	fi
fi


if [ -n "$CXX" ] ; then
	# CXX was set
	w=$CXX-`$CXX --version`
	if [ $? -ne 0 ] ; then
	   # but not g++ 
	   w=$CXX-`$CXX -v`
	      if [ $? -ne 0 ] ; then
	    	 w="missing_cxx_version"
	      fi
	fi
else
	# CXX not set
	w=g++-`g++ --version | head -1`
        if [ $? -ne 0 ] ; then
		# not g++
	        # try c++
		w=c++-`c++ -v`
		if [ $? -ne 0 ] ; then
		   # try CC
		   w=CC-`CC -v`
		   if [ $? -ne 0 ] ; then
		      w="missing_c++_version"
		   fi
		fi
	fi
fi


# so now we have v and w set to something.  We 
# need to sanitize that something.  
v_fid=`echo $v | sed 's/ /_/g'`
w_fid=`echo $w | sed 's/ /_/g'`

compilers_dir=${v_fid}_and_${w_fid}
HTMLLOGDIR=$HTMLLOGS/$compilers_dir
if [ ! -e $HTMLLOGDIR ] ; then 
  mkdir $HTMLLOGDIR
  if [ $? -ne 0 ] ; then
    #
    echo DISASTER: could not make directory $HTMLLOGDIR
    echo exitting.
    exit 2
  fi
fi



# So that python is found when it is installed:
#
PATH=$AUTOBUILD_INSTALLED/bin:$PATH
export PATH

echo PATH is now: $PATH



echo testing for "$AUTOBUILD_INSTALLED"
if [ ! -e "$AUTOBUILD_INSTALLED" ] ; then
	echo $AUTOBUILD_INSTALLED does not exist
	echo making directory $AUTOBUILD_INSTALLED
	mkdir $AUTOBUILD_INSTALLED
	if [ $? -ne 0 ] ; then
		echo DISASTER: $AUTOBUILD_INSTALLED does not exist and no way
		echo to make it.
	        exit 2
	fi
else
	echo INFO:: $AUTOBUILD_INSTALLED exists already.
fi
echo done testing for $AUTOBUILD_INSTALLED

if [ ! -e "$AUTOBUILD_INSTALLED/lib" ] ; then
	echo making "$AUTOBUILD_INSTALLED/lib"
	mkdir "$AUTOBUILD_INSTALLED/lib"
        if [ $? -ne 0 ] ; then
                echo DISASTER: $AUTOBUILD_INSTALLED/lib cannot be created
                echo to make it.
                exit 2
        fi
fi


# need to add test that make is GNU make
make --version
if [ $? -ne 0 ] ; then
   # definately not GNU make
   echo Ooops.  Your make '(' `which make` ')' is not GNU make. 
   echo Exitting now.
   exit 2
fi

if [ ! -e "$AUTOBUILD_BUILD" ] ; then
   mkdir "$AUTOBUILD_BUILD" 
   if [ $? -ne 0 ] ; then
	echo mkdir "$AUTOBUILD_BUILD" failed.
        exit 2
   fi
fi
   

cd $AUTOBUILD_BUILD
if [ $? -ne 0 ] ; then
	echo cd $HOME/autobuild failed for some reason. Now in `pwd`
fi
date=`date | sed  's/[ :]/-/g'`
echo Date: $date

# now in $HOME/autobuild
mkdir ${HOST}_$date
if [ $? -ne 0 ] ; then
	echo mkdir  ${HOST}_$date failed for some reason. Now in `pwd`
fi
cd ${HOST}_$date
if [ $? -ne 0 ] ; then
	echo cd ${HOST}_$date failed for some reason. Now in `pwd`
fi

echo checking wget
which wget
wget  "http://www.chem.gla.ac.uk/~paule/crystal/mmdb-july-2001-tmp.tar.gz" 
if [ $? -ne 0 ] ; then
	echo wget failed for some reason
fi
echo Done wget check.


wget -O index.html -o wget-e.s.log http://www.ysbl.york.ac.uk/~emsley/software/

coot_version_tmp=`egrep href index.html | sed -e 's/.*">//' -e 's/<.*//' | awk '
BEGIN {bmajor = -1; bminor = -1; bmicro = -1; }
# /^coot-[0-9]\.[0-9]+\.[0-9]+.*\.tar\.gz/ { 
/^coot-[0-9]\.[0-9]+.*\.tar\.gz$/ { 
    n = split($1,arr,"[-.]"); 
    # print n,"parts";
    # print arr[1], arr[2], arr[3], arr[4];
    # print arr[5], arr[6], arr[7], arr[8];
    major = arr[2] + 0;
    minor = arr[3] + 0;
    micro = arr[4] + 0;
    if (major >= bmajor) {
       if (minor >= bminor) { 
          if (micro >= bmicro) { 
	     file = $1; 
             bmajor = major;
             bminor = minor;
             bmicro = micro;
	  }
       }
    }
}

END {
    gsub("\.tar\.gz.*", "", file);
    print file;  # which is a version now.
}
'`

if [ -z "$coot_version_tmp" ] ; then
    echo clever coot_version extraction failed using default.
else 
    coot_version=$coot_version_tmp
    echo setting coot_version to $coot_version from extraction
fi

# are we doing a pre-release in York?
in_york_pre=0
domainname=`domainname`
coot_prefix=$generic_prefix
if [ $domainname = yorksbl ] ; then
   # we are in York, let's try the pre-release coot...
   wget -O index.html -o wget-e.s.p.log http://www.ysbl.york.ac.uk/~emsley/software/pre-release
   coot_version_pre=`egrep href index.html | sed -e 's/.*">//' -e 's/<.*//' | awk ' /^coot-[0-9]\.[0-9]+.*-pre.*\.tar\.gz$/ { 
   last=$0 }
END {
   print last
}
'`
   echo debug: in York and coot_version_pre is $coot_version_pre
   if [ -z "$coot_version_pre" ] ; then
      echo In York, but failed to find pre-release version
   else
      in_york_pre=1
      # tinker with coot_prefix, it's generic_prefix with pre-release added to prefix dir
      coot_prefix="--prefix=${AUTOBUILD_INSTALLED}-pre-release ${config_extras:-}"
      coot_source_tar_file=http://www.ysbl.york.ac.uk/~emsley/software/pre-release/$coot_version_pre
      coot_version=`echo $coot_version_pre | sed s/\.tar.gz//`
   fi
fi


# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
#                                which components?
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
#
# Often there are components provided by the operating system or optional 
# installs thereof.
#

# Where might the "system" components be?
#
non_standard_dir_list="$AUTOBUILD_INSTALLED"
dir_list="$non_standard_dir_list"

$AUTOBUILD_INSTALLED/bin/glib-config --prefix
if test $? != 0 ; then
   echo glib should be build
   build_glib=1
else 
   echo system has glib    
fi

$AUTOBUILD_INSTALLED/bin/gtk-config --prefix
if test $? != 0 ; then
   echo gtk should be build
   build_gtk=1
else 
   echo system has gtk
fi

# Darwin currently has guile-1.6 and guile (which is an older version)
# we want to use guile-1.6 if it exists.

$AUTOBUILD_INSTALLED/bin/guile-1.6-config --version
if test $? = 0 ; then
   rm -f $AUTOBUILD_INSTALLED/bin/guile
   rm -f $AUTOBUILD_INSTALLED/bin/guile-config
   ln -s /sw/bin/guile-1.6        $AUTOBUILD_INSTALLED/bin/guile
   ln -s /sw/bin/guile-1.6-config $AUTOBUILD_INSTALLED/bin/guile-config
   ln -s /sw/bin/guile-1.6-snarf  $AUTOBUILD_INSTALLED/bin/guile-snarf
fi

#echo guile-config version:
#which guile-config

$AUTOBUILD_INSTALLED/bin/guile-config --version
if test $? != 0 ; then
   echo guile should be build
   build_guile=1
else 
   echo system has guile
fi

python -V
if test $? != 0 ; then
   echo python should be build
   build_python=1
else 
   echo system has python
fi

$AUTOBUILD_INSTALLED/bin/gsl-config --prefix
if test $? != 0 ; then
   echo gsl should be build
   build_gsl=1
else 
   gsl_version=`gsl-config --version`

   needed_gsl_version=1.3

   build_gsl=`awk -v A=$gsl_version -v N=$needed_gsl_version 'BEGIN{x = (A+0)>(N+0) ? 0 : 1; print x}'`
   echo system has the GSL version $gsl_version
   echo build_gsl is $build_gsl

   if [ $build_gsl = 1 ] ; then
	echo need to build GSL
   else 
	echo GSL is up to date
   fi
fi

$AUTOBUILD_INSTALLED/bin/imlib-config --version
if test $? != 0 ; then
   echo imlib should be built
   build_imlib=1
else 
   echo system has imlib
fi

# gtk canvas
for dir in $dir_list
do
   if test -e $dir/include/gtk-canvas.h ; then
      have_canvas_h=1
   fi
done
if test -z "$have_canvas_h" ; then
   echo gtk-canvas should be build
   build_gtk_canvas=1
else 
   echo system has gtk-canvas
fi

# guile-gtk
for dir in $dir_list
do
   if test -e $dir/include/guile-gtk.h ; then
      have_guile_gtk=1
   fi
done
if test -z "$have_guile_gtk" ; then
   echo guile-gtk should be build
   build_guile_gtk=1
else 
   echo system has guile-gtk
fi

# clipper
for dir in $dir_list
do
   if test -e $dir/include/clipper/core/coords.h ; then
      echo NOT building clipper
   else
      echo CLIPPER should be built
      build_clipper=1
   fi
done

# mmdb
for dir in $dir_list
do
   if test -e $dir/include/mmdb.h ; then
      echo NOT building mmdb
   else
      echo MMDB should be built
      build_mmdb=1
   fi
done

#ssmlib
for dir in $dir_list
do
   if test -e $dir/include/ssm_align.h ; then
      echo NOT building SSMlib
   else
      echo SSMlib should be built
      build_ssm=1
   fi
done

#fftw
for dir in $dir_list
do
    if test -e $dir/include/fftw.h ; then
	echo NOT building fftw
    else
	echo fftw will be built
	build_fftw=1
    fi
done

#mccp4
for dir in $dir_list
do
    if test -e $dir/include/ccp4_array.h ; then
	echo NOT building mccp4
    else
	echo mccp4 should be built
	build_mccp4=1
    fi
done

#guile_www
for dir in $dir_list
do
    if test -e $dir/share/guile/www/url.scm ; then
	echo NOT building guile-www
    else
	echo guile_www should be built
	build_guile_www=1
    fi
done

#guile-gui
for dir in $dir_list
do
    if test -e $dir/share/guile/gui/event-loop.scm ; then
	echo NOT building guile-gui
    else
	echo guile-gui should be built
	build_guile-gui=1
    fi
done

#goosh
for dir in $dir_list
do
    if test -e $dir/share/guile/site/goosh.scm ; then
	echo NOT building goosh
    else
	echo goosh should be built
	build_goosh=1
    fi
done

# gtkglarea
for dir in $dir_list
do
   if test -e $dir/include/gtkgl/gtkglarea.h ; then
      have_gtkglarea=1
   fi
done

gtkglarea_prefix=""
for dir in $non_standard_dir_list
do
   if test -e $dir/include/gtkgl/gtkglarea.h ; then
      gtkglarea_prefix="--with-gtkgl-prefix=$dir"
   fi
done

echo gtkglarea_prefix is $gtkglarea_prefix
if test -z "$have_gtkglarea" ; then
   echo gtkglarea should be build
   build_gtkglarea=1
else 
   echo system has gtkglarea
fi

# build_glib=0
# build_gtk=0
# build_guile=0
# build_python=0
# build_gsl=0
# build_gtk_canvas=0
# build_imlib=0
# build_gtkglarea=0

# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
#                                build components
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
#
#
# mmdb
#
if test -n "$build_mmdb" ; then
    echo BUILDING mmdb:
#
    wget http://www.ysbl.york.ac.uk/~emsley/software/mmdb-1.0.6-ysbl.tar.gz
    (
    date
    gzip -dc mmdb-1.0.6-ysbl.tar.gz | tar xf -
    cd  mmdb-1.0.6-ysbl
    ./configure $generic_prefix
    make
    make install
    ) > $HTMLLOGDIR/00-mmdb.txt 2>&1
fi


#
# wget  http://www.ysbl.york.ac.uk/~emsley/software/extras/SSMlib-0.0-pre-1-ysbl.tar.gz
if test -n "$build_ssm" ; then
    echo BUILDING SSMlib:
    wget  http://www.ysbl.york.ac.uk/~emsley/software/extras/SSMlib-0.0-pre-1.tar.gz
    (
    date
    gzip -dc SSMlib-0.0-pre-1.tar.gz | tar xf -
    cd SSMlib-0.0-pre-1
    ./configure $generic_prefix --with-mmdb-prefix=$AUTOBUILD_INSTALLED
    make
    make install
    ) >  $HTMLLOGDIR/00-ssmlib.txt 2>&1
fi

# root PACKAGES: /y/people/emsley/autobuild/batman_Fri-Aug-17-03:51:01-BST-2001/devel-cctbx



# fftw
if test -n "$build_fftw" ; then
    echo BUILDING fftw:
#
    wget http://www.ysbl.york.ac.uk/~emsley/software/fftw-2.1.3.tar.gz
    wget http://www.ysbl.york.ac.uk/~emsley/software/extras/fftw-configure-stuff.tar.gz
    (
    date
    pwd
    gzip -dc fftw-2.1.3.tar.gz | tar xf -
    gzip -dc fftw-configure-stuff.tar.gz | tar xf -
    cd fftw-2.1.3
    ./configure $generic_prefix --enable-shared --enable-float
    make
    make install
    )  > $HTMLLOGDIR/01-fftw.txt 2>&1
fi

# glib
#
if test -n "$build_glib" ; then 
    echo BUILDING glib:
#
    wget http://www.mirror.ac.uk/sites/ftp.gimp.org/pub/gtk/v1.2/glib-1.2.10.tar.gz
    (
    date
    pwd
    gzip -dc glib-1.2.10.tar.gz | tar xf -
    cd glib-1.2.10
    ./configure $generic_prefix
    make
    make install
    )  > $HTMLLOGDIR/02-glib.txt 2>&1
    else
	echo not building glib:
fi


#
# Note that gtk+ depends on glib, so we need to tell gtk+ where the 
# version of glib (that we just built) has been installed.
# So, we tinker with the configure prefixes and also need to add
# the location of the shared lib to LD_LIBRARY_PATH.  We add at the 
# beginning so that it finds our new lib first.
#
#
glib_extras=--with-glib-prefix=$AUTOBUILD_INSTALLED
gtk_prefixes="$generic_prefix $glib_extras"
if [ -z "$LD_LIBRARY_PATH" ] ; then
	LD_LIBRARY_PATH=$AUTOBUILD_INSTALLED/lib
else
        LD_LIBRARY_PATH=$AUTOBUILD_INSTALLED/lib:$LD_LIBRARY_PATH
fi
export LD_LIBRARY_PATH
#
# IRIX64 (on batman at least) does not seem to use LD_LIBRARY_PATH, 
# it uses, instead, LD_LIBRARYN32_PATH.  So we set that for all
# systems, hoping that it won't do any harm on other systems.
#
#
LD_LIBRARYN32_PATH=$LD_LIBRARY_PATH
export LD_LIBRARYN32_PATH


# gtk+
#
if test -n "$build_gtk" ; then 
    echo BUILDING gtk+:
#
    wget http://www.mirror.ac.uk/sites/ftp.gimp.org/pub/gtk/v1.2/gtk+-1.2.10.tar.gz
    (
    date
    pwd
    gzip -dc gtk+-1.2.10.tar.gz | tar xf -
    cd gtk+-1.2.10
    echo Using LD_LIBRARY_PATH $LD_LIBRARY_PATH
    echo Using LD_LIBRARYN32_PATH $LD_LIBRARYN32_PATH
    echo ./configure $gtk_prefixes
    ./configure $gtk_prefixes
    make
    make install
    )  > $HTMLLOGDIR/03-gtk+.txt 2>&1
    else
	echo not building gtk+:
fi


# mccp4:
#
if test -n "$build_mccp4" ; then
    echo BUILDING mccp4.latest.tar.gz
    wget http://www.ysbl.york.ac.uk/~emsley/software/extras/mccp4-0.7.1.tar.gz
    (
    gzip -dc mccp4-0.7.1.tar.gz | tar xvf -
    cd mccp4-0.7.1
    ./configure $generic_prefix
    make
    make install
    echo 
    ) > $HTMLLOGDIR/04-mccp4.txt 2>&1
fi

# So that python is found when it is installed:
#
PATH=$AUTOBUILD_INSTALLED/bin:$PATH
export PATH

echo PATH is now: $PATH

python_version=python2.2


# python
#
if test -n "$build_python" ; then 
    echo BUILDING ${python_version}:
#
# wget http://www.python.org/ftp/python/2.1.1/Python-2.1.1.tgz
# wget http://www.python.org/ftp/python/2.0.1/Python-2.0.1.tgz
# wget http://www.mirror.ac.uk/sites/ftp.python.org/pub/python/src/python-2.0c1.tar.gz
# wget http://www.mirror.ac.uk/sites/ftp.python.org/pub/python/2.2.3/Python-2.2.3.tgz
    wget http://mirror.ac.uk/mirror/ftp.python.org/pub/www.python.org/ftp/python/2.3.4/Python-2.3.4.tgz
    (
    date
    #gzip -dc Python-2.0.1.tgz | tar xf -
    #gzip -dc python-2.0c1.tgz | tar xf -
    gzip -dc Python-2.2.3.tgz | tar xf -
    
    # cd Python-2.1.1
    # cd Python-2.0.1
    cd Python-2.2.3
    
    echo INFO:: ./configure $generic_prefix $python_spec_flags for python
    ./configure $generic_prefix $python_spec_flags
    echo INFO:: make for python
    make 
    echo INFO:: make test for python
    make test
    echo INFO:: make install for python
    make install
    ) >  $HTMLLOGDIR/05-python.txt 2>&1

    # We need these otherwise python moans about not finding the libraries.
    #
    echo setting PYTHONHOME
    PYTHONHOME=$AUTOBUILD_INSTALLED
    export PYTHONHOME
    echo PYTHONHOME is now $PYTHONHOME
    
    echo post linking python to $python_version
    #
    if [ ! -e $AUTOBUILD_INSTALLED/bin/$python_version ] ; then
	echo Opps\!  $python_version does not exist.
	# only python dependencies from  now on
	echo exiting...
	exit
    fi
    if [ -e $AUTOBUILD_INSTALLED/bin/python ] ; then
	echo removing old python
	rm $AUTOBUILD_INSTALLED/bin/python
    fi
    current_dir=`pwd`
    cd $AUTOBUILD_INSTALLED/bin
    echo linking new...
    ln -s /$python_version python
    if [ $? -ne 0 ] ; then
	    echo Opps: failed: ln -s $AUTOBUILD_INSTALLED/bin/$python_version \
		                 $AUTOBUILD_INSTALLED/bin/python 
    fi
    cd $current_dir
    echo done python linking.
    # now we have "python" in our path - no need to rehash - 
    # clever old Bourne shell (or do I mean bash?).
else
    echo not building python:
fi

echo scons - placeholder only > $HTMLLOGDIR/06-scons.txt
# # scons
# echo BUILDING scons:
# wget http://www.ysbl.york.ac.uk/~emsley/software/scons-0.14.tar.gz
# (
# date
# pwd
# gzip -dc scons-0.14.tar.gz | tar xf -
# cd scons-0.14/
# python setup.py install
# ) > $HTMLLOGDIR/06-scons.txt 2>&1




# # clipper
# #
# echo BUILDING clipper:
# #
# # Note clipper (currently installs in current directory)
# #
# wget http://www.ysbl.york.ac.uk/~cowtan/clipper/clipper.latest.tar.gz
# (
# date
# pwd
# mkdir clipper
# cd   clipper
# gzip -dc ../clipper.latest.tar.gz | tar xf -
# pwd
# scons $scons_spec_flag with-mccp4=$AUTOBUILD_INSTALLED with-fftw=$AUTOBUILD_INSTALLED with-mmdb=$AUTOBUILD_INSTALLED $scons_prefix
# ) > $HTMLLOGDIR/07-clipper.txt 2>&1


# clipper: we use the ccp4 version of clipper now.
# (it uses configure, not scons)
#
if test -n "$build_clipper" ; then
	echo BUILDING clipper:
	wget http://www.ysbl.york.ac.uk/~emsley/software/extras/clipper.cvs-14-nov-2003.tar.gz
	wget http://www.ysbl.york.ac.uk/~emsley/software/extras/clipper-configure.patch
	wget http://www.ysbl.york.ac.uk/~emsley/software/extras/clipper_cif_data_io_importing.patch
	(
	date
	pwd
	gzip -dc clipper.cvs-14-nov-2003.tar.gz | tar xf -
	cd clipper
	pwd
	patch -p0 < ../clipper-configure.patch 
	patch -p0 < ../clipper_cif_data_io_importing.patch
	./configure --with-mccp4=$AUTOBUILD_INSTALLED --with-fftw=$AUTOBUILD_INSTALLED --with-mmdb=$AUTOBUILD_INSTALLED  --enable-mmdb --enable-mtz --enable-cif --enable-mmdbold --enable-minimol --enable-phs $generic_prefix CXXFLAGS="-g -O"
	make
	make install
	) >  $HTMLLOGDIR/07-clipper.txt 2>&1
fi

# Now we need:
#  * gtkglarea
#  * libart_lgpl
#  * gtkcanvas
#  * guile
#  * guile-www
#  * guile-gtk
#  * guile-gui
#  * goosh
#  * gsl
#build_imlib=1
if test -n "$build_imlib" ; then 
    echo BUILDING imlib
#
    wget http://www.ysbl.york.ac.uk/~emsley/software/extras/imlib-1.9.14.tar.gz
    (
    date
    pwd
    gzip -dc imlib-1.9.14.tar.gz | tar xf -
    cd imlib-1.9.14
    ./configure $generic_prefix
    make
    make install
    ) > $HTMLLOGDIR/08-pre-imlib.txt
else
    echo not building imlib:
fi

# build it anyway, don't use it sometimes?
if test -n "$build_gtkglarea" ; then 
   echo BUILDING gtkglarea:
#
    wget http://www.ysbl.york.ac.uk/~emsley/software/extras/gtkglarea-1.2.3.tar.gz
    ( 
    date
    pwd
    gzip -dc gtkglarea-1.2.3.tar.gz | tar xf -
    cd gtkglarea-1.2.3
    ./configure $generic_prefix
    make
    make install
    ) > $HTMLLOGDIR/08-gtkglarea.txt
else
    echo not building gtkglarea:
fi


if test -n "$build_gtk_canvas" ; then 
    echo BUILDING gtkcanvas:
#
    wget http://www.ysbl.york.ac.uk/~emsley/software/extras/gtk-canvas-0.1.1.tar.gz
    ( 
    date
    pwd
    gzip -dc gtk-canvas-0.1.1.tar.gz | tar xf -
    cd gtk-canvas-0.1.1
    ./configure $generic_prefix
    if test $have_malloc = 0 ; then
	sed 's/#include.*malloc.h.*//' gtk-canvas/gtk-canvas-load.c > tmp.out
	mv tmp.out gtk-canvas/gtk-canvas-load.c
    fi
    make
    echo 1 -------------------------------------------------------------------------
    echo '                                ' make install
    echo 1 -------------------------------------------------------------------------
    make install
    ) > $HTMLLOGDIR/09-gtk-canvas.txt
else
    echo not building gtkcanvas:
fi

if test -n "$build_guile" ; then 
    echo BUILDING guile:
#
    wget http://www.ysbl.york.ac.uk/~emsley/software/extras/guile-1.6.4.tar.gz
    ( 
    date
    pwd
    gzip -dc guile-1.6.4.tar.gz | tar xf -
    cd guile-1.6.4
    ./configure $generic_prefix
    make
    echo 1 -------------------------------------------------------------------------
    echo '                                ' make install
    echo 1 -------------------------------------------------------------------------
    make install
    ) > $HTMLLOGDIR/10-guile.txt 2>&1
else
    echo not building guile:
fi

if test -n "$build_guile_www" ; then
    echo BUILDING guile-www:
    #
    wget http://www.ysbl.york.ac.uk/~emsley/software/extras/guile-www-1.1.1ttn5.tar.gz
    ( 
    date
    pwd
    gzip -dc guile-www-1.1.1ttn5.tar.gz | tar xf -
    cd guile-www-1.1.1ttn5
    ./configure $generic_prefix
    make
    make install
    ) > $HTMLLOGDIR/11-guile-www.txt
else
    echo not building guile-www:
fi

if test -n "$build_guile_gtk" ; then 
    echo BUILDING guile-gtk:
#
    wget http://www.ysbl.york.ac.uk/~emsley/software/extras/guile-gtk-1.2-0.31.tar.gz
    ( 
    date
    pwd
    gzip -dc guile-gtk-1.2-0.31.tar.gz | tar xf -
    cd guile-gtk-1.2-0.31
    ./configure $generic_prefix
    # fix ulong if needed:
    sed 's/(ulong)/(unsigned long)/g' guile-gtk.c > tmp.c
    mv tmp.c guile-gtk.c
    make
    echo 1 -------------------------------------------------------------------------
    echo '                                ' make install
    echo 1 -------------------------------------------------------------------------
    make install
    ) > $HTMLLOGDIR/12-guile-gtk.txt
else
    echo not building guile-gtk:
fi

if test -n "$build_guild-gui" ; then
    echo BUILDING guile-gui:
    #
    wget http://www.ysbl.york.ac.uk/~emsley/software/extras/guile-gui-0.2.tar.gz
    ( 
    date
    pwd
    gzip -dc guile-gui-0.2.tar.gz | tar xf -
    cd guile-gui-0.2
    ./configure $generic_prefix
    make
    make install
    ) > $HTMLLOGDIR/13-guile-gui.txt
else
    echo not building guile-gui
fi

if test -n "$build_goosh" ; then
    echo BUILDING goosh:
    #
    wget http://www.ysbl.york.ac.uk/~emsley/software/extras/goosh-1.3.tar.gz
    ( 
    date
    pwd
    gzip -dc goosh-1.3.tar.gz | tar xf -
    cd goosh-1.3
    ./configure $generic_prefix
    make
    make install
    ) > $HTMLLOGDIR/14-goosh.txt
else
    echo not building goosh
fi

if test "$build_gsl" -eq 1 ; then 
    echo BUILDING gsl:
    #
    # wget http://www.mirror.ac.uk/sites/ftp.gnu.org/gnu/gsl/gsl-1.5.tar.gz
    wget ftp://ftp.funet.fi/pub/gnu/prep/gsl/gsl-1.5.tar.gz
    ( 
    date
    pwd
    gzip -dc gsl-1.5.tar.gz | tar xf -
    cd gsl-1.5
    ./configure $generic_prefix
    make
    make install
    ) > $HTMLLOGDIR/15-gsl.txt
else
    echo not building gsl
fi

echo BUILDING coot:
if [ $in_york_pre = 0 ] ; then 
   wget http://www.ysbl.york.ac.uk/~emsley/software/$coot_version.tar.gz 
   wget http://www.ysbl.york.ac.uk/~emsley/software/coot-static.patch
else 
   wget $coot_source_tar_file
   wget http://www.ysbl.york.ac.uk/~emsley/software/coot-static.patch
fi
# question: did we build gtkglarea?
#
(
echo using coot version $coot_version
ls $coot_version.tar.gz
date
pwd
gzip -dc $coot_version.tar.gz | tar xf -
cd $coot_version
if [ $shared_static_flag = --disable-shared ] ; then
    if [ $OS != IRIX64 ] ; then 
	echo patching for static build
	patch -p0 < ../coot-static.patch
    else 
        echo Not patching for static build on this sgi
    fi
else
	echo Not patching for static build
fi
./configure $coot_prefix \
   --with-gtkcanvas-prefix=$AUTOBUILD_INSTALLED \
        --with-mmdb-prefix=$AUTOBUILD_INSTALLED \
     --with-clipper-prefix=$AUTOBUILD_INSTALLED \
      --with-ssmlib-prefix=$AUTOBUILD_INSTALLED \
         --with-gsl-prefix=$AUTOBUILD_INSTALLED \
        --with-glib-prefix=$AUTOBUILD_INSTALLED \
         --with-gtk-prefix=$AUTOBUILD_INSTALLED \
       --with-gtkgl-prefix=$AUTOBUILD_INSTALLED \
       $gtkglarea_prefix \
       $glut_prefix  \
      --with-guile   \
      $shared_static_flag \
      CXXFLAGS="-g -O"
make -k
echo 1 -------------------------------------------------------------------------
echo '                                ' make install
echo 1 -------------------------------------------------------------------------
make install
cp ChangeLog $NIGHTLY_DEST_DIR
cp README    $NIGHTLY_DEST_DIR/coot-README
) > $HTMLLOGDIR/16-coot.txt 2>&1

#         --with-gsl-prefix=$AUTOBUILD_INSTALLED \
#        --with-glib-prefix=$AUTOBUILD_INSTALLED \
#         --with-gtk-prefix=$AUTOBUILD_INSTALLED \
#       --with-gtkgl-prefix=$AUTOBUILD_INSTALLED \


echo done coot.

if test "$do_nightlies" = "1" ; then 
   if test "in_york_pre" = "1" ; then 
      echo York pre-release - no tars for that.
   else 
      echo taring nightly... $NIGHTLY_DEST_DIR/${coot_version}-binary-${OS}-${systype}.tar.gz 
      cd $AUTOBUILD_INSTALLED/..
      tar czf $NIGHTLY_DEST_DIR/${coot_version}-binary-${OS}-${systype}.tar.gz `basename $AUTOBUILD_INSTALLED`
      md5sum $NIGHTLY_DEST_DIR/${coot_version}-binary-${OS}-${systype}.tar.gz > $NIGHTLY_DEST_DIR/${coot_version}-binary-${OS}-${systype}.tar.gz.md5sum
      echo done tar
   fi
else
   echo not doing nightlies: $do_nightlies
fi





