-
Notifications
You must be signed in to change notification settings - Fork 104
/
config-distros.sh
executable file
·432 lines (382 loc) · 13.4 KB
/
config-distros.sh
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#! /bin/sh
# dash built-in echo enforces interpretation of \t etc
echoc () {
#cmd /c echo $*
printf '%s\n' "$*"
}
copy () {
from="$1"
to="$2"
export from to
cmd /c cmd2.bat copy
}
delete () {
from="$1"
to="$1" # same again, to fill parameter
export from to
cmd /c cmd2.bat del/F
}
compare () {
from="$1"
to="$2"
export from to
cmd /c cmd2.bat comp/M
}
case "$installdir" in
?*) custominst=true;;
"") custominst=false;;
esac
INSTDIR="${installdir:-$LOCALAPPDATA/wsltty}"
echoc "Installing wsltty into $INSTDIR"
INSTDIR=`cd "$INSTDIR"; pwd`
installcop=${installdir:-"$LOCALAPPDATA\\wsltty"}
installdir=${installdir:-'%LOCALAPPDATA%\wsltty'}
target="$installdir"'\bin\mintty.exe'
case "$INSTDIR" in
*/) TARGETPATH="$INSTDIR"bin/mintty.exe;;
*) TARGETPATH="$INSTDIR"/bin/mintty.exe;;
esac
CONFDIR="${configdir:-$APPDATA/wsltty}"
configdir=${configdir:-'%APPDATA%\wsltty'}
PATH=/bin:"$PATH":$SYSTEMROOT/System32
contextmenu=false
remove=false
alldistros=true
config=true
case "/$0" in
*/wsl*)
config=false;;
esac
case "$1" in
-info)
config=false
shift;;
-shortcuts)
shift;;
-shortcuts-remove)
remove=true
shift;;
-default)
alldistros=false
shift;;
-contextmenu)
contextmenu=true
shift;;
-contextmenu-default)
contextmenu=true
alldistros=false
shift;;
-contextmenu-remove)
contextmenu=true
remove=true
direckey='/HKEY_CURRENT_USER/Software/Classes/Directory'
regtool list "$direckey/shell" 2>/dev/null |
while read name
do
case `regtool get "$direckey/shell/$name/command/"` in
*bin\\mintty.exe*/bin/wslbridge*|*bin\\mintty.exe*--WSL*)
regtool remove "$direckey/shell/$name/command"
regtool remove "$direckey/shell/$name"
;;
esac
done
regtool list "$direckey/Background/shell" 2>/dev/null |
while read name
do
case `regtool get "$direckey/Background/shell/$name/command/"` in
*bin\\mintty.exe*/bin/wslbridge*|*bin\\mintty.exe*--WSL*)
regtool remove "$direckey/Background/shell/$name/command"
regtool remove "$direckey/Background/shell/$name"
;;
esac
done
exit
shift;;
esac
if $config && ! $contextmenu
then
# remove shortcut entries in Start menu and cmd-line bat shortcuts
(cd "$INSTDIR"
for lnk in *.lnk
do
if compare "$lnk" "$APPDATA\\Microsoft\\Windows\\Start Menu\\Programs\\$lnk"
then delete "$APPDATA\\Microsoft\\Windows\\Start Menu\\Programs\\$lnk"
fi
done
for bat in *.bat
do
if compare "$bat" "$LOCALAPPDATA\\Microsoft\\WindowsApps\\$bat"
then delete "$LOCALAPPDATA\\Microsoft\\WindowsApps\\$bat"
fi
done
)
fi
# test w/o WSL: call this script with REGTOOLFAKE=true dash config-distros.sh
if ${REGTOOLFAKE:-false}
then
regtool () {
case "$1" in
-*) shift;;
esac
key=`echo $2 | sed -e 's,.*{\(.*\)}.*,\1,' -e t -e d`
case "$1.$2" in
list.*)
if $contextmenu
then echo "{0}"
else echo "{1}"; echo "{2}"
fi;;
get.*/DistributionName)
echo "distro$key";;
get.*/BasePath)
echo "C:\\Program\\{$key}\\State";;
get.*/PackageFamilyName)
echo "distro{$key}";;
get.*/PackageFullName)
echo "C:\\Program\\{$key}";;
esac
}
fi
if $config
then while read line; do echo "$line"; done <</EOB > mkbat.bat
@echo off
echo Creating %1.bat
echo @echo off> %1.bat
echo rem Start mintty terminal for WSL package %name% in current directory>> %1.bat
echo %target% -i "%icon%" %minttyargs% %bridgeargs% %%*>> %1.bat
/EOB
fi
if $custominst && $config && ! $remove
then
#mkshortcut.exe -n "add to context menu" -a "$installdir/config-distros.sh -contextmenu" "$installdir/bin/dash.exe" -i '%SystemRoot%\System32\filemgmt.dll' -s min -d "" -w "$installdir"
#mkshortcut.exe -n "add default to context menu" -a "$installdir/config-distros.sh -contextmenu-default" "$installdir/bin/dash.exe" -i '%SystemRoot%\System32\filemgmt.dll' -s min -d "" -w "$installdir"
#mkshortcut.exe -n "remove from context menu" -a "$installdir/config-distros.sh -contextmenu-remove" "$installdir/bin/dash.exe" -i '%SystemRoot%\System32\filemgmt.dll' -s min -d "" -w "$installdir"
#mkshortcut.exe -n "configure WSL shortcuts" -a "$installdir/config-distros.sh" "$installdir/bin/dash.exe" -i '%SystemRoot%\System32\filemgmt.dll' -s min -d "" -w "$installdir"
icon='%SystemRoot%\System32\filemgmt.dll'
wdir="$installdir"
target="$installdir/bin/dash.exe"
minttyargs="/config-distros.sh"
bridgeargs=
export icon wdir target minttyargs bridgeargs
cscript /nologo mkshortcut.vbs "/name:configure WSL shortcuts" "/min:true"
bridgeargs=-contextmenu
cscript /nologo mkshortcut.vbs "/name:add to context menu" "/min:true"
bridgeargs=-contextmenu-default
cscript /nologo mkshortcut.vbs "/name:add default to context menu" "/min:true"
bridgeargs=-contextmenu-remove
cscript /nologo mkshortcut.vbs "/name:remove from context menu" "/min:true"
copy "add to context menu.lnk" "$APPDATA\\Microsoft\\Windows\\Start Menu\\Programs\\WSLtty"
copy "add default to context menu.lnk" "$APPDATA\\Microsoft\\Windows\\Start Menu\\Programs\\WSLtty"
copy "remove from context menu.lnk" "$APPDATA\\Microsoft\\Windows\\Start Menu\\Programs\\WSLtty"
copy "configure WSL shortcuts.lnk" "$APPDATA\\Microsoft\\Windows\\Start Menu\\Programs\\WSLtty"
# restore target
target="$installdir"'\bin\mintty.exe'
fi
lxss="/HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Lxss"
schema="/HKEY_CURRENT_USER/Software/Classes/Local Settings/Software/Microsoft/Windows/CurrentVersion/AppModel/SystemAppData"
appex () {
while read line
do
case "$line" in
*Application*Executable*)
for item in $line
do case "$item" in
Executable=*)
eval $item
echo "$Executable"
break;;
esac
done
break;;
esac
done < $*
}
config () {
guid="$1"
ok=false
case $guid in
{*)
distro=`regtool get "$lxss/$guid/DistributionName"`
case "$distro" in
Legacy)
name="Bash on Windows"
launcher="$SYSTEMROOT/System32/bash.exe"
;;
*) name="$distro"
launcher="$LOCALAPPDATA/Microsoft/WindowsApps/$distro.exe"
;;
esac
basepath=`regtool get "$lxss/$guid/BasePath"`
if package=`regtool -q get "$lxss/$guid/PackageFamilyName"`
then
distrinst=`regtool get "$schema/$package/Schemas/PackageFullName"`
# get actual executable path (may not match $distro) from app manifest
manifest="$ProgramW6432/WindowsApps/$distrinst/AppxManifest.xml"
psh_cmd='([xml]$(Get-Content '"\"$manifest\""')).Package.Applications.Application.Executable'
executable=`appex "$manifest"`
if [ -r "$ProgramW6432/WindowsApps/$distrinst/$executable" ]
then icon="$ProgramW6432/WindowsApps/$distrinst/$executable"
elif [ -r "$ProgramW6432/WindowsApps/$distrinst/images/icon.ico" ]
then icon="$ProgramW6432/WindowsApps/$distrinst/images/icon.ico"
else icon="$installcop"'\wsl.ico'
fi
root="$basepath/rootfs"
elif [ -f "$basepath/$distro.exe" ]
then
icon="$basepath/$distro.exe"
root="$basepath/rootfs"
elif [ -d "$LOCALAPPDATA/lxss" ]
then
# legacy "Bash on Windows"
icon="$LOCALAPPDATA/lxss/bash.ico"
root="$basepath"
else
# imported distro? (#226, #236)
icon="$installcop"'\wsl.ico'
root="$basepath/rootfs"
fi
# invocation parameters for mintty
minttyargs='--WSL="'"$distro"'" --configdir="'"$configdir"'"'
# MINTARGS deprecated; used for mkshortcut.exe rather than mkshortcut.vbs
MINTARGS='--WSL="'"$distro"'" --configdir="'"$CONFDIR"'"'
# invocation commands (deprecated for mintty, used for start menu scripts)
#bridgeargs='--distro-guid "'"$guid"'" -t'
ok=true;;
DefaultDistribution|"") # WSL default installation
distro=
name=WSL
icon="$installcop"'\wsl.ico'
minttyargs='--WSL= --configdir="'"$configdir"'"'
MINTARGS='--WSL= --configdir="'"$CONFDIR"'"'
#bridgeargs='-t'
ok=true;;
esac
bridgeargs=" -" # now used to request login mode
echoc "distro '$distro'"
echoc "- name '$name'"
echoc "- guid $guid"
echoc "- (launcher $launcher)"
echoc "- icon $icon"
echoc "- root $root"
wdir=%USERPROFILE%
case "$name" in
docker*) echo skipping docker system
return;;
esac
case "$root" in
*\\Docker*) echo skipping docker system
return;;
esac
if $ok && ! $remove && [ -n "$distro" ]
then # fix #163: backend missing +x with certain mount options
echo Setting +x wslbridge2 backends for distro "'$distro'"
(cd "$INSTDIR"; cd bin; PATH="${WINDIR}/Sysnative:${PATH}" wsl.exe -d "$distro" chmod +x wslbridge2-backend)
# (cd "$LOCALAPPDATA/wsltty/bin"; wsl.exe -d "$distro" chmod +x wslbridge2-backend)
# (cd ... ; "$SYSTEMROOT/System32/bash.exe" "$guid" -c chmod +x wslbridge2-backend)
fi
if $ok && $config
then
export wdir name target minttyargs bridgeargs icon
if $contextmenu
then
# context menu entries
direckey='HKEY_CURRENT_USER\Software\Classes\Directory'
keyname="${name}_Terminal"
if $remove
then
# obsolete; handled above
reg delete "$direckey\\shell\\$keyname" /f
reg delete "$direckey\\Background\\shell\\$keyname" /f
else
direckey='/HKEY_CURRENT_USER/Software/Classes/Directory'
echoc Registry setting "$direckey/[Background/]shell/$keyname"
target="$installcop"'\bin\mintty.exe'
regtool add "$direckey/shell"
regtool add "$direckey/shell/$keyname"
regtool set "$direckey/shell/$keyname/" -s "$name Terminal"
regtool set "$direckey/shell/$keyname/Icon" -s "$icon"
regtool add "$direckey/shell/$keyname/command"
regtool set "$direckey/shell/$keyname/command/" -s "\"$target\" -i \"$icon\" --dir \"%1\" $MINTARGS $bridgeargs"
regtool add "$direckey/Background/shell"
regtool add "$direckey/Background/shell/$keyname"
regtool set "$direckey/Background/shell/$keyname/" -s "$name Terminal"
regtool set "$direckey/Background/shell/$keyname/Icon" -s "$icon"
regtool add "$direckey/Background/shell/$keyname/command"
regtool set "$direckey/Background/shell/$keyname/command/" -s "\"$target\" -i \"$icon\" $MINTARGS $bridgeargs"
fi
else
# invocation shortcuts and scripts
if $remove
then
delete "$APPDATA\\Microsoft\\Windows\\Start Menu\\Programs\\$name Terminal.lnk"
delete "$LOCALAPPDATA\\Microsoft\\WindowsApps\\$name.bat"
delete "$LOCALAPPDATA\\Microsoft\\WindowsApps\\$name~.bat"
if [ "$name" = "WSL" ]
then
# determine actual Desktop folder
desktopkey='\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop'
desktop=`regtool get "$desktopkey"`
case "$desktop" in
%USERPROFILE%*) desktop="$USERPROFILE${desktop#%USERPROFILE%}";;
esac
delete "$desktop\\$name Terminal.lnk"
fi
else
# desktop shortcut in %USERPROFILE% -> Start Menu - WSLtty
cscript /nologo mkshortcut.vbs "/name:$name Terminal %"
#mkshortcut.exe -n "$name Terminal %" -i "$icon" "$TARGETPATH" -a "$MINTARGS" -d "" -w %USERPROFILE%
copy "$name Terminal %.lnk" "$APPDATA\\Microsoft\\Windows\\Start Menu\\Programs\\WSLtty"
# launch script in . -> WSLtty home, WindowsApps launch folder
cmd /C mkbat.bat "$name"
copy "$name.bat" "$LOCALAPPDATA\\Microsoft\\WindowsApps"
# store backup copies in installation dir
if [ "$PWD" != "$INSTDIR" ]
then
copy "$name Terminal %.lnk" "$installcop"
copy "$name.bat" "$installcop"
fi
# prepare versions to target WSL home directory
#bridgeargs="-C~ $bridgeargs"
minttyargs="$minttyargs -~"
MINTARGS="$MINTARGS -~"
# desktop shortcut in ~ -> Start Menu
cscript /nologo mkshortcut.vbs "/name:$name Terminal"
#mkshortcut.exe -n "$name Terminal" -i "$icon" "$TARGETPATH" -a "$MINTARGS" -d "" -w %USERPROFILE%
copy "$name Terminal.lnk" "$APPDATA\\Microsoft\\Windows\\Start Menu\\Programs"
# default desktop shortcut in ~ -> Desktop
if [ "$name" = "WSL" ]
then
#copy "$name Terminal.lnk" "$USERPROFILE\\Desktop"
#copy "$name Terminal.lnk" "$APPDATA\\..\\Desktop\\"
# the above does not work reliably (see #166)
# determine actual Desktop folder
desktopkey='\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop'
desktop=`regtool get "$desktopkey"`
case "$desktop" in
%USERPROFILE%*) desktop="$USERPROFILE${desktop#%USERPROFILE%}";;
esac
copy "$name Terminal.lnk" "$desktop\\"
fi
# launch script in ~ -> WSLtty home, WindowsApps launch folder
cmd /C mkbat.bat "$name~"
copy "$name~.bat" "$LOCALAPPDATA\\Microsoft\\WindowsApps"
# store backup copies in installation dir
if [ "$PWD" != "$INSTDIR" ]
then
copy "$name Terminal.lnk" "$installcop"
copy "$name~.bat" "$installcop"
fi
fi
fi
fi
}
# ensure proper parameter passing to cmd /C
chcp.com 65001 # just in case; seems to work without as well
# configure for all distros, plus default distro
for guid in `
if $alldistros
then regtool list "$lxss" 2>/dev/null
else echo DefaultDistribution
fi || echo "No WSL packages registered" >&2
`
do config $guid
done