summarylogtreecommitdiffstats
path: root/electroncastlab
blob: ef2a215cdd41e23d793c235182fa267a5d79bdd0 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/bash

set -euo pipefail

name=electron-castlab
flags_file="${XDG_CONFIG_HOME:-$HOME/.config}/${name}-flags.conf"
fallback_file="${XDG_CONFIG_HOME:-$HOME/.config}/electron-flags.conf"

lines=()
if [[ -f "${flags_file}" ]]; then
    mapfile -t lines < "${flags_file}"
elif [[ -f "${fallback_file}" ]]; then
    mapfile -t lines < "${fallback_file}"
fi

flags=()
for line in "${lines[@]}"; do
    if [[ ! "${line}" =~ ^[[:space:]]*#.* ]] && [[ -n "${line}" ]]; then
        flags+=("${line}")
    fi
done

: ${ELECTRON_IS_DEV:=0}
export ELECTRON_IS_DEV
: ${ELECTRON_FORCE_IS_PACKAGED:=true}
export ELECTRON_FORCE_IS_PACKAGED

# electron-castlab v41 can crash at startup when CHROME_DESKTOP is set.
unset CHROME_DESKTOP

has_class=0
has_name=0
for arg in "$@"; do
    if [[ "$arg" == "--class" || "$arg" == --class=* ]]; then
        has_class=1
    fi
    if [[ "$arg" == "--name" || "$arg" == --name=* ]]; then
        has_name=1
    fi
done

if [[ ${has_class} -eq 0 || ${has_name} -eq 0 ]]; then
    app_target="."
    for arg in "$@"; do
        if [[ "$arg" == "--" ]]; then
            continue
        fi
        if [[ "$arg" != -* ]]; then
            app_target="$arg"
            break
        fi
    done

    pkg_path=""
    if [[ -d "${app_target}" && -f "${app_target}/package.json" ]]; then
        pkg_path="${app_target}/package.json"
    elif [[ -f "${PWD}/package.json" ]]; then
        pkg_path="${PWD}/package.json"
    fi

    app_class=""
    if [[ -n "${pkg_path}" ]] && command -v python3 >/dev/null 2>&1; then
        app_class="$(python3 - "${pkg_path}" <<'PY'
import json
import sys

with open(sys.argv[1], 'r', encoding='utf-8') as f:
    pkg = json.load(f)

print((pkg.get('name') or pkg.get('productName') or '').strip())
PY
)"
    fi

    if [[ -z "${app_class}" ]]; then
        app_class="$(basename "${PWD}")"
    fi

    app_class="$(printf '%s' "${app_class}" | tr '[:upper:] ' '[:lower:]-' | tr -cd 'a-z0-9._-')"
    if [[ -z "${app_class}" ]]; then
        app_class="electron-app"
    fi

    if [[ ${has_class} -eq 0 ]]; then
        set -- "--class=${app_class}" "$@"
    fi

    if [[ ${has_name} -eq 0 ]]; then
        set -- "--name=${app_class}" "$@"
    fi
fi

runtime_hook="/usr/lib/${name}/linux-app-id.js"
if [[ -f "${runtime_hook}" ]]; then
    exec /usr/lib/${name}/electron "${flags[@]}" -r "${runtime_hook}" "$@"
fi

exec /usr/lib/${name}/electron "${flags[@]}" "$@"