]> git.proxmox.com Git - rustc.git/blob - src/rust-installer/gen-install-script.sh
0dc2fcccb9dea7482985d254caafc4fad3bb542b
[rustc.git] / src / rust-installer / gen-install-script.sh
1 #!/bin/sh
2 # Copyright 2014 The Rust Project Developers. See the COPYRIGHT
3 # file at the top-level directory of this distribution and at
4 # http://rust-lang.org/COPYRIGHT.
5 #
6 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9 # option. This file may not be copied, modified, or distributed
10 # except according to those terms.
11
12 set -u
13
14 msg() {
15 echo "gen-install-script: ${1-}"
16 }
17
18 step_msg() {
19 msg
20 msg "$1"
21 msg
22 }
23
24 warn() {
25 echo "gen-install-script: WARNING: $1" >&2
26 }
27
28 err() {
29 echo "gen-install-script: error: $1" >&2
30 exit 1
31 }
32
33 need_ok() {
34 if [ $? -ne 0 ]
35 then
36 err "$1"
37 fi
38 }
39
40 need_cmd() {
41 if command -v $1 >/dev/null 2>&1
42 then msg "found $1"
43 else err "need $1"
44 fi
45 }
46
47 putvar() {
48 local t
49 local tlen
50 eval t=\$$1
51 eval tlen=\${#$1}
52 if [ $tlen -gt 35 ]
53 then
54 printf "gen-install-script: %-20s := %.35s ...\n" $1 "$t"
55 else
56 printf "gen-install-script: %-20s := %s %s\n" $1 "$t"
57 fi
58 }
59
60 valopt() {
61 VAL_OPTIONS="$VAL_OPTIONS $1"
62
63 local op=$1
64 local default=$2
65 shift
66 shift
67 local doc="$*"
68 if [ $HELP -eq 0 ]
69 then
70 local uop=$(echo $op | tr '[:lower:]' '[:upper:]' | tr '\-' '\_')
71 local v="CFG_${uop}"
72 eval $v="$default"
73 for arg in $CFG_ARGS
74 do
75 if echo "$arg" | grep -q -- "--$op="
76 then
77 local val=$(echo "$arg" | cut -f2 -d=)
78 eval $v=$val
79 fi
80 done
81 putvar $v
82 else
83 if [ -z "$default" ]
84 then
85 default="<none>"
86 fi
87 op="${default}=[${default}]"
88 printf " --%-30s %s\n" "$op" "$doc"
89 fi
90 }
91
92 opt() {
93 BOOL_OPTIONS="$BOOL_OPTIONS $1"
94
95 local op=$1
96 local default=$2
97 shift
98 shift
99 local doc="$*"
100 local flag=""
101
102 if [ $default -eq 0 ]
103 then
104 flag="enable"
105 else
106 flag="disable"
107 doc="don't $doc"
108 fi
109
110 if [ $HELP -eq 0 ]
111 then
112 for arg in $CFG_ARGS
113 do
114 if [ "$arg" = "--${flag}-${op}" ]
115 then
116 op=$(echo $op | tr 'a-z-' 'A-Z_')
117 flag=$(echo $flag | tr 'a-z' 'A-Z')
118 local v="CFG_${flag}_${op}"
119 eval $v=1
120 putvar $v
121 fi
122 done
123 else
124 if [ ! -z "$META" ]
125 then
126 op="$op=<$META>"
127 fi
128 printf " --%-30s %s\n" "$flag-$op" "$doc"
129 fi
130 }
131
132 flag() {
133 BOOL_OPTIONS="$BOOL_OPTIONS $1"
134
135 local op=$1
136 shift
137 local doc="$*"
138
139 if [ $HELP -eq 0 ]
140 then
141 for arg in $CFG_ARGS
142 do
143 if [ "$arg" = "--${op}" ]
144 then
145 op=$(echo $op | tr 'a-z-' 'A-Z_')
146 local v="CFG_${op}"
147 eval $v=1
148 putvar $v
149 fi
150 done
151 else
152 if [ ! -z "$META" ]
153 then
154 op="$op=<$META>"
155 fi
156 printf " --%-30s %s\n" "$op" "$doc"
157 fi
158 }
159
160 validate_opt () {
161 for arg in $CFG_ARGS
162 do
163 local is_arg_valid=0
164 for option in $BOOL_OPTIONS
165 do
166 if test --disable-$option = $arg
167 then
168 is_arg_valid=1
169 fi
170 if test --enable-$option = $arg
171 then
172 is_arg_valid=1
173 fi
174 if test --$option = $arg
175 then
176 is_arg_valid=1
177 fi
178 done
179 for option in $VAL_OPTIONS
180 do
181 if echo "$arg" | grep -q -- "--$option="
182 then
183 is_arg_valid=1
184 fi
185 done
186 if [ "$arg" = "--help" ]
187 then
188 echo
189 echo "No more help available for Configure options,"
190 echo "check the Wiki or join our IRC channel"
191 break
192 else
193 if test $is_arg_valid -eq 0
194 then
195 err "Option '$arg' is not recognized"
196 fi
197 fi
198 done
199 }
200
201 msg "looking for install programs"
202 msg
203
204 need_cmd sed
205 need_cmd chmod
206 need_cmd cat
207
208 CFG_ARGS="$@"
209
210 HELP=0
211 if [ "$1" = "--help" ]
212 then
213 HELP=1
214 shift
215 echo
216 echo "Usage: $0 [options]"
217 echo
218 echo "Options:"
219 echo
220 else
221 step_msg "processing arguments"
222 fi
223
224 OPTIONS=""
225 BOOL_OPTIONS=""
226 VAL_OPTIONS=""
227
228 valopt product-name "Product" "The name of the product, for display"
229 valopt rel-manifest-dir "manifestlib" "The directory under lib/ where the manifest lives"
230 valopt success-message "Installed." "The string to print after successful installation"
231 valopt output-script "install.sh" "The name of the output script"
232 valopt legacy-manifest-dirs "" "Places to look for legacy manifests to uninstall"
233
234 if [ $HELP -eq 1 ]
235 then
236 echo
237 exit 0
238 fi
239
240 step_msg "validating arguments"
241 validate_opt
242
243 src_dir="$(cd $(dirname "$0") && pwd)"
244
245 rust_installer_version=`cat "$src_dir/rust-installer-version"`
246
247 # Replace dashes in the success message with spaces (our arg handling botches spaces)
248 product_name=`echo "$CFG_PRODUCT_NAME" | sed "s/-/ /g"`
249
250 # Replace dashes in the success message with spaces (our arg handling botches spaces)
251 success_message=`echo "$CFG_SUCCESS_MESSAGE" | sed "s/-/ /g"`
252
253 script_template=`cat "$src_dir/install-template.sh"`
254
255 # Using /bin/echo because under sh emulation dash *seems* to escape \n, which screws up the template
256 script=`/bin/echo "$script_template"`
257 script=`/bin/echo "$script" | sed "s/%%TEMPLATE_PRODUCT_NAME%%/\"$product_name\"/"`
258 script=`/bin/echo "$script" | sed "s/%%TEMPLATE_REL_MANIFEST_DIR%%/$CFG_REL_MANIFEST_DIR/"`
259 script=`/bin/echo "$script" | sed "s/%%TEMPLATE_SUCCESS_MESSAGE%%/\"$success_message\"/"`
260 script=`/bin/echo "$script" | sed "s/%%TEMPLATE_LEGACY_MANIFEST_DIRS%%/\"$CFG_LEGACY_MANIFEST_DIRS\"/"`
261 script=`/bin/echo "$script" | sed "s/%%TEMPLATE_RUST_INSTALLER_VERSION%%/\"$rust_installer_version\"/"`
262
263 /bin/echo "$script" > "$CFG_OUTPUT_SCRIPT"
264 need_ok "couldn't write script"
265 chmod u+x "$CFG_OUTPUT_SCRIPT"
266 need_ok "couldn't chmod script"