#!/bin/sh -e

# OPTIONS:
#   -d: start the daemon in the background
#   -p <filename>: log the pid to a file (useful to kill it later)
#   -v: print crate version to stdout
#   -h: print usage information
#   -D: set a java system property value
#   -C: set a Crate property value
#   -X: set a nonstandard java option

# CONTROLLING STARTUP:
#
# This script relies on few environment variables to determine startup
# behavior, those variables are:
#
#   JAVA_OPTS    -- Additional arguments to the JVM for heap size, etc
#   CRATE_JAVA_OPTS -- External Java Opts on top of the defaults set
#
#
# Optionally, exact memory values can be set using the following values, note,
# they can still be set using the `CRATE_JAVA_OPTS`. Sample format include "512m", and "10g".
#
#   CRATE_HEAP_SIZE -- Sets both the minimum and maximum memory to allocate (recommended)
#
# As a convenience, a fragment of shell is sourced in order to set one or
# more of these variables. This so-called `include' can be placed in a
# number of locations and will be searched for in order. The lowest
# priority search path is the same directory as the startup script, and
# since this is the location of the sample in the project tree, it should
# almost work Out Of The Box.
#
# Any serious use-case though will likely require customization of the
# include. For production installations, it is recommended that you copy
# the sample to one of /usr/share/crate/crate.in.sh,
# /usr/local/share/crate/crate.in.sh, or
# /opt/crate/crate.in.sh and make your modifications there.
#
# Another option is to specify the full path to the include file in the
# environment. For example:
#
#   $ CRATE_INCLUDE=/path/to/in.sh crate -p /var/run/crate.pid
#
# Note: This is particularly handy for running multiple instances on a
# single installation, or for quick tests.
#
# If you would rather configure startup entirely from the environment, you
# can disable the include by exporting an empty CRATE_INCLUDE, or by
# ensuring that no include files exist in the aforementioned search list.
# Be aware that you will be entirely responsible for populating the needed
# environment variables.

CDPATH=""
SCRIPT="$0"

# SCRIPT may be an arbitrarily deep series of symlinks. Loop until we have the concrete path.
while [ -h "$SCRIPT" ] ; do
  ls=`ls -ld "$SCRIPT"`
  # Drop everything prior to ->
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    SCRIPT="$link"
  else
    SCRIPT=`dirname "$SCRIPT"`/"$link"
  fi
done

# determine crate home
CRATE_HOME=`dirname "$SCRIPT"`/..

# make CRATE_HOME absolute
CRATE_HOME=`cd "$CRATE_HOME"; pwd`

# If an include wasn't specified in the environment, then search for one...
if [ "x$CRATE_INCLUDE" = "x" ]; then
    # Locations (in order) to use when searching for an include file.
    for include in /usr/share/crate/crate.in.sh \
                   /usr/local/share/crate/crate.in.sh \
                   /opt/crate/crate.in.sh \
                   ~/.crate.in.sh \
                   `dirname "$0"`/crate.in.sh; do
        if [ -r "$include" ]; then
            . "$include"
            break
        fi
    done
# ...otherwise, source the specified include.
elif [ -r "$CRATE_INCLUDE" ]; then
    . "$CRATE_INCLUDE"
fi

if [ "$(uname -s)" = "Darwin" ]; then
    JAVA="$CRATE_HOME/jdk/Contents/Home/bin/java"
else
    JAVA="$CRATE_HOME/jdk/bin/java"
fi

if [ -z "$CRATE_CLASSPATH" ]; then
    echo "CRATE_CLASSPATH should've been initialized via crate.in.sh" >&2
    exit 1
fi

# Special-case path variables.
case `uname` in
    CYGWIN*)
        CRATE_CLASSPATH=`cygpath -p -w "$CRATE_CLASSPATH"`
        CRATE_HOME=`cygpath -p -w "$CRATE_HOME"`
    ;;
esac

launch_service()
{
    pidpath=$1
    daemonized=$2
    javaProperties=$3

    case "$4" in
        *"-Cpath.home="*)
            props="$4" ;;
        *)
            props="\"-Cpath.home=$CRATE_HOME\" $4";;
    esac

    if [ "x$pidpath" != "x" ]; then
        props="\"-Cpidfile=$pidpath\" $props"
    fi

    if [ "x$daemonized" = "x" ]; then
        eval exec "\"$JAVA\"" $JAVA_OPTS $CRATE_JAVA_OPTS $javaProperties -cp "\"$CRATE_CLASSPATH\"" \
             io.crate.bootstrap.CrateDB $props
        execval=$?
    else
        # Startup Crate, background it, and write the pid.
        eval exec "\"$JAVA\"" $JAVA_OPTS $CRATE_JAVA_OPTS $javaProperties -cp "\"$CRATE_CLASSPATH\"" \
             io.crate.bootstrap.CrateDB $props -d <&- &
        execval=$?
        [ ! -z "$pidpath" ] && printf '%d' $! > "$pidpath"
    fi

    return $execval
}

args="$@"
if test "${args#*"-Des."}" != "$args"; then
    echo "Support for defining Crate specific settings with the -D option and the es prefix has been dropped."
    echo "Please use the -C option to configure Crate.\n"
    exit 1
fi

# Parse any command line options.
while getopts "vdhp:D:X:C:" opt; do
    case $opt in
        v)
            "$JAVA" $JAVA_OPTS $CRATE_JAVA_OPTS $es_parms -cp "$CRATE_CLASSPATH" $props \
                    org.elasticsearch.Version
            exit 0
        ;;
        p)
            pidfile="$OPTARG"
        ;;
        d)
            daemonized="yes"
        ;;
        h)
            echo "CrateDB is a shared nothing, fully searchable document oriented cluster datastore."
            echo ""
            echo "  Usage: $(basename $0) [OPTION]..."
            echo "         starts a new CrateDB instance"
            echo ""
            echo "General options:"
            echo "  -d            start the daemon in the background"
            echo "  -h            print usage information"
            echo "  -p <pidfile>  log the pid to a file"
            echo "  -v            print version information"
            echo "  -C            set a CrateDB setting"
            echo "  -D            set a java system property value"
            echo "  -X            set a nonstandard java option"
            exit 0
        ;;
        D)
            javaProperties="$javaProperties \"-D$OPTARG\""
        ;;
        C)
            properties="$properties \"-C$OPTARG\""
        ;;
        X)
            properties="$properties \"-X$OPTARG\""
        ;;
        --)
            break
        ;;
        *)
            echo "Error parsing arguments!" >&2
            exit 1
        ;;
    esac
done

# Start up the service
launch_service "$pidfile" "$daemonized" "$javaProperties" "$properties"

exit $?
