#! /bin/sh
#
#          make all the components for charissa sunsort
#
. ./config.mk
INSTALL=install
SORT_FILES=
MAKE_VARS=
SOURCE=
DESTINATION=
CC="CC=cc"
OPTFLAG="-xO4"
OPENWINHOME=${OPENWINHOME:-/usr/openwin}
export OPENWINHOME

usage='Usage:
    install.sh [-p] [-u] [-g] [-d destination] [-s source] -c
        [sunsort directory names] [makevariable=value]

	-p tars sunsort  ... must specify destination and source 
	-u untars sunsort ... must specify destination and source 
	-c cleans up sunsort release
	-d destination directory/file
	-s source directory/file
	-g force use of gcc rather than cc'

(
case "`uname -sr`" in
SunOS\ 5*)
    # We must reject the source compatability compiler
    PATH=`echo :${PATH}: | sed -e 's,:/usr/ucb:,:,' -e 's/^://' -e 's/:$//'`
    ;;
esac
type cc > /dev/null) || CC="CC=gcc"

#
# process user supplied arguments here
#
while getopts pucgd:s: option
do
    case "$option" in
    p)
	INSTALL=pack
	;;
    u)
	INSTALL=unpack
	;;
    c)
	INSTALL=clean
	;;
    s)
	SOURCE=$OPTARG
	;;
    d)
	DESTINATION=$OPTARG
	;;
    g)
	CC="CC=gcc"
	;;
    \?)
	echo "$usage"
	exit 1
	;;
    esac
done
test $CC = "CC=gcc" && OPTFLAG="-O3"
shift `expr $OPTIND - 1`
#
#  check whether we want to act only on specified directory
#
while [ $# -gt 0 ]
do
    case "$1" in
    CC=*)
	CC="$1"
	;;
    *=*)
	MAKE_VARS="$MAKE_VARS \"$1\""
	;;
    *)
	SORT_FILES="${SORT_FILES} $1"
	;;
    esac
    shift
done

#
# set the version of the operating system we expect
#
OS=`uname -r`
OS_VERSION=`(IFS=.; set $OS; echo $1)`
echo "Performing $0 for OS version $OS => using Makefile_$OS_VERSION"

#
# if SORT_FILES is empty assume that we want all files processed
#
if [ -z "$SORT_FILES" ]
then
    SORT_FILES="./sort-spy ./ftnchek_dir ./sunsort_src ./make_tools \
	./sunsort_lib ./sunsort_gui ./display_1d ./display_2d ./buffit_dir \
	./peakfind_dir ./calc_xvgr_dir ./slicing ./ansitp_dir ./sort-subs \
	./interface"
fi

#
# start the making process
#
if [ "$INSTALL" = "install" -o "$INSTALL" = "clean" ]
then
    # Since some makefiles call others, must set up all makefiles first
    for DIR in $SORT_FILES
    do
	( cd $DIR 
	rm -f Makefile 
	ln -s Makefile_${OS_VERSION} Makefile )
    done
    for DIR in $SORT_FILES
    do
	( cd $DIR
	make $INSTALL VERS=$VERS "$CC" OPTFLAG=$OPTFLAG $MAKE_VARS || {
	    echo "error detected in $DIR/Makefile operation"
	    exit 3
	}) || exit $?
    done
fi
#
#  pack sunsort into tar file
#
if [ "$INSTALL" = "pack" ]
then
    if [ -z "$DESTINATION" ] 
    then
	echo "no destination file specified"
	echo "$usage"
	exit 4
    fi
    if [ -z "$SOURCE" -o ! -d "$SOURCE" ] 
    then 
	echo "no source directory specified or not a valid directory"
	echo "$usage"
	exit 5
    fi
    cd `dirname "$SOURCE"` || exit 6
    SOURCE=./`basename $SOURCE`
    tar cvf "$DESTINATION" "$SOURCE"
    compress "$DESTINATION"
fi
#
# unpack sunsort from tar file
#
if [ "$INSTALL" = "unpack" ]
then
    if [ -z "$DESTINATION" -o ! -d "$DESTINATION" ] 
    then
	echo "no destination directory specified or not a valid directory"
	echo "$usage"
	exit 7
    fi
    if [ -z "$SOURCE" ] 
    then 
	echo "no source file specified"
	echo "$usage"
	exit 8
    fi
    uncompress -c "$SOURCE" | (cd "$DESTINATION"; tar xvf -)
fi

#
echo "installation complete under option '$INSTALL'"
exit 0

