-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinstall.tcl
executable file
·284 lines (236 loc) · 7.21 KB
/
install.tcl
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env tclsh
#
# Test if we are on the Windows platform.
#
proc windows {} {
return [expr {$::tcl_platform(platform) eq "windows"}]
}
#
# Ask the user for a Y/N answer. Returns 1 if Y, 0 if N
#
proc user_consents {prompt {default_response {}}} {
switch -exact -nocase -- $default_response {
Y {
append prompt { [Y/n] }
}
N {
append prompt { [y/N] }
}
"" {}
default {
error "Parameter default_response must be \"\", \"Y\" or \"N\""
}
}
while {1} {
puts -nonewline $prompt
flush stdout
set user_input [string trim [gets stdin]]
if {$user_input eq ""} {
set user_input $default_response
}
switch -exact -nocase -- $user_input {
Y { return 1 }
N { return 0 }
}
}
}
#
# Run command inside tcl_shell and return subprocess' stdout.
#
proc tcl_shell_eval {tcl_shell command} {
# file join converts \ to / for the open pipe on Windows.
set fd [open "|[file join $tcl_shell]" r+]
fconfigure $fd -buffering line
# send command (make sure this is one line) and harvest result
puts $fd $command
set rval [gets $fd]
# for some reason this blows up on Debian/Ubuntu currently
catch {[close $fd]}
return $rval
}
#
# Check if tcl_shell has the required version and necessary extensions.
#
proc test_tcl_shell {tcl_shell} {
puts [format "* %-50s" "Checking Tcl shell $tcl_shell..."]
puts -nonewline [format "- %-50s " "Check Tcl version >= 8.6:"]
set result [tcl_shell_eval $tcl_shell {puts $::tcl_version}]
lassign [split $result .] major minor
if {$major < 8 || ($major == 8 && $minor < 6)} {
puts "fail"
return 0
}
puts "ok"
puts -nonewline [format "- %-50s " "Check if interpreter is thread-enabled:"]
set result [tcl_shell_eval $tcl_shell \
{puts [info exists ::tcl_platform(threaded)]}]
if {$result ne "1"} {
puts "fail"
return 0
}
puts "ok"
puts -nonewline [format "- %-50s " "Check for the Thread package:"]
set result [tcl_shell_eval $tcl_shell \
{puts [catch {package require Thread}]}]
if {$result ne "0"} {
puts "fail"
return 0
}
puts "ok"
puts -nonewline [format "- %-50s " "Check for tcllib (require json):"]
set result [tcl_shell_eval $tcl_shell \
{puts [catch {package require json}]}]
if {$result ne "0"} {
puts "fail"
return 0
}
puts "ok"
puts -nonewline [format "- %-50s " "Check for \[incr Tcl] extension:"]
set result [tcl_shell_eval $tcl_shell \
{puts [catch {package require Itcl}]}]
if {$result ne "0"} {
puts "fail"
return 0
}
puts "ok"
# Is Expect available for 32 bit Tcl?
if {![windows]} {
puts -nonewline [format "- %-50s " "Check for Expect:"]
set result [tcl_shell_eval $tcl_shell \
{puts [catch {package require Expect}]}]
if {$result ne "0"} {
puts "fail"
return 0
}
puts "ok"
}
puts -nonewline [format "- %-50s " "Check for tdom extension:"]
set result [tcl_shell_eval $tcl_shell \
{puts [catch {package require tdom}]}]
if {$result ne "0"} {
puts "fail"
return 0
}
puts "ok"
return 1
}
#
# Search system path for a compatible Tcl shell.
#
proc find_compatible_tclsh {} {
# We will keep track of inodes so as to not check same file multiple
# times in case of links, duplicate paths etc.
array set inodes {}
# First check if the invoking shell meets conditions
set tcl_shell [file nativename [info nameofexecutable]]
if {[test_tcl_shell $tcl_shell]} {
if {[user_consents [format "- %-50s" \
"Install for shell $tcl_shell?"] Y]} \
{
return $tcl_shell
}
# Remember we already tried this
if {![catch {file stat $tcl_shell stat}]} {
if {$stat(ino) != 0} {
# Remember the inode number
set inodes($stat(ino)) 1;
}
}
}
if {[windows]} {
set tcl_shell_names {tclsh.exe tclsh86t.exe}
} else {
set tcl_shell_names {tclsh tclsh8.6}
}
if {$::tcl_platform(platform) eq "unix"} {
set os_path_sep ":"
} else {
set os_path_sep ";"
}
foreach shell_name $tcl_shell_names {
foreach dir_name [split $::env(PATH) $os_path_sep] {
set tcl_shell [file nativename [file join $dir_name $shell_name]]
if {![catch {file stat $tcl_shell stat}]} {
if {[info exists inodes($stat(ino))]} {
# Already checked this shell
continue
}
if {$stat(ino) != 0} {
# Remember the inode number
set inodes($stat(ino)) 1;
}
}
if {[file executable $tcl_shell]} {
if {[test_tcl_shell $tcl_shell]} {
if {[user_consents [format "- %-50s" \
"Install for shell $tcl_shell?"] Y]} \
{
return $tcl_shell
}
}
}
}
}
return {}
}
#
# Install caius into the package path of a compatible Tcl shell.
#
proc install_caius {} {
if {[set tcl_shell [find_compatible_tclsh]] eq ""} {
puts "* Could not find a suitable Tcl shell!"
exit 1
}
set pkg_path [regexp -inline -all -- {\S+} \
[tcl_shell_eval $tcl_shell {puts $::auto_path}]]
set install_dir [lindex $pkg_path 0]
foreach {dir} $pkg_path {
if {[regexp {^/usr/local} $dir]} {
set install_dir $dir
break
}
}
append install_dir /caius
puts [format "* %-50s" "Installing to $install_dir..."]
if {[file exists $install_dir]} {
if {![user_consents \
[format "- %-50s" \
"Found a previous installation, replace it?"] N]}\
{
puts "* Installation aborted!"
exit 0
}
file delete -force $install_dir
}
puts -nonewline [format "- %-50s " "Copying files:"]
if {[catch {
file mkdir $install_dir/bin
foreach {name} [glob lib/* xsl] {
file copy $name $install_dir
}
set src_fp [open bin/caius r]
set caius_script [read $src_fp]
close $src_fp
# set the right interpreter
set caius_script [regsub -line {^#!.*$} $caius_script "#!$tcl_shell"]
set dst_fp [open $install_dir/bin/caius w+]
puts -nonewline $dst_fp $caius_script
close $dst_fp
if {![windows]} {
# make caius executable
file attributes $install_dir/bin/caius -permissions 0755
set caius_symlink /usr/local/bin/caius
if {[file exists $caius_symlink]} {
file delete $caius_symlink
}
file link -symbolic $caius_symlink $install_dir/bin/caius
}
} err ] != 0} {
puts "fail"
puts [format "- %-50s " "Error: $err"]
exit 1
}
puts "done"
}
### MAIN ###
install_caius