#!/bin/csh -f

# Set the prefix, or use a default (option x or default).  Note that
# STARLAB_PATH must be set for the default option to work.

# Handy to keep this flag in case we want to force the build.
# A second argument also forces a build.

set force_build = 0

set flag = "default"
if ($#argv > 0) then

    if ("x$1" == "x-h" || "x$1" == "x--h" || "x$1" == "x--help") then
	echo Usage: install-autotools \[install-dir\] \[force-build\]
	exit
    endif

    set flag = $1
endif

if ($#argv > 1) set force_build = 1

if (x$flag == xx || x$flag == xdefault) then
    if ($?STARLAB_PATH == 0) then
	echo STARLAB_PATH not set
	exit
    endif
    set p = $STARLAB_PATH/usr
else
    set p = $flag
endif

set pre = "--prefix=$p"

# We will update the search path after each tool build, since
# automake and libtool installation may require the installed
# version of autoconf.  First add the target bin directory to
# the search path.

set path = ($p/bin $path)

echo package prefix flag is \"$pre\"

# (Re)build the autotools (isn't standardization wonderful?):

./clean-autotools

# See which tool packages exist.

set autoconf = (`find . -name autoconf\*.tar.gz | sed "s/.tar.gz//"`)
set automake = (`find . -name automake\*.tar.gz | sed "s/.tar.gz//"`)
set libtool  = (`find . -name libtool\*.tar.gz  | sed "s/.tar.gz//"`)

if ($#autoconf == 0 || $#automake == 0 || $#libtool == 0) then
    echo "Autotool package(s) not found."
    exit
endif

# Start by checking whether we should actually build the package.  Compare
# its version with the version number of any installed version of the tool.
# Previous version only built packages for which the installed version is
# older.  New version (7/05) builds all packages if any needs to be built
# to ensure a cosistent set of tools.

set build = $force_build

if ($build == 0) then

  foreach dir ($autoconf $automake $libtool)

#   Start by checking whether we should actually build the package.  Compare
#   its version with the version number of any installed version of the tool.

    set buildtool = 1

#   Determine the tool name and version from dir.  We assume that the format
#   here will continue to be standard terminology -- NEED TO MONITOR THIS.

    set tmp = (`echo $dir:t | tr '-' ' '`)
    if ($#tmp == 2) then

	set tool = $tmp[1]
	set version = $tmp[2]

	echo version of $tool tarball is $version

#	See if a more up-to-date version of the tool already exists.

	set v = unknown
	set tmp = `which $tool | grep not\ found`
	if ($#tmp == 0) then

#	    Tool exists on the current search path.  Check its version.
#	    Note that, for automake at least, even --version can fail if
#	    the tool is relocated, as the location of its configuration
#	    file is hardcoded...

	    alias t $tool
	    set tmp = (`t --version | grep $tool`)

#	    The following is very specific to the current format of the
#	     --version output.  NEED TO MONITOR THIS.

	    if ($#tmp > 3) then

		set v = $tmp[4]

#	 	Local package version is version, existing version is v.
#	    	Test whether we should supercede the installed version.
#	    	Assume that a simple lexical comparison will suffice.

#	    	(Don't split this command -- some shells don't like it...)

		set buildtool = \
		    `echo $version | awk -v v=$v '{print ($1 > v ? 1 : 0);}'`

	    endif
	endif

	echo installed version of $tool is $v\; buildtool = $buildtool

    endif

    if ($buildtool != 0) then

	set build = 1
	break

    endif

  end
endif

# Now see if all the tools should be built.

if ($build == 1) then

    echo Installing all autotools

    foreach dir ($autoconf $automake $libtool)

	echo unpacking $dir
	gunzip -c $dir.tar.gz | tar xf -

	if (-d $dir) then

	    echo installing $dir
	    set log = $dir.install.log
	    echo installation log is $log

	    chdir $dir
	    ./configure $pre			>&! ../$log
	     make				>>& ../$log
	    make install			>>& ../$log
	    chdir ..

	endif
       
        rehash
    end

endif
