#!/bin/sh
# Copyright (c)2000 TogetherSoft Corporation. Patents pending.
# All rights reserved
#
# 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).
#

PATH=$PATH:/usr/openwin/bin:/usr/bin/X11

#DEBUG="debug"

debug() {
    if [ ! -z "$DEBUG" ] ; then
        echo "debug: $*" >&2
    fi
}

debug "Call oistart: $*"

OISTARTLOG=$HOME/oistart.log
if [ -z "$TGH" ] ; then
    TGH=`dirname $0`/../..
fi

UNAME=`uname -s`
ARCH=`uname -pm | tr ' ' '-'`
PATH=$PATH:$TGH/bin/$UNAME/$ARCH:$TGH/bin/unix
export PATH
debug "UNAME=<$UNAME>, ARCH=<$ARCH>"

if [ -z "$*" ] ; then
   echo 'TOGETHER TOOLS LAUNCHER'
   echo '   Copyright (c) 2000 TogetherSoft Corporation'
   echo 'USAGE:'
   echo "   $0" ' [-r|-s] [-f <logfile>] <file_spec> [params]'
   echo "or $0 " '[-r|-s] [-o <out_pipe>] [-e <err_pipe>] <file_spec> [params]'
   echo ''
   echo 'without \"-r\" start application using assotiations'
   echo '     <params> are ignored'
   echo 'with \"-r\" <file_spec> is executable file. Run it with <params>'
   echo '  -o, -e, -f - output stream for -r option:'
   echo '    -o <out_pipe> - pipes stdout to <out_pipe> file'
   echo '    -e <err_file> - pipes stderr to <err_pipe> file'
   echo '    -f <logfile>  - merged stdout and stderr of the process to'
   echo '                    <logfile>, -e and -o options are ignored'
   echo '  -d <path> or -w <path> - path to the work directory'
   echo '-min - start application minimized (by -r option)'
   echo '-normal - start application -in normal window (by -r option)'
   echo '-retcode     - return applications exit code'
# only for compatiblity - unix-oistart always returns retcode
   exit
fi

EXEC=false
EXTENDED=false
CAPTURE=false
SEPARATE=""
CAPFILE=""
CAPFILE1=""
CAPFILE2=""
WD=`pwd`
CONSOLE="minimized"
RET=false

while : ; do
   debug "Parameter: $1"
   # call executable: -r flag
   if [ "$1" = "-r" ] ; then
      EXEC=true
      shift
      continue
   # extended assotiations: -s flag
   elif [ "$1" = "-s" ] ; then
      EXTENDED=true
      shift
      continue
   # pipes: -f <filename> 
   elif [ "$1" = "-f" ] ; then
      CAPTURE=true
      CAPFILE=`echo $2 | sed s/\\"//g`
      shift 2
      continue
   elif [ "$1" = "-o" ] ; then
      CAPTURE=true
      CAPFILE1=`echo $2 | sed s/\\"//g`
      SEPARATE=true
      shift 2
      continue
   elif [ "$1" = "-e" ] ; then
      CAPTURE=true
      CAPFILE2=`echo $2 | sed s/\\"//g`
      SEPARATE=true
      shift 2
      continue
   # working directory
   elif [ "$1" = "-w" -o "$1" = -d ] ; then
      WD=`echo $2 | sed s/\\"//g`
      shift 2
      continue
   # retcode
   elif [ "$1" = "-retcode" ] ; then
      RET=true
      shift
      continue
   # -minimized
   elif [ "$1" = "-min" ] ; then
      CONSOLE="minimized"
      shift
      continue
    elif [ "$1" = "-normal" ] ; then
      CONSOLE="normal"
      shift
      continue
   else
      break
   fi
done

debug "EXEC=<$EXEC>, CAPTURE=<$CAPTURE>, SEPARATE=<$SEPARATE>"
debug "CAPFILE=<$CAPFILE>, WD=<"$WD">, CONSOLE=<$CONSOLE>, RET=<$RET>"
debug "Using WD: <"$WD">"

# working directory accessibility
if [ ! -r "$WD" -o ! -x "$WD" ] ; then
    echo "oistart: can't access specified working directory"
    exit 1
fi

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

if test -z "$OPWD" ; then
   # this should not be
   OPWD=$TGH/bin
fi

debug "OPWD=$OPWD"

# if we execute via -r ...
if [ "$EXEC" = "true" ] ; then
   PROGGY=$1
   shift
   ARGS=$*
#  if test -x "$PROGGY" ; then
#     >/dev/null
#  elif test -x "`which $PROGGY`" ; then
#     PROGGY=`which $PROGGY`
#  else
#    echo "oistart: can't execute $PROGGY" >&2
#    exit # program not found in path
#  fi
fi

debug "PROGGY=<$PROGGY>, ARGS=<$ARGS>"

if [ "$EXEC" = "true" ] ; then
   # -r flag: dictates completely different behaviour
   ARGS=`eval echo $ARGS`
   debug ":PROGGY=<$PROGGY>, ARGS=<$ARGS>"
   if [ "$CAPTURE" = "true" ] ; then
      if [ -z "$CAPFILE1" ] ; then
         CAPFILE1="/dev/null"
      fi
      if [ -z "$CAPFILE2" ] ; then
         CAPFILE2="/dev/null"
      fi
      if [ -z "$SEPARATE" ] ; then
         CAPFILE1="$CAPFILE"
         CAPFILE2="$CAPFILE..stderr"
      fi

      echo $CAPFILE $CAPFILE1 $CAPFILE2
	    
      debug "Executing $PROGGY $ARGS with output redirection"
      XTERM_PROG=none
      for XTERM in /usr/bin/xterm /usr/bin/X11/xterm /usr/X11R6/bin/xterm /usr/X11/bin/xterm /usr/openwin/bin/xterm; do
        if [ -x $XTERM ]; then
	  XTERM_PROG=$XTERM;
	fi;  
      done

      if [ "$CONSOLE" = "normal" -a "$XTERM_PROG" != "none" ]; then
        debug "start in normal window <$PROGGY $ARGS>"
        (cd "$WD" ; $XTERM_PROG -e $PROGGY $ARGS >"$CAPFILE1" 2>"$CAPFILE2")
      else
         debug "start minimized <$PROGGY $ARGS>" 
         (cd "$WD" ; eval $PROGGY $ARGS ) >"$CAPFILE1" 2>"$CAPFILE2"
      fi
      
      ret=$?
      export ret
      debug "return code = $ret"
      if [ -z "$SEPARATE" ] ; then
         cat "$CAPFILE2" >>"$CAPFILE"
         rm -f "$CAPFILE2"
      fi
      exit $ret    
   else
      debug "Executing $PROGGY $ARGS"
      XTERM_PROG=none
      for XTERM in /usr/bin/xterm /usr/bin/X11/xterm /usr/X11R6/bin/xterm /usr/X11/bin/xterm /usr/openwin/bin/xterm; do
        if [ -x $XTERM ]; then
	  XTERM_PROG=$XTERM;
	fi;  
      done

      if [ "$CONSOLE" = "normal" -a "$XTERM_PROG" != "none" ]; then
        debug "start in normal window <$PROGGY $ARGS>"
        (cd "$WD" ; $XTERM_PROG -e $PROGGY $ARGS)
      else
         debug "start minimized <$PROGGY $ARGS>"
         (cd "$WD" ; eval $PROGGY $ARGS )
      fi
      exit $?
   fi
else
   # -s flag (or without flags): association using
   # First, check if it is a properly formed url
   # EXT stands for method now
   EXT=`echo $1 | awk '\
   BEGIN {FS=":"}\
   /^.*:/ { printf "%s:", \$1; }'`
 
   if [ -z "$EXT" ] ; then
      EXT=`echo $1 | awk '\
      BEGIN {FS="."}\
      { printf \$NF }' | tr -d ' \t'`
   fi
   debug "EXT=<$EXT>"

   ASSOC=$OPWD/associations

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

   if [ -z "$PROGGY" ] ; then
      if [ -x "$1" ] ; then
         (cd "$WD"; eval $*)
         exit $?
      else
         PROGGY="xterm -e $VISUAL $*"
      fi
   fi

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

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

   debug "BLOAT=<$BLOAT>, PROGGY=<$PROGGY>, TH1=<$THINGY1>, TH2=<THINGY2>"

########################################
# Uncomment next 5 lines to enable
# logging feature.
########################################
# (echo "----" `date` "----"
# echo $THINGY1
# echo $THINGY2
# echo --------------------------------------
# ) >> $OISTARTLOG
   TESTVAL=`echo $THINGY1 | awk 'BEGIN { FS=" "} { printf "%s",$1; }'`
   if [ ! -x `which $TESTVAL` ] ; then 
      SOMETHING=$TGH/bin/unix/associations
      xterm -title "Command not found" -exec ":Command $TESTVAL not found. Please check $SOMETHING"
      exit 10 
   fi  

   (cd "$WD";
   $THINGY1 || $THINGY2 &
)
fi



