#! /bin/sh
#
#          make all the components for charissa sunsort
#
. ./config.mk
INSTALL=install
SORT_FILES=
MAKE_VARS=
SOURCE=
DESTINATION=
CC="cc"
OPTFLAG="-xO4 -xcg92"
STDCFLAG="-xstrconst"
PICFLAG="-Kpic"
PROFFLAG="-xpg -xinline= -xnolibmil -DPROFILE"

OPENWINHOME=${OPENWINHOME:-/usr/openwin}
export OPENWINHOME

usage='Usage:
    sort-install [-g] [-P] -c [sunsort directory names] [makevariable=value]
	-c cleans up sunsort release
	-g force use of gcc rather than cc
	-P make profile libraries'

(
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="gcc"

PROFILE=false

#
# process user supplied arguments here
#
while getopts Pcg option
do
    case "$option" in
    P)
	PROFILE=true
	INSTALL=profile
	;;
    c)
	INSTALL=clean
	;;
    g)
	CC="gcc"
	;;
    \?)
	echo "$usage"
	exit 1
	;;
    esac
done

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

test $CC = "gcc" && OPTFLAG="-O3 -msupersparc" && \
    PROFFLAG="-pg -fno-inline-functions" && STDCFLAG="" && PICFLAG="-fpic"

$PROFILE && OPTFLAG="$OPTFLAG $PROFFLAG"

#
# 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 ./ansitp_dir ./sort-subs ./interface \
	./utilities"
fi

#
# start the making process
#

# 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
XDEFS=
STDCFLAG="$STDCFLAG -DSVR4 $XDEFS"
export OPTFLAG STDCFLAG CC PICFLAG XDEFS
for DIR in $SORT_FILES
do
    ( cd $DIR
# make $INSTALL VERS=$VERS "$CC" "OPTFLAG=$OPTFLAG" $MAKE_VARS || {
make $INSTALL VERS=$VERS $MAKE_VARS || {
    echo "error detected in $DIR/Makefile operation"
    exit 3
}) || exit $?
done

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

