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