#!/bin/sh

###
### Launch script for Cinema. 
###
### Copyright (c) 2001 Phillip Lord (p.lord@hgmp.mrc.ac.uk)

###
### This is the definative launch script for Cinema. I have scripts in
### other languages also, but if they differ in their functionality,
### all the others are wrong and this one is right. 
###

## I wanted to do some command line parsing here, but my shell
## scripting is not really up to it, which is a pity. The end solution
## to this is probably to do as much of the command liner parsing in
## Java as possible. 


## This is the location of the main install directory where Cinema
## should find all most of its jar files. 
CINEMA_LIB=../lib

## This is the location of any user extensions. 
USER_EXTENSIONS=~/.cinema/ext

VERBOSE=0
    
# while getopts :L:U:D:V c

# do 
#         case $c in
#         L)    CINEMA_LIB=$OPTARG;;
#         U)    USER_EXTENSIONS=$OPTARG;;
#         V)    VERBOSE=1;;
#         ## we want to be able to pass -D options as java virtual
#         ## machine options. This duplicates them so that we can pass
#         ## them before the java class argument. There presence after
#         ## the class as well should not cause any problems and I don't
#         ## know a quick way of removing them.     
#         D)    JVM_OPTIONS="${JVM_OPTIONS} -D$OPTARG";;
#         ## pass everything else onwards
#         \?)   echo parsing unknown $OPTARG $c
#               OTHER_OPTIONS="${OTHER_OPTIONS} -$OPTARG";;
#         esac
# done


## identify the user jars and generate the relevent string
for jar_file in ${USER_EXTENSIONS}/*.jar
do
  user_jars=${user_jars}:$jar_file
done
## also let the users have files not in jars
user_jars=${user_jars}:${USER_EXTENSIONS}

## and the same for cinema jars
for jar_file in ${CINEMA_LIB}/*.jar
do
  cinema_jars=${cinema_jars}:$jar_file
done

## and finally for any external libraries
for jar_file in ${CINEMA_LIB}/ext/*.jar
do 
  ext_jars=${ext_jars}:$jar_file
done

# classpath
classpath=${user_jars}${cinema_jars}${ext_jars}


##
## run CINEMA
##
COMMAND_LINE="java -classpath ${classpath} ${JVM_OPTIONS} uk.ac.man.bioinf.apps.cinema.Cinema ${OTHER_ARGUMENTS} $*"

## print out the command line if verbose
if test $VERBOSE -eq 1 
    then
    echo "Running Cinema as ${COMMAND_LINE}"
fi

java -classpath ${classpath} uk.ac.man.bioinf.apps.cinema.Cinema $OTHER_ARGUMENTS $*
