-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·106 lines (90 loc) · 2.75 KB
/
entrypoint.sh
File metadata and controls
executable file
·106 lines (90 loc) · 2.75 KB
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
99
100
101
102
103
104
105
106
#!/usr/bin/env bash
set -euo pipefail
# Portable realpath function
__realpath() {
if [[ $# -eq 0 ]]
then
printf "A portability wrapper around realpath. Usage: __realpath <path>\n" >&2
return 1
fi
# Check for GNU realpath using the --version flag
if realpath --version >/dev/null 2>&1
then
# This is a GNU realpath. Use it. Use `--` to handle problematic
# filenames starting with a hyphen.
realpath --no-symlinks -- "$1"
return $?
fi
# Check for GNU grealpath (often from Homebrew on macOS)
if command -v grealpath >/dev/null 2>&1
then
grealpath --no-symlinks -- "$1"
return $?
fi
# Fallback to pure shell emulation for BSD or other systems
(
local file_path="${1}"
local file_dir
local file_name
while [[ -h "${file_path}" ]]
do
file_dir=$(dirname "${file_path}")
file_name=$(basename "${file_path}")
file_path=$(readlink "${file_path}")
if [[ "${file_path}" != /* ]]
then
file_path="${file_dir}/${file_path}"
fi
done
file_dir=$(dirname "${file_path}")
file_name=$(basename "${file_path}")
if ! cd -P "${file_dir}"
then
printf "__realpath: cannot resolve path '%s'\n" "${1}" >&2
return 1
fi
printf "%s/%s\n" "$(pwd -P)" "${file_name}"
)
return $?
}
# Get the absolute path of a script
__get_script_dir() {
local source=$(__realpath "$1")
echo "$(dirname $source)"
}
OPTIND=1 # Reset in case getopts has been used previously in the shell.
while getopts "h" opt
do
case "$opt" in
h|\\?)
echo "Usage: $0 <script> [args...]"
echo "Sets the __PREFIX__ variable to the parent directory of $0 and __DIR__ to the directory of <script>"
echo "If <script> is not contained in __PREFIX__, then __MODE__ is set to 'external' and __DIR__ is unset"
echo "Otherwise __MODE__ is set to 'internal'"
exit 1
;;
*) echo "Error parsing input argument: $opt"
exit 1
;;
esac
done
# Get the current directory => __PREFIX__
export __PREFIX__=$(__get_script_dir ${BASH_SOURCE[0]})
# Get the script to run
script="$1"
shift
# Get the directory of the called script => __DIR__
export __DIR__=$(__get_script_dir $script)
export __MODE__="internal"
script_path=$(which $script)
if [[ "${script_path}" =~ ^/.* ]]
then
if [[ ! "${script_path}" =~ "${__PREFIX__}" ]]
then
__MODE__="external"
PATH=$PATH:${__PREFIX__}/opt/bin:${__PREFIX__}/opt/util
unset __DIR__
fi
fi
# Execute the script with remaining arguments
exec "$script" "$@"