#!/bin/bash
## Main starting script for Jajuk VERSION_REPLACED_BY_ANT

###########################
### CONFIGURATION START ###

#to use your default java on your system use JAVA_BIN="java", or you can give the full path of java.
JAVA_BIN="java"

#JVM arguments used by Jajuk. It's important  to notice that some of this tuning is useful only when using Java player (when mplayer is not available)
# * -client : Use the client JVM, optimized for single user usage [Don't change this setting]
# * -Xms : Initial Heap (total JVM reserved memory) size. We set it to a pretty large value because it requires resources to expand heap size and it causes a blanck when using java player. [it can be reduced to 25M by some users if required]
# * -Xmx: Maximal Heap size. We set a large size because Out Of Memory errors make the application crash. In some rare cases, very large collection (>200Gb) users could increase this setting (see Performance section in the manual). We don't set it to a lower value to avoid Out of Memory issues when performing memory consuming tasks (like generating thumbs). We don't set an higher value because some operations (still generating thumbs) could quickly reach any value and setting this value force the JVM to clear the memory when near to this value (Garbage collecting) [Change this setting only if you have very large collection, do not reduce it]
# * -XX:MinHeapFreeRatio -XX:MaxHeapFreeRatio : fine running parameters that optimizes JVM to garbage collecting as rarely as possible (because a gc 'end of the world' causes an audio blanck). These values have been set by experience [keep these parameters as it]
JAVA_OPTIONS="-client -Xms20M -Xmx512M -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10"

#Jajuk options
JAJUK_OPTIONS="TEST_FLAG_REPLACED_BY_ANT"

#Do you want this script to try to find and use an alternate java version installed on your system that will be more suitable for Jajuk? Use "true" or "false".
FIND_OTHER_JAVA="true"

### CONFIGURATION END ###
#########################

JAJUK_VERSION="VERSION_REPLACED_BY_ANT"
PROGNAME=`basename $0`

## find java version in the default path
JAVA_VER=$($JAVA_BIN -version 2>&1 | grep version | cut -d\" -f 2-2 | cut -c 1-3)

find_java () {
    if [ "$JAVA_VER" = "1.6" ] ; then       
        echo " Java version in the default path is: $JAVA_VER"
    elif [ "$FIND_OTHER_JAVA" = "true" ] ; then
        echo " Java version in the default path is: $JAVA_VER"
        echo "   Java version in the default path may not be the best or compatible with Jajuk. Let's try other places in case a better version is present..."
        echo "   To turn off this auto-detection funtion, edit $0 and set FIND_OTHER_JAVA to false"
        echo " Search for other java..." 
        #Search for Java SUN 1.6
        if [ -e /usr/lib/j2re1.6-sun/bin/java ] ; then
            JAVA_BIN="/usr/lib/j2re1.5-sun/bin/java"
            echo "  j2re1.6-sun detected in $JAVA_BIN"
        elif [ -e /usr/lib/jvm/java-6-sun/jre/bin/java ] ; then
            JAVA_BIN="/usr/lib/jvm/java-6-sun/jre/bin/java"
            echo "  j2re1.6-sun detected in $JAVA_BIN"
        fi
     else
        echo " Java version in the default path is: $JAVA_VER"    
        echo "   No Java compatible version detected, but let's still try to run it!"
     fi
}    


##Go in the right /bin directory
#test if jajuk has been installed in default directory /usr/lib/jajuk/ via RPM/Deb package
if [ "/usr/bin/jajuk" = "$0" ] && [ -e /usr/share/jajuk/bin/jajuk.jar ] ; then
    JAJUK_HOME="/usr/share/jajuk/bin"
    cd /usr/share/jajuk/bin
else
    JAJUK_HOME=$(pwd)/`dirname "$0"`/bin
    cd "$JAJUK_HOME"
fi

##Options 
if [ $# -gt 0 ]; then
    case $1 in
        -h|--help)
            cat <<EOF
Usage: $PROGNAME [options] [<arg> ...]
Options:
   -h, --help     This help
   -v, --version  Report Jajuk, Java version and exit
EOF
            exit 1
            ;;
        -v|--version)
            echo "$PROGNAME ($JAJUK_HOME): $JAJUK_VERSION"
            echo "Jajuk options: $JAJUK_OPTIONS"                        
            find_java 
            echo "   Java bin: $JAVA_BIN"
            $JAVA_BIN -version
            echo "   Java options: $JAVA_OPTIONS"
            exit 0
            ;;
    esac
fi

## Check 32- or 64-bit system
if [ "`uname -m`" = "x86_64" ]; then
	echo Detected a 64-bit operating system, using the lib64 native library directory.
	ARCHLIB=lib64
	RPMLIB=lib64
else
	ARCHLIB=lib32
	RPMLIB=lib
fi

echo "$PROGNAME ($JAJUK_HOME): $JAJUK_VERSION"


## find installed java version 
find_java 


##let's finally start Jajuk:
echo " "
echo $JAVA_BIN $JAVA_OPTIONS -Djava.library.path=/usr/lib/jni:/usr/lib:/usr/share/jajuk/$RPMLIB:$JAJUK_HOME/../lib/$ARCHLIB -jar jajuk.jar $JAJUK_OPTIONS
$JAVA_BIN $JAVA_OPTIONS -Djava.library.path=/usr/lib/jni:/usr/lib:/usr/share/jajuk/$RPMLIB:"$JAJUK_HOME"/../lib/$ARCHLIB -jar jajuk.jar $JAJUK_OPTIONS