#! /bin/csh -f
#
# This C-shell script will download and install/upgrade a runtime Karma
# distribution.
#
#   Copyright (C) 2002-2003  Richard Gooch
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#   Richard Gooch may be reached by email at  karma-request@atnf.csiro.au
#   The postal address is:
#     Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.

# Written by		Richard Gooch   3-OCT-2002

# Updated by		Richard Gooch   22-JUN-2003

# Updated by		Richard Gooch   26-AUG-2003: Use KARMABASE if defined.

# Updated by		Richard Gooch   3-SEP-2003: Use nm(1) instead of
# reading symlink, since SuSE Linux has /lib/libc.so.6 as a real file rather
# than a symlink. Grrr.

# Last updated by	Richard Gooch   9-NOV-2003: Added warning if installing
# outside of /usr/local/karma but /usr/local/karma/lib exists.


# Usage: install-karma dest-dir


umask 022
set rsync_host = rsync.ras.ucalgary.ca
if ("$#argv" == "0") then
    if ($?KARMABASE) then
	set dest_dir = $KARMABASE
    else
	set dest_dir = /usr/local/karma
    endif
else
    set dest_dir   = $argv[1]
endif

if ( ("$dest_dir" != "/usr/local/karma") && (-d /usr/local/karma/lib) ) then
    echo "There appears to be a Karma installation in the standard location of"
    echo "/usr/local/karma but you are installing Karma elsewhere."
    echo "THIS MAY CAUSE YOUR APPLICATIONS TO CRASH OR MISBEHAVE"
    echo "You can try to set the LD_LIBRARY_PATH environment variable to:"
    echo "  ${dest_dir}/lib"
    echo "but that will not work on most systems."
    echo "The solution is to install in /usr/local/karma or remove /usr/local/karma first"
    echo -n "Waiting 10 seconds for you to abort the install..."
    sleep 10; echo ""; echo ""
    echo "OK, going ahead anyway. I warned you."
endif

# Make destination directory
if (! -d $dest_dir) then
    mkdir $dest_dir
endif
if (! -d $dest_dir) then
    echo "Could not create $dest_dir"
    exit 1
endif
rm -f $dest_dir/bin/tx
if (-e $dest_dir/bin/tx) then
    echo "Could not remove $dest_dir/bin/tx"
    exit 1
endif

# Install/upgrade share (common) distribution
rsync -az ${rsync_host}::karma/common/ $dest_dir
if (! -x $dest_dir/csh_script/uname_to_platform) then
    echo "Could not find $dest_dir/csh_script/uname_to_platform"
    exit 1
endif

# Figure out what platform this is
setenv MACHINE_OS `$dest_dir/csh_script/uname_to_platform`
set tmp = `echo "$MACHINE_OS" | sed -e 's/_\([^_]*\)$/ \1/'`
setenv MACHINE "$tmp[1]"
setenv OS "$tmp[2]"
unset tmp
if ("$MACHINE" == "UNKNOWN") then
    echo "Unknown machine type"
    exit 1
endif
if ("$OS" == "unknown") then
    echo "Unknown OS type"
    exit 1
endif
set subversion = ""

# Figure out OS subversion (if appropriate)
switch ("$OS")
  case "Linux":
    if (-r /lib/libc.so.6) then
	if (`nm -D /lib/libc.so.6 | fgrep -c GLIBC_2.3` > 0) then
	    set subversion = "_libc6.3"
	else if (`nm -D /lib/libc.so.6 | fgrep -c GLIBC_2.2` > 0) then
	    set subversion = "_libc6.2"
	else
	    set subversion = "_libc6.1"
	endif
    endif
    breaksw
endsw
set platform = "${MACHINE}_${OS}$subversion"

rsync -az --delete --exclude=site ${rsync_host}::"karma/$platform/*" $dest_dir
if (-e $dest_dir/bin/tx) then
    echo "Karma installation/upgrade for $platform appears to have completed successfully"
else
    echo "Replacement $dest_dir/bin/tx was not found"
    exit 1
endif

# Darwin (MacOS X) needs extra installations in order to be useful
if ( ("$OS" == "Darwin") && ( ! -e /sw/lib/libdl.dylib ) ) then
    if ("$uid" == 0) then
	echo "Installing libdl"
	rsync ${rsync_host}::ftp/software/karma/libs/libdl.${MACHINE_OS}.tar.gz /tmp
	cd /
	tar xpzf /tmp/libdl.${MACHINE_OS}.tar.gz
	if (-e /sw/lib/libdl.dylib) then
	    echo "Installation of libdl appears to be successful"
	    rm /tmp/libdl.${MACHINE_OS}.tar.gz
	else
	    echo "Installation of libdl failed: do it manually"
	endif
    else
	echo "You need to install libdl. Become root and re-run this script"
    endif
endif

exit 0
###############################################################################
#      SAMPLE PROCMAIL CONFIGURATION SECTION. CUT AND PASTE THIS.

# Karma rsync robot
:0 fb
* ^Subject: \[ANNOUNCE\] New version of Karma available for rsync update
| (cat > /dev/null) | (/usr/local/karma/csh_script/install-karma | /usr/bin/mail -s "Karma update result" root@localhost rgooch@atnf.csiro.au)
