summarylogtreecommitdiffstats
path: root/komi-store.sh
blob: cbec3309726d8be1aea427e5d2889e85cb67a363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env bash
set -eo pipefail
APPDIR="/usr/lib/@appname@"
RUNNAME="${APPDIR}/bin/@runname@"
RUNTIME="${APPDIR}/lib/runtime"
JAVA="${RUNTIME}/bin/java"
APPDIR_JARS="${APPDIR}/lib/app"

if [ ! -x "${JAVA}" ]; then
    JAVA="$(command -v java 2>/dev/null || true)"
    if [ -z "${JAVA}" ] || [ ! -x "${JAVA}" ]; then
        echo "komi-store: no java found" >&2
        exit 1
    fi
fi

CFG="$(find "${APPDIR_JARS}" -maxdepth 1 -name '*.cfg' 2>/dev/null | head -n1 || true)"
if [ -z "${CFG}" ] || [ ! -f "${CFG}" ]; then
    echo "komi-store: launcher .cfg not found under ${APPDIR}" >&2
    exit 1
fi

expand() {
    local v="$1"
    v="${v//\$APPDIR/${APPDIR_JARS}}"
    v="${v//\$\{APPDIR\}/${APPDIR_JARS}}"
    v="${v//\$RUNTIME/${RUNTIME}}"
    v="${v//\$\{RUNTIME\}/${RUNTIME}}"
    printf '%s' "$v"
}

classpath=""
modulepath=""
mainclass=""
mainmodule=""
javaopts=()
section=""

while IFS= read -r line || [ -n "$line" ]; do
    line="${line%$'\r'}"
    case "$line" in
        \[*\]) section="$line"; continue ;;
        ''|'#'*) continue ;;
    esac
    case "$section" in
        '[Application]')
        case "$line" in
            app.classpath=*)
            entry="$(expand "${line#app.classpath=}")"
            if [ -z "$classpath" ]; then classpath="$entry"; else classpath="$classpath:$entry"; fi
            ;;
            app.modulepath=*)
            entry="$(expand "${line#app.modulepath=}")"
            if [ -z "$modulepath" ]; then modulepath="$entry"; else modulepath="$modulepath:$entry"; fi
            ;;
            app.mainclass=*)  mainclass="${line#app.mainclass=}" ;;
            app.mainmodule=*) mainmodule="${line#app.mainmodule=}" ;;
        esac
        ;;
        '[JavaOptions]')
        case "$line" in
            java-options=*) javaopts+=("$(expand "${line#java-options=}")") ;;
        esac
        ;;
    esac
done < "$CFG"

cd "${APPDIR_JARS}"

launch_args=("${javaopts[@]}")
if [ -n "$mainmodule" ]; then
    [ -n "$modulepath" ] && launch_args+=(--module-path "$modulepath")
    [ -n "$classpath" ] && launch_args+=(-cp "$classpath")
    launch_args+=(-m "$mainmodule")
    elif [ -n "$classpath" ] && [ -n "$mainclass" ]; then
    launch_args+=(-cp "$classpath" "$mainclass")
    else
    echo "komi-store: could not resolve main class / module from $CFG" >&2
    exit 1
fi

exec "${JAVA}" "${launch_args[@]}" "$@"