Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·145 lines (125 loc) · 3.23 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·145 lines (125 loc) · 3.23 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/sh
set -eu
usage() {
printf '%s\n' 'Usage: ./install.sh --role buyer|provider|all [--prefix ABSOLUTE_PATH]'
}
fail() {
printf 'punch-cli install: %s\n' "$1" >&2
exit 1
}
role=
prefix=${HOME:+"$HOME/.local"}
while [ "$#" -gt 0 ]; do
case "$1" in
--role)
[ "$#" -ge 2 ] || fail 'missing value for --role'
role=$2
shift 2
;;
--prefix)
[ "$#" -ge 2 ] || fail 'missing value for --prefix'
prefix=$2
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
usage >&2
fail "unknown argument: $1"
;;
esac
done
case "$role" in
buyer|provider|all) ;;
'') fail '--role is required' ;;
*) fail '--role must be buyer, provider, or all' ;;
esac
[ -n "$prefix" ] || fail 'HOME is unset; supply --prefix'
case "$prefix" in
/*) ;;
*) fail '--prefix must be an absolute path' ;;
esac
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)
version_file=$script_dir/VERSION
payload_dir=$script_dir/payload
[ -f "$version_file" ] && [ ! -L "$version_file" ] || fail 'release VERSION file is missing or unsafe'
version=$(sed -n '1p' "$version_file")
case "$version" in
''|.|..|*[!0-9A-Za-z._-]*) fail 'release VERSION is invalid' ;;
esac
[ -d "$payload_dir" ] && [ ! -L "$payload_dir" ] || fail 'release payload is missing or unsafe'
[ -x "$payload_dir/runtime/bin/node" ] || fail 'bundled runtime is missing'
install_root=$prefix/share/punch-cli
install_dir=$install_root/$version
bin_dir=$prefix/bin
mkdir -p -- "$install_root" "$bin_dir"
install_root=$(CDPATH= cd -- "$install_root" && pwd -P)
bin_dir=$(CDPATH= cd -- "$bin_dir" && pwd -P)
install_dir=$install_root/$version
[ ! -e "$install_dir" ] || fail "version is already installed: $version"
umask 022
check_link() {
command=$1
link=$bin_dir/$command
if [ -e "$link" ] && [ ! -L "$link" ]; then
fail "refusing to replace non-symlink: $link"
fi
if [ -L "$link" ]; then
old_target=$(readlink "$link")
case "$old_target" in
"$install_root"/*/bin/"$command") ;;
*) fail "refusing to replace unrelated symlink: $link" ;;
esac
fi
}
case "$role" in
buyer)
check_link punch
check_link punch-buyer
;;
provider)
check_link punch
check_link punch-provider
;;
all)
check_link punch
check_link punch-buyer
check_link punch-provider
;;
esac
tmp_dir=$install_root/.install-$version-$$
trap 'rm -rf -- "$tmp_dir"' EXIT HUP INT TERM
mkdir -- "$tmp_dir"
cp -R -- "$payload_dir"/. "$tmp_dir"/
for command in punch punch-buyer punch-provider; do
[ -x "$tmp_dir/bin/$command" ] || fail "release command is missing: $command"
done
mv -- "$tmp_dir" "$install_dir"
trap - EXIT HUP INT TERM
install_link() {
command=$1
link=$bin_dir/$command
target=$install_dir/bin/$command
tmp_link=$bin_dir/.$command.$$
ln -s -- "$target" "$tmp_link"
mv -f -- "$tmp_link" "$link"
}
case "$role" in
buyer)
install_link punch
install_link punch-buyer
;;
provider)
install_link punch
install_link punch-provider
;;
all)
install_link punch
install_link punch-buyer
install_link punch-provider
;;
esac
printf 'Installed Punch CLI %s (%s) under %s\n' "$version" "$role" "$prefix"
printf 'Add %s to PATH if needed.\n' "$bin_dir"