-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0s
executable file
·425 lines (365 loc) · 10.2 KB
/
0s
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
#! /usr/bin/env tclsh
###############################################################################
# 0s: utilities/shortcuts for the system
set home $::env(HOME)
if {{root} eq $::tcl_platform(user)} { set home /home/b }
source $home/bin/lib.tcl
unset home
need notify-send \
hyprctl \
hyprlock \
dracut \
rsync xbps-query \
0snooze-bat 0snooze-temper snooze \
tlp
set SUDO /usr/sbin/sudo
set ICONS /usr/share/icons/Papirus/24x24
###############################################################################
# helper: notify-send hinting for deduplication
proc _nrep {name} {
return "--hint=string:wired-tag:$name"
}
###############################################################################
# helper: notify-send hinting for icons
proc _nicon {icon} {
global ICONS
return "--hint=string:image-path:$ICONS/$icon"
}
###############################################################################
proc bat {} {
set title bat
set device BAT0
set crit 5
set lo 20
set hi 80
set icon_crit [_nicon panel/battery-010.svg]
set icon_lo [_nicon panel/battery-020.svg]
set icon_hi [_nicon panel/battery-080-charging.svg]
set notify "notify-send"
set dir /sys/class/power_supply/$device
set capacity [readfile $dir/capacity]
set status [readfile $dir/status]
switch -- [string index $status 0] {
{D} {
if {$capacity <= $crit} {
exec {*}$notify -u critical -t 0 $icon_crit $title $capacity%
} elseif {[expr {$lo - 5}] <= $capacity && $capacity <= $lo} {
exec {*}$notify -u normal $icon_lo $title $capacity%
}
}
{C} {
if {$hi <= $capacity && $capacity <= [expr {$hi + 2}]} {
exec {*}$notify -u low $icon_hi $title $capacity%
}
}
}
}
###############################################################################
proc bye {arg} {
global SESS SUDO
proc kill {} {
catch {exec pkill dbus-daemon}
catch {exec pkill dbus-launch}
catch {exec pkill pipewire-pulse}
catch {exec pkill pipewire}
catch {exec pkill wireplumber}
}
proc save {} {
global LOGW LOGX
foreach f "$LOGW $LOGX" {
if {[file exists $f]} {
file rename -force -- $f $f.old
}
}
}
proc sync {} {
exec 0s temper
}
switch -- $arg {
k - o - lock {
if {{w} ne $SESS} {
return
}
set locker hyprlock
if {0 == [catch {exec pidof $locker}]} {
puts {0s: locking canceled, already locked}
return
}
puts {0s: locking...}
catch {exec $locker}
}
e - l - q - x - logout {
puts {0s: logging out...}
save
kill
set desk $::env(XDG_SESSION_DESKTOP)
switch -- $desk {
Hyprland {
exec hyprctl dispatch exit
}
default {
exec pkill $desk
}
}
}
z - sleep - standby - suspend {
puts {0s: sleeping...}
sync
exec $SUDO /bin/zzz -S
}
b - r - restart - reboot {
puts {0s: rebooting...}
save
sync
kill
exec $SUDO /bin/reboot
}
h - s - halt - shutdown {
puts {0s: shutting down...}
save
sync
kill
exec $SUDO /bin/halt
}
default {
ufail {bye k|o|lock | e|l|q|x|logout | z|sleep|standby|suspend | b|r|restart|reboot | h|s|halt|shutdown}
}
}
}
###############################################################################
proc doze {{sec 60}} {
global SUDO
set threshold 10
if {[expr {5 > $sec}]} {
fail {check interval too low}
}
if {[expr {1800 < $sec}]} {
fail {check interval too high}
}
set f /sys/class/power_supply/BAT0/capacity
while true {
exec sleep $sec
set perc [readfile $f]
if {[expr {$perc < $threshold}]} {
exec $SUDO /bin/zzz -S
return
}
}
}
###############################################################################
proc dracut {ver} {
global SUDO
exec -- >@stdout 2>@stderr $SUDO /usr/bin/dracut \
--conf $::env(HOME)/cfg/opt/_kernel/dracut.conf \
--force \
/boot/initramfs-${ver}_c.img ${ver}_c
}
###############################################################################
proc save {} {
global TMPD
set dst $::env(HOME)/bak/save
set nam [exec hostname]
set tmp ${nam}_[clock format [clock seconds] -gmt true -format %y%m%d-%H%M%S]
set src $TMPD/$tmp
set zip $tmp.tgz
set want "$::env(HOME)/cfg/*dotfiles \
$::env(HOME)/.gnupg/*gnupg \
$::env(HOME)/.ssh/*ssh"
proc msg {s} { puts "### $s ###" }
proc sep {} { puts "--------------------" }
proc prep {src nam tmp} {
if {[file isdirectory $src]} {
file delete -force $src
}
file mkdir $src
set f [open $src/LOG w]; puts $f $tmp; close $f
sep
puts "created $src\nsaving \"$nam\" customisations:"
}
proc copy {src} {
upvar 1 want want
# there cannot be asterisk (*) anywhere except as file*name separator
# as specified in this declaration. see copy()
foreach i $want {
lassign [split $i *] f n
if {{} eq $n} {
regsub {^\.(.+)$} [file tail $f] {\1} m
set n $m
}
if {![file readable $f]} {
msg "invalid file $f"
continue
}
if {[file readable $src/$n]} {
msg "name \"$n\" already exists"
continue
}
if {[file isfile $f]} {
if {[catch {exec cp -d $f $src/$n}]} {
msg "error copying $f"
continue
}
} elseif {[file isdirectory $f]} {
if {[catch {exec rsync -lr --exclude=.git $f $src/$n}]} {
msg "error copying $f"
continue
}
set n $n/
} else {
msg "$f is neither file nor dir"
continue
}
puts "* $f ($n)"
set p [open $src/LOG a]; puts $p "$f * %n"; close $p
}
}
proc etc {src} {
exec cp /etc/fstab $src/fstab
puts "* fstab"
exec lsmod >$src/lsmod
puts "* lsmod"
exec tail -n +1 {*}[glob /etc/modprobe.d/*] >$src/modprobe
puts "* modprobe"
exec tail -n +1 {*}[glob /etc/wpa_supplicant/wpa_supplicant-wl*] >$src/wpa
puts "* wpa"
exec tail -n +1 {*}[glob /etc/X11/xorg.conf.d/*] >$src/xorg
puts "* xorg"
exec ls "$::env(HOME)/opt/" >$src/opt
puts "* ~/opt/"
exec cp /boot/EFI/refind/refind.conf $src/refind
puts "* refind"
exec xbps-query --list-pkgs >$src/installed
puts "* packages installed"
exec ls /var/service/ >$src/services
puts "* services enabled"
}
proc save {src nam dst} {
cd $src/..
if {[catch {exec tar czf $dst $nam}]} { fail "error zipping" }
}
proc fin {src zip dst} {
if {[file isdirectory $src]} { file delete -force $src }
puts "removed $src"
set p [open |[list du -hs $dst/$zip] r]
regsub {^(\S+).+$} [read $p] {\1} size
close $p
sep
puts "$zip ($size) saved in $dst"
}
prep $src $nam $tmp
copy $src
etc $src
save $src $tmp $dst/$zip
fin $src $zip $dst
}
###############################################################################
proc snooze {} {
proc dream {name} {
if {0 == [catch {exec ps x | grep "snooze .* 0s $name$"}]} {
puts {snooze $name already running}
return
}
exec 0snooze-$name &
}
dream bat
dream temper
}
###############################################################################
proc temper {} {
global TMPD
# taken from https://www.verot.net/firefox_tmpfs.htm
set where $TMPD
set flag .unpacked
set what mozilla/firefox
set name uiupka2h.default-default
proc base {name} {
return [lindex [file split $name] end]
}
#set once "\"$::env(XDG_DATA_HOME)/$what/sessions/default.yml \
# $::env(XDG_DATA_HOME)/bak/${what}_session.yml\""
# parent link stat vola
set cont "\"$::env(HOME)/.$what $name _$name $where/[base $what]\" \
\"$::env(XDG_CACHE_HOME)/$what $name _$name $where/[base $what]-cache\""
proc log {msg} {
global ME LOGW
set f [open $LOGW a]
puts $f "$ME: $msg"
close $f
}
proc to_disk {link stat flag} {
#global LOGW
log {omitting to-disk rsync messages}
# to emit: exec >>&$LOGW
if {[catch {exec >&/dev/null rsync --archive --verbose --delete --exclude $flag ./$link/ ./$stat/}]} {
log {error copying to disk}
}
}
proc to_mem {stat link flag} {
log {omitting to-mem rsync messages}
if {[catch {exec >&/dev/null rsync --archive ./$stat/ ./$link/}]} {
log {error copying to mem}
}
# touch flag
close [open $link/$flag a]
}
proc backup {src dst} {
file copy -force -- $src $dst
}
proc refresh {root link stat vola} {
upvar 1 flag flag
file mkdir $vola
file attributes $vola -permissions 00700
cd $root
if {{link} ne [file type $link] || $vola ne [file readlink $link]} {
file rename $link $stat
file link $link $vola
}
if {[file exists $link/$flag]} {
to_disk $link $stat $flag
return
}
to_mem $stat $link $flag
}
file mkdir $where
#foreach i $once {
# set src [lindex $i 0]
# set dst [lindex $i 1]
# log "(once) $src -> $dst"
# backup $src $dst
#}
foreach i $cont {
set root [lindex $i 0]
set link [lindex $i 1]
log "(cont) $root/$link"
refresh $root $link [lindex $i 2] [lindex $i 3]
}
}
###############################################################################
proc tlp {} {
# for wm
global SUDO
set title profile
set gov [readfile /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor]
set icon [_nicon panel/indicator-sensors-cpu.svg]
switch -- [string index $gov 1] {
e { set profile start; set message {tlp (auto)} }
o { set profile ac; set message {full (manual)} }
default { set profile ?; set message ? }
}
exec $SUDO tlp $profile
exec notify-send -u low $icon $title $message
}
###############################################################################
set arg [lindex $::argv 0]
if {0 == [llength [info procs $arg]]} {
ufail {bat|bye|doze|dracut|save|snooze|temper|tlp
bat battery alert
bye stop session lock|logout|sleep|reboot|shutdown
doze auto sleep [5-1800]
dracut compile initramfs KERNEL_VERSION
save backup data
snooze scheduled exec
temper www data to tmpfs
tlp toggle tlp profile}
}
$arg {*}[lrange $::argv 1 end]