#!/bin/csh
# $Id: oistart,v 1.6 1998/05/08 15:36:29 svo Exp $
#
# You may want to use the log file if any problems occur. 
# If it is the case, edit the following line and 
# uncomment several ones in the end of this file 
# (check comments thoroughly).
#

setenv OISTARTLOG $HOME/oistart.log

set TGH=`dirname $0`/../..

set UNAME=`uname -s`
set ARCH=`uname -pm | tr ' ' '-'`
setenv PATH ${PATH}:$TGH/bin/$UNAME/$ARCH:$TGH/bin/unix
#
# Usage: oistart [-r] [-f <logfile_name>] <file_specifier> [<parameters>]
#
# -r is for compulsory execution of <file_specifier> with parameters
# parameters ain't to be passed otherwise
#

if ( "$*" == "" ) then
	echo 'Together tools launcher'
	echo 'Copyright (C) 1998,99,2000 TogetherSoft Corporation'
	echo 
	echo "Syntax: $0" '[-r|-s] [-f <logfile_name> <file_specifier> [<parameters>]'
	
	exit
endif

set EXEC="false"
set CAPTURE="false"
set SEPARATE=""
set CAPFILE=""
set CAPFILE1=""
set CAPFILE2=""
set WD=`pwd`
while(1)
	# check -r flag
	switch( "$1" )
	case "-r":
		set EXEC=true
		shift
		breaksw
	# -f <filename>
	case "-f":
		set CAPTURE=true
		set CAPFILE=$2
		shift
		shift
		breaksw
    case "-o":
        set CAPTURE=true
        set CAPFILE1=$2
        set SEPARATE=true
        shift
		shift
        breaksw
    case "-e":
        set CAPTURE=true
        set CAPFILE2=$2
        set SEPARATE=true
        shift
		shift
        breaksw
    case "-s":
        set EXTENDED=true
        shift 
   		breaksw
    case "-w":
        set WD=$2
        shift
		shift
        breaksw
	default:
		break
	endsw
end

# working directory accessibility
if ( ! -r "$WD" || ! -x "$WD" ) then
    echo "oistart: can't access specified working directory"
    exit 1
endif

set OPWD=`echo $0 | awk 'BEGIN {FS="/"} {for(i = 1; i<NF; i++) printf "%s/", $i }'`

if ( "$OPWD" == "" ) then
	# this should not be
	set OPWD=$TGH/bin
endif

# if we execute via -r..
if ( "$EXEC" == "true" ) then
	set PROGGY=$1
	shift
	set ARGS=$*
endif

#
# -r flag dictates completely different behaviour
#
if ( "$EXEC" == "true" ) then
	set ARGS=`eval echo $ARGS`
	if ( "$CAPTURE" == "true" ) then
        if ( "$CAPFILE1" == "" ) then
            set CAPFILE1="/dev/null"
        endif
        if ( "$CAPFILE2" == "" ) then
            set CAPFILE2="/dev/null"
        endif
        if ( "$SEPARATE" == "" ) then
            set CAPFILE1="$CAPFILE..stdout"
            set CAPFILE2="$CAPFILE..stderr"
        endif

        ( (cd $WD ; $PROGGY $ARGS) >$CAPFILE1 ) >& $CAPFILE2
        if ( "$SEPARATE" == "" ) then
            cat $CAPFILE1 $CAPFILE2 >$CAPFILE
            rm -f $CAPFILE1 $CAPFILE2
        endif
	else
        echo "Executing $PROGGY $ARGS"
	    (cd $WD; eval $PROGGY $ARGS)
	endif
else

# First, check if it is a properly formed url
# EXT stands for method now

EXT=`echo $1 | awk 'BEGIN {FS=":"} /^.*:/{printf "%s:", \$1;}'`		

if ( "$EXT" == "" ) then
   EXT=`echo $1 | awk 'BEGIN {FS="."} {printf \$NF}' | tr -d ' \t'`
endif

set ASSOC=$OPWD/associations

set BUCK='$i'
set EXT2=`echo $EXT | sed 's/\\//\\\\\\//g'`
set PROGGY=`cat $ASSOC | tr -s ' \t' | awk "/^$EXT2/ {for(i=2; i<=NF; i++) printf \"%s \", $BUCK}"`

if ( "$PROGGY" == "" ) then
    if ( -x "$1" ) then
        (cd $WD; eval $*)
        exit $?
    else
        set PROGGY="xterm -e $VISUAL $*"
    endif
endif

BSLASH='s/\//\\\//g'

set BLOAT=`echo $* | sed $BSLASH`
set PROGGY=`echo $PROGGY | sed "s/%f/$BLOAT/g"`
set THINGY1=`echo $PROGGY | cut -f 1 -d '#'`
set THINGY2=`echo $PROGGY | cut -f 2 -d '#'`

########################################
# Uncomment next 5 lines to enable
# logging feature.
########################################
# (echo "----" `date` "----" 
# echo $THINGY1
# echo $THINGY2
# echo --------------------------------------
# ) >> $OISTARTLOG

(cd $WD;
 $THINGY1 || $THINGY2 &
)
endif


