#!/bin/bash

set -euo pipefail

jarfile="aliview.jar"
defprogdir="/usr/share/aliview"
minjavamem="512M"
maxjavamem="1024M"
debug=0
version=0

function show_help () {
cat <<HEREDOC
File:

    `basename $0` - Launcher script for aliview.jar

Version:

    Wrapper script version 29 Jan 2018
    (to see aliview.jar version, use the '-v' option)

By:

    Anders.Larsson [at] icm.uu.se

Description:

    AliView: a fast and lightweight alignment viewer
    and editor for large data sets.
    Citation: Larsson, A. (2014). Bioinformatics30(22): 3276-3278.
    http://dx.doi.org/10.1093/bioinformatics/btu531

    See http://ormbunkar.se/aliview for documentation
    and downloads.

    Source: https://github.com/AliView/AliView

Options:

    -?, -h    -- show this help
    -d        -- show debug info in terminal
    -v        -- show aliview.jar version
    -x <size> -- set max mem size for java

Usage:

    `basename $0` infile
    `basename $0` infile1 infile2
    `basename $0` -x2048M infile
    `basename $0` -d infile
    `basename $0` -v
    `basename $0` -h

HEREDOC
}

#
# Adapted from stackoverflow-questions-59895
#
function resolve_links() {
  # parameters
  source="$1"
  # resolve $source until the file is no longer a symlink
  while [ -h "$source" ]; do
    dir="$(cd -P "$(dirname "$source")" && pwd)"
    # if $dir was a relative symlink, we need to resolve it relative to
    # the path where the symlink file was located
    source="$(readlink "$source")"
    [[ $dir != /* ]] && dir="$dir/$dir"
  done
  echo "$source"
}

## Check java
if type -p java &>/dev/null; then
    myjava=java
elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then
    myjava="$JAVA_HOME/bin/java"
else
    "Error: Cannot locate java"
    exit 1
fi

## Check args
while getopts "h?vdx:" opt; do
    case "$opt" in
        h|\? )
            show_help
            exit 0
            ;;
        d )
            debug=1
            ;;
        v )
            version=1
            ;;
        x )
            maxjavamem=$OPTARG
            ;;
        * ) echo "Unrecognized argument. use '-h' for usage information."
            exit 1
            ;;
    esac
done

shift $((OPTIND-1))

## Set current directory default, and check if not in local
## then always use this jar - otherwise confusing
absolute_file="$(resolve_links $0)"
progdir=$(dirname $absolute_file)
aliview="$progdir/$jarfile"
if [ ! -f "$aliview" ]; then
    progdir="$defprogdir"
    aliview="$progdir/$jarfile"
    if [ ! -x "$aliview" ] ; then
        echo "$0: Error: Cannot find \'$progdir/$jarfile\'"
        echo "This script is just a wrapper for $jarfile."
        echo "See the aliview documentation for more information."
        exit 1
    fi
fi

## Try to get aliview version (experimental)
if [ "$version" -eq 1 ]; then
    nr=$("$myjava" -Djava.awt.headless=true -jar "$aliview" | grep -m1 'version' | awk '{print $NF}')
    echo "$jarfile v.$nr"
    exit 0
fi

## Launch aliview on all input
if [ $# -eq 0 ]; then
    if [ "$debug" -eq 0 ]; then
        $myjava -Xmx$maxjavamem -Xms$minjavamem -jar $aliview &> /dev/null &
    else
        $myjava -Xmx$maxjavamem -Xms$minjavamem -jar $aliview
    fi
else
    for fil in "$@" ; do
        if [ "$debug" -eq 0 ]; then
            $myjava -Xmx$maxjavamem -Xms$minjavamem -jar $aliview "$fil" &> /dev/null &
        else
            $myjava -Xmx$maxjavamem -Xms$minjavamem -jar $aliview "$fil" &
        fi
    done
fi

