#!/bin/bash

# OS specific support (must be 'true' or 'false').
cygwin=false;
darwin=false;
case "`uname`" in
    CYGWIN*)
        cygwin=true
        ;;
        
    Darwin*)
        darwin=true
        ;;
esac

if [ "$SPRING_HOME" == "" ]; then
    SPRING_HOME=`cd "$(dirname $0)"/.. && pwd`
fi
SPRING_BIN=$(dirname $0)

export GROOVY_CONF="${SPRING_BIN}"/groovy.conf
SPRING_HANDLER=auto

TARGETDIR=target/classes
if [ -f build.gradle ]; then
    TARGETDIR=build/classes/main
fi
mkdir -p "${TARGETDIR%/}"

function find_classfile {
    classname="$( echo ${1%%.groovy} | sed -e 's,.*/,,')"
    package="$( grep ^package ${1} | sed -e 's,package\s,,g' -e 's,;,,g' -e 's,\.,/,g')"
    if [ "${package}" != "" ]; then package="${package}/"; fi
    for f in $( find "${TARGETDIR}" -name "${classname}.class" ); do
        if [ "${f}" == "${TARGETDIR}/${package}${classname}.class" ]; then
            echo $f; return 0
        fi
    done
}

function is_compile_needed {
    config=$1
    STATOPTS="-c %X"
    if $darwin; then STATOPTS="-f %Dm"; fi
    # Compile .groovy files if necessary
    if [ ! -f ${config} ]; then
        echo "File ${config} does not exist.  Did you point at the wrong file?"
        exit 3
    else
        classfile=$( find_classfile ${config} )
        if [ ! -f "${classfile}" -o $(stat "${STATOPTS}" ${config}) -gt $(stat "${STATOPTS}" ${classfile} 2>/dev/null || echo 0) ]; then
            return 0
        fi
    fi
    return 1
}

function is_option {
    echo "$1" | grep -q "^--.*"
}

function is_groovy {
    [ "${1%%.groovy}" != "${1}" ]
}

function convert_config_to_class {
    classfile=$( find_classfile ${config} )
    if [ -z "${classfile}" ]; then
        echo "No class found for ${config}. Compiler failed or class not defined?"
        exit 1
    fi
    config="${classfile#${TARGETDIR}/}"
    while [ "${config%/*}" != "${config}" ]; do
        config="${config%/*}"."${config##*/}"
    done
}

config=$1; shift
configs=()
compilables=()
while [ "$config" != "" ]; do
    if is_groovy "$config"; then
        if is_compile_needed "${config}"; then
            compilables[${#compilables[@]}]="${config}"
        fi
        configs[${#configs[@]}]="${config}"
    elif is_option "${config}"; then
        case "${config%=*}" in
            "--handler") SPRING_HANDLER="${config#*=}";;
        esac
    else
        args[${#args[@]}]="${config}"
    fi
    config=$1; shift
done

CLASSPATH="${SPRING_BIN}":"${TARGETDIR}"

for f in "${SPRING_HOME}"/lib/*.jar; do
    CLASSPATH="${CLASSPATH}":$f
done

for f in "${SPRING_HOME}"/*.jar; do
    CLASSPATH="${CLASSPATH}":$f
done

if $cygwin; then
    CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
fi

if [ "${#compilables[@]}" -gt 0 ]; then
    groovyc -cp "${CLASSPATH}" --configscript "$SPRING_BIN"/customizer.groovy  -d "${TARGETDIR}" "${compilables[@]}"
fi

config_classes=("org.springframework.bootstrap.grapes.BootstrapAutoConfiguration.class")
for config in "${configs[@]}"; do
    convert_config_to_class
    config_classes[${#config_classes[@]}]="${config}"
done

if [ "${#config_classes[@]}" == "0" ]; then
    echo "No files to run."
    exit 2
fi

exec groovy -cp "${CLASSPATH}" -Dspring.home="${SPRING_HOME}" --configscript "$SPRING_BIN"/customizer.groovy "$SPRING_BIN"/spring-"${SPRING_HANDLER}".groovy "${config_classes[@]}" "${args[@]}"





































