forked from xp-runners/reference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh.in
executable file
·229 lines (194 loc) · 6.1 KB
/
setup.sh.in
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/sh
set -e
set -u
display() {
lines=$(wc -l "$TEMPFILE" | cut -d ' ' -f 1)
case $lines in
0) echo ;;
1) head -1 $TEMPFILE ;;
*) echo ; cat $TEMPFILE | sed -e 's/^/ /g'
esac
}
run() {
message=$1
command=$2
fix=${3-No}
type=${4-Error}
printf "> $message...\r"
eval $command > $TEMPFILE 2>&1 && { echo "✓ $(display)$SPACE"; return 0; } || {
printf "\033[31;1m✕ $type #$?: $(display)$SPACE\n"
printf " Cannot verify $message executing \`$command'\033[0m\n"
if [ Warning = $type ] ; then
echo " Ignoring for the moment"
return 0
fi
if [ No = "$fix" ] || [ Unattended = $MODE ] ; then
echo "Installation cannot continue."
return 1
fi
echo " This can be fixed by running \`$fix'"
echo
printf ' Should we do this now (y/N)? '
read answer
if [ y = "$answer" ] ; then
eval $fix
run "$message" "$command"
return $?
fi
echo "OK, you need to fix this yourself, then."
return 2
}
}
download() {
url=$1
target=$2
printf '> Download'
if which curl >/dev/null 2>&1; then
curl '-#' -L "$url" -o $target
elif which wget >/dev/null 2>&1; then
wget -nv "$url" -O $target
fi
return $?
}
commands() {
for command in "$@"; do
which $command 2>/dev/null && return 0
done
echo "Cannot find $* in path"
return 127
}
bundle_php() {
releases=http://windows.php.net/downloads/releases/
download $releases - | grep -Eo 'php-[0-9.]+-nts-Win32-[VCx0-9-]+.zip' | sort -rn | uniq > $TEMPFILE
echo "> Select which PHP version you want to use"
nl -b a $TEMPFILE
lines=$(wc -l "$TEMPFILE" | cut -d ' ' -f 1)
while : ; do
printf ' Select: '
read choice
test $choice -ge 1 2>/dev/null && test $choice -le $lines 2>/dev/null && break
printf "\033[31;1m✕ Obviously not a valid choice.\033[0m Use Ctrl+C to exit.\n"
done
release=$(head -$choice $TEMPFILE | tail -1)
test -f $release || download $releases/$release $release
pwd=$(cygpath -w $(pwd) | sed -e 's#\\#\\\\#g')
target=$(basename $release .zip)
mkdir -p $target
echo "var shell = WSH.createObject('Shell.Application');" > $TEMPFILE
echo "var origin = shell.nameSpace('$pwd\\\\$release').items();" >> $TEMPFILE
echo "var target = shell.nameSpace('$pwd\\\\$target');" >> $TEMPFILE
echo "for (var i = 0; i < origin.count; i++) {" >> $TEMPFILE
echo " WScript.Echo(' ' + origin.item(i));" >> $TEMPFILE
echo " target.copyHere(origin.item(i), 20);" >> $TEMPFILE
echo "}" >> $TEMPFILE
echo "> Extracting into $target"
cscript //NoLogo //e:JScript $(cygpath -w $TEMPFILE)
rm $release
PATH=$PATH:$target
return 0
}
configure() {
printf "> Configuration\r"
if [ -f xp.ini ] ; then
echo "! Not overwriting existing $(ls -al xp.ini)"
else
for option in "$@"; do
echo $option >> xp.ini
done
echo "✓ Created $(ls -al xp.ini)"
fi
cat xp.ini | sed -e 's/^/ /g'
}
printf "\033[32;1mXP Runners setup (@VERSION@)\033[0m\n"
echo '(c) 2016 https://github.com/xp-runners/reference/'
echo
TEMPFILE=$(mktemp -u)
SPACE=' '
MODE=Default
CHECKS=Error
FORCE=
while getopts :uf flag ; do
case $flag in
u) MODE=Unattended ; echo '> Unattended mode' ;;
f) CHECKS=Warning ; echo '> Ignoring errors from prerequisites checks' ;;
esac
done
OS=$(uname)
if [ 'Linux' = "$OS" ] ; then OS=$(lsb_release -si) ; fi
case $OS in
CYGWIN*)
EXE=xp.exe
run 'Cygwin' "uname -a"
run 'Tar' "commands tar" "/cygwinsetup.exe -P tar -q -n -W"
run 'Curl' "commands curl wget" "/cygwinsetup.exe -P curl -q -n -W"
run '.NET Framework installed' "ls -d $(cygpath -m $WINDIR)/Microsoft.NET/Framework*/v*" No $CHECKS
run 'PHP runtime available' "php.exe -v" "bundle_php" $CHECKS
php=$(which php.exe 2>/dev/null || echo No)
if [ No = $php ] ; then
echo "! No PHP runtime, cannot create xp.ini"
else
ver=$($php -r 'echo substr(PHP_VERSION, 0, 3);')
configure \
"rt=$ver" "" \
"[runtime@$ver]" \
"default=$(cygpath -w $php)" \
"extension_dir=$(cygpath -w $(dirname $php))\\ext" \
"extension=php_com_dotnet.dll"
fi
;;
Ubuntu*|Debian*)
EXE=xp
run 'Linux' "uname -a"
run 'Tar' "commands tar" "sudo apt-get install tar"
run 'Curl' "commands curl wget" "sudo apt-get install curl"
run '.NET Framework installed' "mono --version" "sudo apt-get install libmono-corlib4.5-cil libmono-2.0-1" $CHECKS
run 'PHP runtime available' "php -v" "sudo apt-get install php5-cli" $CHECKS
php=$(which php 2>/dev/null || echo No)
if [ No = "$php" ] ; then
echo "! No PHP runtime, cannot create xp.ini"
else
ver=$($php -r 'echo substr(PHP_VERSION, 0, 3);')
configure "rt=$ver" "" "[runtime@$ver]" "default=$php"
fi
;;
Darwin*)
EXE=xp.exe
run 'OS X' "uname -a"
run 'Tar' "commands tar"
run 'Curl' "commands curl wget"
run '.NET Framework installed' "mono --version" No $CHECKS
run 'PHP runtime available' "php -v" No $CHECKS
php=$(which php 2>/dev/null || echo No)
if [ No = "$php" ] ; then
echo "! No PHP runtime, cannot create xp.ini"
else
ver=$($php -r 'echo substr(PHP_VERSION, 0, 3);')
configure "rt=$ver" "" "[runtime@$ver]" "default=$php"
fi
echo '#!/bin/sh' > xp
echo $(which mono)' $(dirname "$0")/xp.exe "$@"' >> xp
chmod 755 xp
;;
*)
EXE=xp
run 'Un*x' "uname -a"
run 'Tar' "commands tar"
run 'Curl' "commands curl wget"
run '.NET Framework installed' "mono --version" No $CHECKS
run 'PHP runtime available' "php -v" No $CHECKS
php=$(which php 2>/dev/null || echo No)
if [ No = $php ] ; then
echo "! No PHP runtime, cannot create xp.ini"
else
ver=$($php -r 'echo substr(PHP_VERSION, 0, 3);')
configure "rt=$ver" "" "[runtime@$ver]" "default=$php"
fi
;;
esac
TARFILE=$(mktemp -u)
download https://bintray.com/artifact/download/xp-runners/generic/xp-runners_@VERSION@.tar.gz $TARFILE
run 'Extract' "tar xvfz $TARFILE $EXE class-main.php web-main.php"
run 'Test' "ls -al $(pwd)/$EXE"
echo
echo "Done, runners installed to $(pwd)"
rm "$TEMPFILE" "$TARFILE"