#!/bin/sh
# -*- Mode: Perl; perl-indent-level: 4; perl-continued-statement-offset: 4; perl-label-offset: -2; indent-tabs-mode: nil -*-
# The contents of this file are subject to the Netscape Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/NPL/
# 
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
# 
# The Original Code is the Netscape Mailstone utility, 
# released March 17, 2000.
# 
# The Initial Developer of the Original Code is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 1999-2000 Netscape Communications Corporation. All
# Rights Reserved.
# 
# Contributor(s):	Dan Christian <robodan@netscape.com>
#			Marcel DePaolis <marcel@netcape.com>
#			Thom O'Connor <thom@communigate.com>
# 
# Alternatively, the contents of this file may be used under the
# terms of the GNU Public License (the "GPL"), in which case the
# provisions of the GPL are applicable instead of those above.
# If you wish to allow use of your version of this file only
# under the terms of the GPL and not to allow others to use your
# version of this file under the NPL, indicate your decision by
# deleting the provisions above and replace them with the notice
# and other provisions required by the GPL.  If you do not delete
# the provisions above, a recipient may use your version of this
# file under either the NPL or the GPL.
#####################################################

# push or clean up mailclient and message files to client hosts
# Usage: setup|cleanup|timesync|checktime|config [mode] [-w workload_file]
#
# Setup (or cleanup) OS specific versions of mailclient and utilities

gconf=conf/general.wld	# default file for machines info

# figure out the host arch
run_nsarch() {
    if [ ! -x bin/nsarch ] ; then
	echo "Installation error!  Cannot locate $1 or nsarch.\n"
	exit 1
    fi

    OUROS=`bin/nsarch`
    # deal with new versions of OS falling back to versions we have
    if [ ! -d build/$OUROS ] ; then
	shortos=`echo $OUROS | sed -e 's/[0-9].*//'`
	near=`ls build | grep $shortos`
	if [ -n "$near" ] ; then
	  near=`echo $near | cut -f2 -d' '`
	  echo "No support for $OUROS, using $near"
	  OUROS=$near
	else
	  echo "No support for $OUROS and nothing similar"
 	fi
    fi
    export OUROS
}

# Setup perl to work from perl/bin/perl
setup_perl() {
    local=`which perl | wc -w`
    if [ 1 -eq "$local" ] ; then
        [ -d perl/bin ] || mkdir -p perl/bin
        (cd perl/bin; ln -s `which perl` perl)
    else
        echo "Could not locate perl"
        exit 1;
    fi
}

# we always need this
[ -d perl/bin ] || setup_perl	# need perl, even for cleanup mode

# Get the default version of mailclient
setup_mailclient() {
    [ -n "$OUROS" ] || run_nsarch $1
    (cd bin; ln -s ../build/$OUROS/{mailclient,getdist} ./ )
}

# setup_file source_dir directory filename
# usually link in a directory.
# if needed, make the directory and link a local executable.
setup_dir_file() {
    [ -n "$OUROS" ] || run_nsarch $1
    if [ -d  $1/$OUROS/$2 ] ; then	# found it in our distribution
      ln -s $1/$OUROS/$2 $2
    else			# look for a system copy
      local=`which $3 | wc -w`
      if [ 1 -eq "$local" ] ; then
	[ -d $2 ] || mkdir -p $2
	(cd $2; ln -s `which $3` $3)
      else
	echo "WARNING: No packaged version of $3 and no local version"
	echo "	Operation may be severely limited"
      fi
    fi
}


# Pick up mode as an argument
if [ $# -gt 0 -a \( "$1" = timesync -o "$1" = checktime \
	-o "$1" = setup -o "$1" = cleanup -o "$1" = config \
	-o "$1" = mstone \) ] ; then
	mode=$1
	shift
else				# or the name we were called
	mode=$0
        sm=`echo $mode | cut -f2 -d/`   # strip ./ from name
        [ -n "$sm" ] && mode=$sm
fi

# see if setup was ever run
if [ "$mode" != setup -a "$mode" != cleanup \
	-a \( ! -x perl/bin/perl -o ! -f .license \) ] ; then
    echo "mstone setup was never run.  Running setup."
    mode=setup
fi

if [ "$mode" = setup ] ; then	# setup mode
    # check to see if our binaries are configured right
    #echo "Checking links"
    [ -x gnuplot/gnuplot ] || setup_dir_file bin gnuplot gnuplot
    [ -x bin/mailclient ] || setup_mailclient

    # copy in an initial conf/general.wld, if needed
    [ ! -f $gconf -a -f ${gconf}.in ] && cp ${gconf}.in $gconf

    # query gnuplot for available output types
    if [ -d tmp ]; then
       echo "set terminal" | gnuplot/gnuplot > tmp/termtypes.out 2>&1
    fi
fi

# figure out an OS specific flags
extra=""
if [ `uname -s` = HP-UX ] ; then        # HP uses remsh for remote exec
    extra="RSH=/usr/bin/remsh $extra"
fi

if [ "$mode" != mstone ] ; then	# other setup modes
    if [ -n "$*" ] ; then
        perl/bin/perl -Ibin -- bin/setup.pl $mode $extra "$@"
    else
        perl/bin/perl -Ibin -- bin/setup.pl $mode $extra -w $gconf
    fi
else				# run a mstone test
    # This just runs the test with the right path to perl
    # Usage: mstone testname
    if [ -z "$1" -o "$1" = "-h" -o "$1" = "--help" ] ; then
       echo "Usage: $0 testname [arguments]"
       echo "Common arguments:"
       echo "  -t time          Test time.  s=seconds, m=minutes, h=hours"
       echo "  -r ramp_time     Connection ramp up time.  s=seconds, m=..."
       echo "  -l load          Number of test clients to use."
       echo "  -b 'banner'      Test description banner."
       echo "  -n 'notes'       Test notes."
       echo "Example:"
       echo " ./mstone pop -t 10m -r 90s -l 24 -b 'Pop reads (full store)'"
       exit 1
    fi

    testcase=$1			# get testname
    shift

    if [ -f $testcase ] ; then	# if they gave us the full path
	testfile=$testcase
    elif [ -f conf/$testcase.wld ] ; then
	testfile="conf/$testcase.wld"
    else
	echo "Cannot find test: $testcase"
    fi

    exec perl/bin/perl -Ibin -- bin/mailmaster.pl $extra -w $testfile "$@"
    # no need to return
fi

if [ "$mode" = cleanup ] ; then # cleanup mode
    #echo "Check for old links"
    # remove any configured links. if these are directories, it will fail
    rm -f perl/bin gd gnuplot 2> /dev/null

    # the built in [ on Solaris does not have -L
    /usr/bin/test -L bin/mailclient && rm -f bin/mailclient
fi
