#!/bin/bash -

#
# This script will start Artemis on a UNIX system.
#

QUIET=no
PRG=$0

usage () {
    echo "SYNOPSIS"
    echo "        Artemis: Genome Browser and Annotation Tool"
    echo "USAGE"
    echo "        $0 [options] <SEQUENCE_FILE> [+FEATURE_FILE ...]"
    echo "OPTIONS"
    echo "        SEQUENCE_FILE                  An EMBL, GenBank, FASTA, or GFF3 file"
    echo "        FEATURE_FILE                   An Artemis TAB file, or GFF file"
    echo
    echo "        -options FILE                  Read a text file of options from FILE"
    echo "        -chado                         Connect to a Chado database (using PGHOST, PGPORT, PGDATABASE, PGUSER environment variables)"
    echo
    echo "        -Dblack_belt_mode=?            Keep warning messages to a minimum [true,false]"
    echo "        -Doffset=XXX                   Open viewer at base position XXX [integer >= 1]"
    echo "        -Duserplot=FILE[,FILE2]        Open one or more userplots"
    echo "        -Dloguserplot=FILE[,FILE2]     Open one or more userplots, take log(data)"
    echo "        -Dbam=FILE[,FILE2,...]         Open one or more BAM, CRAM, VCF or BCF files"
    echo "        -DbamClone=n                   Open all BAM, CRAM, VCF or BCF files in multiple (n > 1) panels"
    echo "        -Dbam[1,2,..]=FILE[,FILE2,..]  Open BAM, CRAM, VCF or BCF files in separate panels"
    echo "        -Dshow_snps                    Show SNP marks in BamView"
    echo "        -Dshow_snp_plot                Open SNP plot in BamView"
    echo "        -Dshow_cov_plot                Open coverage plot in BamView"
    echo "        -Dshow_forward_lines=?         Hide/show forward frame lines [true,false]"
    echo "        -Dshow_reverse_lines=?         Hide/show reverse frame lines [true,false]"
    echo "        -Dchado=\"h:p/d?u\"              Get Artemis to open this CHADO database"
    echo "        -Dread_only                    Open CHADO database read-only"
    echo "EXAMPLES"
    echo "        % art AJ006275.embl" 
    echo "        % art contigs.fa +annotation.gff +islands.tab"
    echo "        % art -Dblack_belt_mode=true -Dbam=ecoli_hiseq.bam E_coli_K12.gbk"
    echo "        % art -Duserplot=repeatmap.plot,geecee.plot Plasmid.gff3"
    echo "HOMEPAGE"
    echo "        http://www.sanger.ac.uk/science/tools/artemis"
    echo   
    
    exit 1
}

add_proxy_properties() {

	if [[ "$http_proxy" = "" ]]
	then
		http_proxy=$HTTP_PROXY
	fi
	
	if [[ "$http_proxy" = "" ]]
	then
	 	http_proxy=$HTTP_proxy
	fi
	
	if [[ "$http_proxy" != "" ]]
	then
		APPLICATION_PROPERTIES="$APPLICATION_PROPERTIES -DproxySet=true "`echo $http_proxy | sed 's/http:\/\/\(.*\):\(.*\)/ -Dhttp.proxyHost=\1 -Dhttp.proxyPort=\2/'`
	fi
}

#
# Resolve links - $0 may be a link
#
while [ -h "$PRG" ] ; do
	ls=`ls -ld "$PRG"`
	link=`expr "$ls" : '.*-> \(.*\)$'`
	if expr "$link" : '.*/.*' > /dev/null; then
		PRG="$link"
	else
		PRG="`dirname $PRG`/$link"
	fi
done

# Special Sanger override on chado PGUSER
if [[ "$ARTEMIS_SANGER_DBUSER" != "" ]]
then
	export PGUSER=$ARTEMIS_SANGER_DBUSER
fi

#
# Parse arguments.
#
APPLICATION_PROPERTIES="-Djdbc.drivers=org.postgresql.Driver -Dartemis.environment=UNIX $SANGER_ARTEMIS_OPTIONS"
while test $# != 0
do
    case $1 in
    		-options)   APPLICATION_PROPERTIES="$APPLICATION_PROPERTIES -Dextra_options=$2"; shift ;;
    		-chado)     APPLICATION_PROPERTIES="$APPLICATION_PROPERTIES -Dchado=$PGHOST:$PGPORT/$PGDATABASE?$PGUSER -Dibatis" ;;
	    -D*)        APPLICATION_PROPERTIES="$APPLICATION_PROPERTIES $1" ;;
	    -quiet)     QUIET=yes ; APPLICATION_PROPERTIES="$APPLICATION_PROPERTIES -Drun_quietly=true" ;;
	    -help)      usage ;;
	    --help)     usage ;;
	    -h)         usage ;;
	    *)          break ;;
    esac
    shift
done

APPLICATION_HOME=`dirname "$PRG"`
JAR_NAME=artemis.jar
JAR_FILE=$APPLICATION_HOME/$JAR_NAME

#
# Use a custom Java version if necessary
#
if [[ "$ARTEMIS_JAVA_JRE" = "" ]]
then
	JAVA=`which java`
else
	JAVA_HOME=$ARTEMIS_JAVA_JRE
	JAVA=$ARTEMIS_JAVA_JRE/bin/java
fi

#
# Allow URLs to work from behind firewalls.
#
add_proxy_properties

#
# "-mx1g" sets the maximum amount of memory to use. 
# This may need to be increased when dealing with large files
#
if [[ "$ARTEMIS_JVM_FLAGS" = "" ]]
then
    FLAGS="-mx2g -ms100m -noverify"
else
    FLAGS="$ARTEMIS_JVM_FLAGS -noverify"
fi

PLATTMP=`uname`
if [[ "$PLATTMP" = "Darwin" ]]
then
  	APPLICATION_PROPERTIES="$APPLICATION_PROPERTIES -Dapple.laf.useScreenMenuBar=true -Dcom.apple.mrj.application.apple.menu.about.name=Artemis"
fi

if [[ "$QUIET" = "no" ]]
then
    echo "Starting Artemis with arguments: $FLAGS $APPLICATION_PROPERTIES $*"
fi

if [[ -e $JAR_FILE ]]
then
	$JAVA $FLAGS $APPLICATION_PROPERTIES -jar $JAR_FILE $*
	result=$?
else
	CLASSPATH=$APPLICATION_HOME:$APPLICATION_HOME/lib/biojava.jar:$APPLICATION_HOME/lib/jemAlign.jar:$APPLICATION_HOME/lib/jakarta-regexp-1.2.jar:$APPLICATION_HOME/lib/postgresql-8.4-701.jdbc3.jar:$CLASSPATH
	CLASSPATH=$CLASSPATH:$APPLICATION_HOME/lib/batik/batik-awt-util.jar:$APPLICATION_HOME/lib/batik/batik-dom.jar:$APPLICATION_HOME/lib/batik/batik-ext.jar:$APPLICATION_HOME/lib/batik/batik-svggen.jar:$APPLICATION_HOME/lib/batik/batik-util.jar:$APPLICATION_HOME/lib/batik/batik-xml.jar:$APPLICATION_HOME/lib/batik/batik-codec.jar
	CLASSPATH=$CLASSPATH:$APPLICATION_HOME/lib/j2ssh/commons-logging.jar:$APPLICATION_HOME/lib/j2ssh/j2ssh-core.jar:$APPLICATION_HOME/lib/j2ssh/
	CLASSPATH=$CLASSPATH:$APPLICATION_HOME/lib/ibatis/ibatis-2.3.4.726.jar:$APPLICATION_HOME/lib/ibatis/:$APPLICATION_HOME/lib/ibatis/log4j-1.2.14.jar:$APPLICATION_HOME/lib/ibatis/cglib-nodep-2.2.jar
	CLASSPATH=$APPLICATION_HOME/lib/picard/picard.jar:$APPLICATION_HOME/lib/commons-net-3.6.jar:$CLASSPATH
	CLASSPATH=$APPLICATION_HOME/lib/commons-lang-2.6.jar:$CLASSPATH
	export CLASSPATH

	$JAVA $FLAGS $APPLICATION_PROPERTIES uk.ac.sanger.artemis.components.ArtemisMain $*
	result=$?
fi

exit $result

