#!/bin/bash 
# A wrapper script to launch imagej from the UNIX command line
# Images given as arguments will be opened, macros may also be given as arguments
# Looks for macros in the imagej macro directory
#
# This program is free software, but comes with no warrenty or guarantee
# send bug reports or feedback to jjackson at familyjackson dot net
# Author: Jon Jackson
# Last modified date: 2007/04/23
#
# INSTALLATION INSTRUCTIONS
#
### WARNING ###########################################################
# if this file is downloaded and saved directly from web it may not run 
# because of the carriage return / line feed issue
#######################################################################

# 1) copy the text in this file from http://rsb.info.nih.gov/ij/download/linux/unix-script.txt into a text editor
# 2) Modify path variables according to your system 
# 3) Save in the ImageJ directory as 'imagej'
# 4) Give the new file execute permission
# 5) Be sure the 'imagej' wrapper is in the 'PATH' 

# setup environment
set +u # don't give error for unset variables (matters for environment variables)
shopt -s extglob # allow extended pattern matching

# Site specific variables ------------------------------------------------------------
# ImageJ path
ij_path='/local/ImageJ'
# Path to ImageJ development version
ij_path_dev='/home/jjackson/ImageJ'
# modules / classpath
# see the line beginning modules= below for custom classpath entries
# report errors to this user
ijadmin='' # enter admin's email here
# Documentation URL
doc_url='http://rsb.info.nih.gov/ij/'
# temp folder
ij_tmp='/tmp/imagej'
# default behaviour when an ImageJ window is already open
newwindow='true'
#newwindow='false'
# macro argument conjoining character
separator=':'
# a ' ' may work provided no arguments would contain spaces
# use macro functions:  args=getArgument(); argArray=split(args, ':'); 
# to recover macro arguments
declare -i mem
declare -i default_mem=500
declare -i min_mem=16
declare -i max_mem
declare -i free_mem

# max memory allocation is 1800MB on 32bit java and 4000 on 64bit java
if [[ `uname` == 'SunOS' ]] ; then
    arch='-d64'
	java_path="${ij_path}/jre64/bin/java"	
	max_mem=`vmstat | awk 'NR == 3 {fmem=int($5 / 1024); if (fmem < 4000) {print fmem} else {print 4000}}'`
	free_mem="max_mem"
	mem=${free_mem}/2*3
	if (( $mem > $default_mem || $mem < $min_mem )) ; then mem=$default_mem ; fi
elif [[ `uname` == 'Linux' ]] ; then
	if [[ `uname -m` == 'x86_64' ]] ; then
    	arch='-d64'
    	java_path="${ij_path}/jre64/bin/java"
    	max_mem=`free | awk 'NR == 2 {fmem=int($2 / 1024); if (fmem < 4000) {print fmem} else {print 4000}}'`
		free_mem=`free | awk 'NR == 3 {fmem=int($4 / 1024); if (fmem < 4000) {print fmem} else {print 4000}}'`
		mem=${free_mem}/3*2
		if (( $mem > $default_mem || $mem < $min_mem )) ; then mem=$default_mem ; fi
	else
		arch='-d32'
    	java_path="${ij_path}/jre/bin/java"
    	free_mem=`free | awk 'NR == 2 {fmem=int($2 / 1024); if (fmem < 1800) {print fmem} else {print 1800}}'`
		free_mem=`free | awk 'NR == 3 {fmem=int($4 / 1024); if (fmem < 1800) {print fmem} else {print 1800}}'`
		mem=${free_mem}/3*2
		if (( $mem > $default_mem || $mem < $min_mem )) ; then mem=$default_mem ; fi	
	fi
fi

# Warning on syntax change
for var in "$@" ; do
	if [[ "$var" == @(-batch|-eval|-macro|-mem|-new|-port|-run|-verbose) ]] ; then
		echo "ImageJ command line options have changed!" 1>&2
		echo "$var is no longer a valid option, type 'imagej -h'" 1>&2
		echo "for full usage" 1>&2
		exit 1
	fi
done

# End Site specific variables ---------------------------------------------------------

# other variables 
dir=`pwd`
user=`whoami`
host=`hostname`
if [[ -z "$DISPLAY" ]] ; then
    echo 'Display variable not set'
    echo 'If ImageJ fails to load, try '
    echo '% setenv DISPLAY yourcomputer:0'
    echo 'if you use the "csh" or for "bash" try'
    echo '% export DISPLAY=yourcomputer:0'
    display='default' 
else
    display="$DISPLAY"
fi

declare -i port=0
declare -i verbosity=0
images=''
macrocmd=''
macroargs=''


function usage {
    echo 
    echo 'Image display and analysis program. Opens formats including:'
    echo 'UNC, Analyze, Dicom, NIFTI, Tiff, Jpeg, Gif, PNG ...'  
    echo
    echo 'imagej [options] image [ image2 ... image3 ]'
    echo '    -h        print help and more options'
    echo '    -o        open images in an open ImageJ panel'
    echo '    -p <N>    open images in ImageJ panel number <N>' 
    echo "    -x <MB>   set available memory (default=${mem} max=${max_mem})"     
    echo 
}

function fullusage {
    echo 
    echo 'Image display and analysis program. Opens formats including:'
    echo 'UNC, Analyze, Dicom, NIFTI, Tiff, Jpeg, Gif, PNG ...'  
    echo 
    echo 'imagej [options] image [ image2 ... image3 ] -> open images'
    echo
    echo 'basic options:'
    echo '  -h        print help and more options'
    echo '  -o        open images in existing ImageJ panel if one exists'
    echo '  -p <N>    open images in existing ImageJ panel number <N>' 
    echo "  -x <MB>   set available memory (default=${mem} max=${max_mem})"
    echo
    echo 'advanced options:'
    echo '  -c        enable plugin compilation within imagej'
    echo '  -d        use development version'
    echo '  -v        be verbose (vv or vvv increases verbosity)'  
    echo
    echo 'options for batch processing:'
    echo "  -e 'Macro Code'            execute macro code"
    echo "  -r 'Menu Command'          run menu command"
    echo "Quotation marks '' are required around commands including spaces"
    echo 'Commands can be sent to open ImageJ panels with the -p option'
    echo 
    echo 'options for macros:'
    echo 'imagej [-i image] [-b|-m] [arg1 ... argN] '
    echo '  -b macro    run macro without graphics window' 
    echo '  -m macro    run macro' 
    echo '"image" will be opened before macro is run'
    echo 'all following arguments are passed to macro'        
    echo 
    echo "Documentation - $doc_url "
	echo "Report problems with this software to $ijadmin"
    echo
}

function macroCmdError {
	fullusage 
	echo 'Only one command option (-b -e -m OR -r) may be specified' 1>&2
	exit 1
}

# parse input arguments
while getopts b:cde:hi:m:op:r:vx: options
do
	case $options in
		b)	if [[ -n "$macrocmd" ]] ; then macroCmdError ; fi
			macrocmd="-batch ${OPTARG}"
			;;
		c)	modules="${modules}:${tools}"
			;;
		d)	ij_path="$ij_path_dev"
			;;
		e)  if [[ -n "$macrocmd" ]] ; then macroCmdError ; fi
			macrocmd='-eval'
			macroargs="'${OPTARG}'"
			;;
		h)  fullusage
			exit 0
			;;
		i)  images="${images}'${OPTARG}' "
			;;
		m)	if [[ -n "$macrocmd" ]] ; then macroCmdError ; fi
			macrocmd="-macro ${OPTARG}"
			;;
		o)  newwindow='false'
			;;
		p)	newwindow='false'
			port="${OPTARG}"
        	if (( "$port" < 1 || "$port" > 99 )) ; then
            	echo "${OPTARG} is not a permissible value for port number (-p)" 1>&2
            	exit 1
        	fi
			;;
		r)	if [[ -n "$macrocmd" ]] ; then macroCmdError ; fi
			macrocmd='-run'
			macroargs="'${OPTARG}'"
			;;
		v)	verbosity=verbosity+1
			if (( $verbosity == 2 )) ; then set -x ; fi
			if (( $verbosity == 3 )) ; then set -v ; fi
			;;
		x)	mem="${OPTARG}"
			if (( $mem < $min_mem || $mem > $max_mem )) ; then
            	echo "${OPTARG} is not a permissible value for memory (-x)" 1>&2
            	echo "min=${min_mem}, max=${max_mem}" 1>&2
            	exit 1				
			fi
			;;
		\?) usage
			exit 1 
			;;
	esac
done
			
#for ((i=1; i < $OPTIND; i++)) 
#do
#	shift
#done
## above syntax not supported in bash < 2.05
declare -i i=1
while (( i < $OPTIND )) ; do
	shift
	i=i+1
done

if (( "$#" == 0 )) ; then
	usage
fi	

# The best way to install .jar libraries required by plugins is to copy them 
# to the imagej plugins/jars directory
# Alternatively, either copy them to ${ij_path}/jre/lib/ext/ or add the .jar
# filepath to the modules line below. Paths are separated by a colon
modules="-cp ${ij_path}/ij.jar:${ij_path}/plugins/jars/dcmie.jar"

# if tools.jar is not in ${ij_path}/jre/lib/ext/ edit the 'tools=' line 
# to point to tools.jar. The -compile switch will load tools.jar into the 
# classpath and enable plugins to be compiled in imagej
tools="${ij_path}/tools.jar"

# this is for susan nr plugin
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${ij_path}"

# -b and -m options only:
# remaining command line arguments are passed as macro arguments 
# separated by $separator
if [[ -n "$macrocmd" && -z "$macroargs" ]] ; then
	while (( "$#" > 0 )) ; do
        if [[ -z "$macroargs" ]] ; then 
            macroargs="${1}"
        else
            macroargs="${macroargs}${separator}${1}"
        fi
        shift 		
	done
	macroargs="'$macroargs'"
fi
	
# protect possible spaces in image filenames
if (( "$#" > 0 )) ; then
	while (( "$#" > 0 )) ; do
		images="${images}'${1}' "
		shift 
	done
fi	
  		
# create imagej socket-lock directory if non existant 
if [[ ! -d "$ij_tmp" ]] ; then
    mkdir "$ij_tmp"
    chmod 777 "$ij_tmp"
fi

# Creates a temp file indicating a port is in use by imagej
cd "$ij_tmp"
declare -i count=1
portopen='false'
lockFileCreated='false'
declare -a locklist=(`ls | grep '[0-9][0-9]-.*'`)

if (( $verbosity > 0 )) ; then echo -e "locklist: \n ${locklist[*]}" ; fi

# port specified by user
if (( $port > 0 )) ; then
    # look for a lock on the port specified
    for lockname in ${locklist[*]} ; do
        prefix=`printf '%02u' $port`
        if [[ "$lockname" == ${prefix}-${user}-${host}* ]] ; then
        	# found lock on the requested port, owned by user on current display
            portopen='true'
            if (( $verbosity > 0 )) ; then echo "Using socket lock: $lockname" ; fi
            count=$port
            break
        elif ("$lockname" =~ ${prefix}-* ) ; then
            echo "Port $port is in use by some other user or a different host" 1>&2
            if (( $verbosity > 0 )) ; then echo "Port lock: $lockname" ; fi
            exit 1
        fi
    done 
    # specified port not in use 
    count=$port

# If existing window is requested, look for listening port
elif [[ "$newwindow" == 'false' && ${#locklist} != 0 ]] ; then
    # look for a lock on the current display for this user
    for lockname in ${locklist[*]} ; do
        if [[ "$lockname" == [0-9][0-9]-${user}-${host}-${display} ]] ; then
            portopen='true'
            if (( $verbosity > 0 )) ; then echo "Found socket lock: $lockname" ; fi
            # if a matching user/display is found, use this one
            count="${lockname%-*-*-*}"
            #count=`echo $lockname | sed  -e 's/^\([0-9][0-9]\).*/\1/' -e 's/^0//'` # csh?
            break
        fi
    done
fi

# if a new port is to be used
if [[ "$portopen" == 'false' ]] ; then
    # new window requested or no matching port found
    # if port is not specified, look for first free port
    if (( "$port" == 0 )) ; then 
        if (( ${#locklist} == 0 )) ; then
            # no active locks - use first port
            count=1
        else
            # active locks - check each port number so see if it is in use
            # this is not synchronised!!
            count=0
            inuse='true'
            while [[ "$inuse" == 'true' ]] ; do
                count=count+1
                prefix=`printf '%02u' $count`
                inuse='false'
                for lockname in ${locklist[*]} ; do
                    if [[ "$lockname" == ${prefix}-* ]] ; then
                        inuse='true'
                    fi
                done
            done
        fi
    fi
    # creating a new port lock
    prefix=`printf '%02u' $count`
    lockname=${prefix}-${user}-${host}-${display}
	if (( $verbosity > 0 )) ; then echo -n "creating lock $lockname ... " ; fi
	touch $lockname
	trap '\rm -f ${ij_tmp}/$lockname >/dev/null ; exit 1' EXIT TERM 
	# Quitting ImageJ sends EXIT, as does a kill/kill -9 
	# CTRL+C in terminal sends INT + EXIT
	# System shutdown sends TERM (+EXIT??)
	
	if (( $verbosity > 0 )) ; then  echo 'done' ; fi

    lockFileCreated='true'
	echo 'Open other images in this ImageJ panel as follows:'
	echo "  imagej -p $count <image1> [<image2> ... <imageN>]"
    if (( $verbosity > 0 )) ; then echo "Socket lock: $lockname" ; fi
	echo
fi

# Report number of port locks - more than 50 may indicate error in this script
if [[ $count -gt 50 && $port == 0 && "$ijadmin" != '' ]] ; then
mail -s "ImageJ ports on $host" $ijadmin << EOF
Port locks on $host reached $count
EOF
fi

if (( $verbosity > 0 )) ; then
    echo ${java_path} ${arch} -mx${mem}m ${modules} ij.ImageJ -ijpath ${ij_path} -port${count} ${images} ${macrocmd} ${macroargs}
fi

cd "$dir"
eval "${java_path} ${arch} -mx${mem}m ${modules} ij.ImageJ -ijpath ${ij_path} -port${count} ${images} ${macrocmd} ${macroargs} "

exit 0
