#!/bin/csh -f

# Starlab installation script.  Specify a non-default installation
# directory as the first command-line argument.  Other arguments go
# directly to the configure script.
#
# The extra layers are necessary to ensure that the correct autotools
# are always available when needed.

# We will by default install with the current directory as STARLAB_PATH.
# If STARLAB_PATH is set on entry and is not PWD, warn the user and 
# redefine STARLAB_PATH to PWD for installation purposes.

if ($?STARLAB_PATH == 0) then
    echo Setting STARLAB_PATH = $PWD
    setenv STARLAB_PATH $PWD
else if ("x$STARLAB_PATH" != "x$PWD") then
    echo Warning: STARLAB_PATH on entry \($STARLAB_PATH\) is not PWD
    echo Resetting STARLAB_PATH to $PWD
    setenv STARLAB_PATH $PWD
else
    echo Using environment STARLAB_PATH = $STARLAB_PATH
endif

# Note that we do *not* update the search path if STARLAB_PATH is changed.

set install_dir = $STARLAB_PATH/usr	# this is now the configure default
if ($#argv > 0) set install_dir = $1

# Don't allow (accidental?) installation in ., PWD, or STARLAB_PATH.
# Attach /usr to install_dir in all those cases.  Use readlink to
# expand all paths before comparing.

(chdir sbin; make -s readlink)	>& /dev/null

set install_dir = `sbin/readlink -f $install_dir`

set dot = `sbin/readlink -f .`
set slp = `sbin/readlink -f $STARLAB_PATH`
set cur = `sbin/readlink -f $PWD`

if ("x$install_dir" == "x$dot" \
    || "x$install_dir" == "x$slp" \
    || "x$install_dir" == "x$cur") then
    echo "Don't allow installation in $install_dir; appending /usr"
    set install_dir = "$install_dir/usr"
endif

echo install_dir \ = $install_dir

set log = install.log
echo Writing detailed output to $log

# Install autotools (place in install_dir):

echo Installing local versions of autotools in install_dir
echo Installing local versions of autotools in install_dir		>&! $log
(chdir src/packages; ./install-autotools $install_dir)			>>& $log

# Make sure that install_dir precedes system directories in search path

set path = ($install_dir/bin $path)
rehash

# First time through we need to do autoreconf  Also should do it if
# configure is older than autoconf -- delete configure in this case.

if (-e configure) then
    set tmp = `find . -name configure.ac -cnewer configure`
    if ($#tmp > 0) then
	rm configure
	echo Deleted configure because it is older than configure.ac
	echo Deleted configure because it is older than configure.ac	>>& $log
   endif
endif

if (! -e configure) then
    echo Running autoreconf to build configure
    echo Running autoreconf to build configure				>>& $log
    autoreconf -i -f							>>& $log
endif

set OK = 0

if (-e configure) then

    # Work around temporary Qt glitch.  Should no longer be necessary,
    # as configure checks for this and takes appropriate action...

    set Qt = ""
    set qt = `sbin/qt_major_version`
    if (x$qt == "x" || $qt < 3) then
	set Qt = "--without-qt"
	unsetenv QTDIR		# just in case...
    endif

    # Perform the usual configuration process:

    echo Running configure \(`date`\)
    echo ./configure $Qt --prefix=$install_dir $argv[2*]
    echo 								>>& $log
    echo Running configure \(`date`\)					>>& $log
    echo ./configure $Qt --prefix=$install_dir				>>& $log
    ./configure $Qt --prefix=$install_dir $argv[2*]			>>& $log

    if ($status != 0) then
	echo Configure error...
	exit
    endif

    # Build the package.

    echo Running make \(`date`\; this may take a while...\)
    echo 								>>& $log
    echo Running make \(`date`\)					>>& $log
    make								>>& $log

    echo Running make install \(`date`\)
    echo 								>>& $log
    echo Running make install \(`date`\)				>>& $log
    make install							>>& $log

    # Then test whatever...

    rehash

    echo Looking for kira
    set which_kira = (`which kira | grep -v 'Command not found.'`)
    if ($#which_kira > 0) then
	echo -n kira':  '; kira --help |& grep created

#	Really want to find kira in $install_dir/bin.  Make sure
#	we didn't just find an older version, and explicitly say
#	so if we have.

	if (! -x $install_dir/bin/kira) then
	    Found old kira, but no new version.
	else
	    set OK = 1
	endif
    else
	echo Couldn\'t find kira...
    endif

    # Note that testsuite uses STARLAB_PATH and the current search path,
    # so it should find the correct kira and test in the proper place.

    echo Running Testsuite \(`date`\; this may take a few minutes\)
    echo 								>>& $log
    echo Running Testsuite \(`date`\)					>>& $log
    ./sbin/testsuite							>>& $log

    set n1 = `grep ^Working $log | grep OK       | wc -l`
    set n2 = `grep ^Working $log | grep Problems | wc -l`
    echo Found $n1 OK and $n2 Problems.

    echo Number of executables found: `ls $install_dir/bin | wc -l`

    # Deuglify the log file (install.log --> install.log.2)

    ./sbin/beautify-install $OK $log

endif

# Congratulate the user and explain some details.

if (X$OK == "X1") then
cat <<EOF

Congratulations, you have successfully installed Starlab!
To use the Starlab tools, make sure that the directory

        $install_dir/bin

is in your search path and *precedes* any system files.
If you prefer, source the script

        $STARLAB_PATH/starlab_start.csh

(or its .sh equivalent) to set up the environment and your
search path automatically.

For programmers, the Starlab header files are installed in

        $install_dir/include/starlab

and the libraries are in

        $install_dir/lib/starlab

Developers should find that any changes made to the automake
"Makefile.am" files will cause the relevant Makefiles to be
automagically regenerated.

                                      Enjoy!
                                      The Starlab Team

EOF
endif
