#!/bin/csh
#
# installation script
#

set PROG="xalign"

echo ""
echo " ************************************************"
echo " Welcome to the $PROG installation script"
echo " ************************************************"
echo ""
echo ""

unalias rm
unalias cp
unalias ls

####################################################################
#
# check version
#
set VERSION=`cat Lib/VERSION | awk 'NR == 1 {print $0}'`
if ($VERSION == "") then
	echo ""
	echo " *** Error *** "
	echo "I could not determine the version of $PROG."
	echo "Are you in the download directory and did you type './Install'?"
	echo ""
	exit 1
endif

set OS_EXE="unknown"
set OS_TYPE="unknown"
if ( $#argv > 0 ) then
    if (x$1 == "xlinux") then
		set OS_EXE="linux"
		set OS_TYPE="Linux"
    endif
    if (x$1 == "xsolaris") then
		set OS_EXE="sol"
		set OS_TYPE="SunOS"
    endif
    if (x$1 == "xppc") then
		set OS_EXE="ppc"
		set OS_TYPE="ppc"
    endif
    if (x$1 == "xi386") then
		set OS_EXE="i386"
		set OS_TYPE="i386"
    endif
#	if (x$1 == "xsgi") then
#		set OS_EXE="sgi6"
#		set OS_TYPE="IRIX64"
#	endif
endif

#
# determine which operating system you have
#
if (x$OS_EXE == "xunknown") then
	set OS_TYPE=`uname -s`

# Don't support sgi anymore 
#	if ((x$OS_TYPE == "xIRIX64") || (x$OS_TYPE == "xIRIX")) then
#		set OS_EXE="sgi6"
#	endif

# linux
	if (x$OS_TYPE == "xLinux") then
		set OS_EXE="linux"
	endif

# solaris
	if (x$OS_TYPE == "xSunOS") then
		set OS_EXE="sol"
	endif

# powermac or intel?
    if (x$OS_TYPE == "xDarwin") then
        set OS_EXE="ppc"
        set EXE=`uname -m`
        if (x$EXE == "xi386") then
            set OS_EXE="i386"
            set OS_TYPE="i386"
        endif
    endif
endif
	
if (x$OS_EXE == "xunknown") then
	echo ""
	echo " I do not recognize your operating system '$OS_TYPE'."
	echo " Here is what I support:"
	echo ""
	echo "   linux   ('uname -s' = Linux)"
	echo "   solaris ('uname -s' = SunOS)"
	echo "   mac OSX ('uname -s' = Darwin)"
#	echo "   sgi     ('uname -s' = IRIX64 or IRIX)"
	echo ""
	exit 1
endif

####################################################################
#
# Now let's decide where you want to put the software 
#
set DEFAULT_DIR=$HOME/$PROG-$VERSION
echo ""
echo " Enter the directory which will contain the installation"
echo " of $PROG. "
echo ""
echo " I am guessing that you wish to install $PROG in your home directory"
echo "      $DEFAULT_DIR"
echo " Just press return if this is correct."
echo ""
set INSTALL_DIR=$<
if ($INSTALL_DIR == "") then
	set INSTALL_DIR=$DEFAULT_DIR
endif
if (-d $INSTALL_DIR ) then
	echo ""
	echo " The directory '$INSTALL_DIR' already exists."
    echo " You will need to remove this directory and re-run the"
    echo " installation script."
    echo ""
    echo " Terminating xalign Install..."
    echo ""
    exit 1 
endif

####################################################################
# 
# create install directory according to user choices
#
echo " "
echo "****************************************************"
echo " "
echo " Executing installation script..."
echo " "
set TMP_DIR=./$VERSION
set BIN_DIR=$TMP_DIR/bin

rm -rf $TMP_DIR
mkdir $TMP_DIR 

cp -r Lib $TMP_DIR/lib
cp -r Tcl $TMP_DIR/tcl
cp README $TMP_DIR/README
cp -pr Bin.$OS_TYPE $TMP_DIR/bin

# just make sure we have read permission on everything
chmod -R go+r $TMP_DIR

# move it, user confirmed it was ok to do this.
rm -rf $INSTALL_DIR
mv $TMP_DIR $INSTALL_DIR

if (-x $INSTALL_DIR/bin/$PROG) then
	echo " "
	echo " $PROG installation is now complete. "
	echo " Please make sure $PROG works as advertised."
	echo " See the Examples section of the documentation."
	echo " "
	echo "     www.bionmr.ualberta.ca/bds/software/$PROG/latest"
	echo " "
	echo " The $PROG startup script can be found at:"
	echo " "
	echo "     $INSTALL_DIR/bin/$PROG"
	echo " "
	echo " "
	exit 0
endif
echo " "
echo " The $PROG startup script did not get created."
echo " Please investigate messages reported by this installation script."
echo " Please report errors to the author."
echo " "
