OSDN Git Service

remove pacdiff (for now)
[instantos/instantOS.git] / autostart.sh
1 #!/usr/bin/env bash
2
3 ###############################################
4 ## script for instantOS autostart            ##
5 ###############################################
6
7 INSTANTVERSION="$(cat /usr/share/instantutils/version)"
8 if iconf version && [ "$(iconf version)" = "$INSTANTVERSION" ]; then
9     echo "version check successful"
10     echo "running version $INSTANTVERSION"
11 else
12     echo "running update hooks"
13     /usr/share/instantutils/userinstall.sh
14     iconf -i userinstall 1
15     iconf version "$INSTANTVERSION"
16     instantutils default
17 fi
18
19 # apply wm settings
20 /usr/share/instantutils/wmautostart.sh
21
22 # architecture detection
23 if [ -z "$1" ]; then
24     if uname -m | grep -q '^arm'; then
25         export ISRASPI=true
26     fi
27
28     if iconf -i noautostart; then
29         echo "autostart disabled"
30         exit
31     fi
32
33     if [ "$(ps aux | grep bash | grep instantautostart | wc -l)" -gt 3 ]; then
34         echo "already running"
35         exit
36     fi
37 else
38     echo "forced run"
39 fi
40
41 cd
42 if ! iconf -r keepdotfiles && ! iconf -i nodotfiles; then
43     command -v instantdotfiles && instantdotfiles
44 fi
45
46 if ! iconf -i rangerplugins && command -v rangerplugins; then
47     mkdir instantos
48     echo "installing ranger plugins"
49     mkdir -p ~/.config/ranger/plugins
50     cp -r /usr/share/rangerplugins/* ~/.config/ranger/plugins/
51     iconf -i rangerplugins 1
52 fi
53
54 # find out if it's a live session
55 if [ -e /usr/share/liveutils ] &>/dev/null; then
56     ISLIVE="True"
57     echo "live session detected"
58 fi
59
60 if iconf -i islaptop; then
61     export ISLAPTOP="true"
62     echo "laptop detected"
63 else
64     echo "not a laptop"
65 fi
66
67 islive() {
68     if [ -n "$ISLIVE" ]; then
69         return 0
70     else
71         return 1
72     fi
73 }
74
75 applymouse() {
76     # read cursor speed
77     if iconf mousespeed; then
78         echo "setting mousespeed"
79         instantmouse s "$(iconf mousespeed)"
80     fi
81
82     if iconf -i reversemouse; then
83         instantmouse r 1
84     else
85         instantmouse r 0
86     fi
87 }
88
89 # optionally disable status bar
90 if iconf -i nobar; then
91     NMON=$(iconf names | wc -l)
92     for i in $(eval "echo {1..$NMON}"); do
93         echo "found monitor $i"
94         xdotool key super+comma
95         xdotool key super+b
96     done &
97 fi
98
99 if [ -n "$ISRASPI" ]; then
100     # enable double drawing for moving floating windows
101     # greatly increases smoothness
102     iconf -i highfps 1
103     if ! [ -e ~/.config/instantos/israspi ]; then
104         echo "marking machine as raspi"
105         mkdir -p ~/.config/instantos
106         touch ~/.config/instantos/israspi
107         # logo does not work on raspi
108         iconf -i nologo 1
109     fi
110 fi
111
112 if ! iconf -i notheming; then
113     instantthemes a
114     xrdb ~/.Xresources
115     iconf -i instantthemes 1
116
117     # dynamically switch between light and dark gtk theme
118     DATEHOUR=$(date +%H)
119     if [ "$DATEHOUR" -gt "20" ] || [ "$DATEHOUR" -lt "7" ]; then
120         instantthemes d &
121         touch /tmp/instantdarkmode
122         [ -e /tmp/instantlightmode ] && rm /tmp/instantlightmode
123     else
124         instantthemes l &
125         touch /tmp/instantlightmode
126         [ -e /tmp/instantdarkmode ] && rm /tmp/instantdarkmode
127     fi &
128 else
129     touch /tmp/instantlightmode
130 fi
131
132 mkdir -p /tmp/notifications &>/dev/null
133 if ! pgrep dunst; then
134     while :; do
135         # wait for theming before starting dunst
136         if [ -e /tmp/instantdarkmode ] || [ -e /tmp/instantlightmode ]; then
137             dunst
138         fi
139         sleep 2
140     done &
141 fi
142
143 onlinetrigger() {
144     if ! iconf -i nowallpaper; then
145         instantwallpaper &
146     fi
147 }
148
149 # set up oh-my-zsh config if not existing already
150 iconf -i nozsh || instantshell &
151
152 # fix resolution on virtual machine
153 if ! iconf -i novmfix && grep -q 'hypervisor' /proc/cpuinfo; then
154     # indicator file only exists on kvm/QEMU on amd
155     if [ -e /opt/instantos/kvm ]; then
156         iconf -i highfps 1
157         if lshw -c video | grep -i 'qxl' || xrandr | grep -i '^qxl'; then
158             iconf -i qxl 1
159             # iconf -i noanimations 1
160             if ! iconf -i potato && ! iconf -i nopotato; then
161                 if echo "please set your video card to virtio or passthrough
162 QXL on AMD on QEMU/kvm has been known to cause a severe Xorg memory leak. 
163 Disabling compositing makes this somewhat bearable,
164 but switching really is recommended.
165 (or switch to virtualbox, no issues there...)
166 Disable compositing for this VM?" | imenu -C; then
167                     iconf -i potato 1
168                     pkill picom
169                 else
170                     if ! imenu -c "ask again next time?"; then
171                         iconf -i nopotato 1
172                     fi
173                 fi
174             fi
175         else
176             iconf -i qxl 1
177         fi
178     fi
179
180     if ! [ -e /opt/instantos/guestadditions ]; then
181         if lsmod | grep -q vboxguest; then
182             echo "guestadditions detected"
183         else
184             if echo "virtual machine detected.
185 Would you like to switch to a 1080p resolution?" | imenu -C; then
186                 echo "applying virtual machine workaround"
187                 /usr/share/instantassist/assists/t/v.sh
188             else
189                 if [ -z "$ISLIVE" ]; then
190                     if ! imenu -c "ask again next session"; then
191                         iconf -i novmfix 1
192                     fi
193                 fi
194             fi
195         fi
196     fi
197 fi
198
199 if ! islive; then
200     echo "not a live session"
201     if [ -e /opt/instantos/installtrigger ]; then
202         zenity --info --text "finishing installation in background" &
203
204         # ask for password if postinstall already ran
205         if ! timeout 2 sudo echo test; then
206             instantsudo instantpostinstall
207             sudo rm /opt/instantos/installtrigger
208         else
209             sudo instantpostinstall
210         fi
211
212         pkill zenity
213     fi
214
215     cd ~/instantos || exit 1
216     if ! iconf -i max; then
217         instantmonitor
218     fi
219
220     if [ -e ~/instantos/monitor.sh ]; then
221         echo "restoring resolution"
222         bash ~/instantos/monitor.sh &
223     elif [ -e ~/.config/autorandr/instantos/config ]; then
224         echo "restoring autorandr resolution"
225         autorandr instantos &
226     fi
227
228     if checkinternet; then
229         onlinetrigger
230     else
231         # fall back to already installed wallpapers
232         instantwallpaper offline
233         for i in $(seq 10); do
234             if checkinternet; then
235                 onlinetrigger
236                 break
237             else
238                 sleep 10
239             fi
240         done
241     fi &
242
243     # apply keybpard layout
244     if ! iconf layout; then
245         if iconf -r layout; then
246             iconf layout "$(iconf -r layout)"
247         fi
248     fi
249
250     KEYLAYOUT="$(iconf layout:us)"
251     setxkbmap -layout "$KEYLAYOUT"
252
253     if ! iconf -i noconky; then
254         shuf /usr/share/instantwidgets/tooltips.txt | head -1 >~/.cache/tooltip
255         conky -c /usr/share/instantwidgets/tooltips.conf &
256     fi
257
258 else
259     echo "live session detected"
260     instantmonitor
261
262     echo "disabling compositing for qxl graphics"
263     if lshw -c video | grep -i 'qxl' || xrandr | grep -i '^qxl'; then
264         iconf -i potato 1
265     fi
266
267     sudo systemctl start NetworkManager
268
269     iconf -b welcome 1
270     iconf -i wifiapplet 1
271     instantwallpaper set /usr/share/instantwallpaper/defaultphoto.png
272     conky -c /usr/share/instantwidgets/install.conf &
273     sleep 0.3
274     while :; do
275         if ! pgrep python; then
276             installapplet
277         fi &
278         sleep 6m
279     done &
280     sleep 1
281 fi
282
283 # make built in status optional
284 if ! iconf -i nostatus; then
285     source /usr/bin/instantstatus &
286 fi
287
288 offerdpi() {
289     HEIGHT=$(iconf max | grep -o '[0-9]*$')
290     WIDTH=$(iconf max | grep -o '^[0-9]*')
291     RESOLUTION="$((HEIGHT * WIDTH))"
292     DPIMESSAGE="HiDpi settings can be found in settings->display->dpi"
293     if ! imenu -C <<<"high resolution display detected
294 would you like to enable HiDpi?"; then
295         if imenu -c "ask again next time?"; then
296             return
297         fi
298         iconf -i nohidpi 1
299         imenu -m "$DPIMESSAGE"
300         return
301     fi
302
303     DPI=$(imenu -i 'enter dpi (default is 96)')
304     while ! [ "$DPI" -eq "$DPI" ] || [ "$DPI" -gt 500 ] || [ "$DPI" -lt "20" ]; do
305         imenu -m "please enter a number between 20 and 500 (default is 96), enter q to skip hidpi"
306         DPI=$(imenu -i 'enter dpi (default is 96)')
307         if grep -q 'q' <<<"$DPI"; then
308             imenu -m "$DPIMESSAGE"
309             return
310         fi
311     done
312
313     iconf dpi "$DPI"
314
315     instantdpi
316     xrdb ~/.Xresources
317     imenu -m "a restart is needed to globally apply dpi"
318
319 }
320
321 if ! iconf -i nohidpi && iconf max; then
322     if [ "$RESOLUTION" -gt 8294000 ]; then
323         offerdpi
324     fi
325 fi
326
327 # compositing
328 if iconf -i potato || iconf -i nocompositing; then
329     echo "compositing disabled"
330 else
331     ipicom &
332 fi
333
334 xfce4-power-manager &
335
336 # auto open menu when connecting/disconnecting monitor
337 if ! (iconf -i noautoswitch && iconf -i islaptop) || iconf -i autoswitch; then
338
339     if nvidia-xconfig --query-gpu-info; then
340         DISPLAYCOUNT="$(nvidia-xconfig --query-gpu-info | grep -oi 'number of dis.*' | grep -o '[0-9]*')"
341     else
342         DISPLAYCOUNT="$(xrandr | grep -c '[^s]connected')"
343     fi
344
345     if [ "$DISPLAYCOUNT" -eq "$DISPLAYCOUNT" ]; then
346         while :; do
347             NEWDISPLAYCOUNT="$(xrandr | grep -c '[^s]connected')"
348             if ! [ "$DISPLAYCOUNT" = "$NEWDISPLAYCOUNT" ]; then
349                 notify-send "display changed"
350                 echo "displays changed"
351                 if [ "$NEWDISPLAYCOUNT" -gt 1 ]; then
352                     instantdisper
353                     echo "multi monitor setup"
354                 else
355                     disper -e
356                 fi
357                 DISPLAYCOUNT="$NEWDISPLAYCOUNT"
358                 # todo: open menu
359             fi
360             sleep 10
361         done &
362     else
363         echo "error detecting display count"
364     fi
365 fi
366
367 # welcome app
368 if iconf -b welcome; then
369     instantwelcome
370 fi &
371
372 # prompt to fix configuration if installed from the AUR
373 if ! iconf -i norootinstall && ! islive; then
374     if ! command -v imenu || ! command -v instantmenu; then
375         notify-send "please install instantmenu and imenu"
376     else
377         if ! [ -e /opt/instantos/rootinstall ]; then
378             imenu -m "instantOS is missing some configuration"
379             while ! [ -e /tmp/rootskip ]; do
380                 if imenu -c "would you like to fix that?"; then
381                     touch /tmp/topinstall
382                     instantsudo bash -c "instantutils root"
383                     touch /tmp/rootskip
384                 else
385                     if imenu -c "Are you sure? this will prevent parts of instantOS from functioning correctly"; then
386                         touch /tmp/rootskip
387                     fi
388                 fi
389
390             done
391         fi
392     fi
393 fi
394
395 TODAY="$(date '+%d%m')"
396 OTHERTODAY="$(iconf today)"
397
398 if [ -z "$OTHERTODAY" ]; then
399     iconf today "$(date '+%d%m')"
400     OTHERTODAY="$(iconf today)"
401 fi
402
403 if ! [ "$TODAY" = "$OTHERTODAY" ]; then
404     iconf today "$(date '+%d%m')"
405     echo "running daily routine"
406     menuclean
407 fi &
408
409 # displays message user opens the terminal for the first time
410 if ! iconf -i nohelp; then
411     if ! grep -q 'instantterminalhelp' ~/.zshrc; then
412         echo '[[ $- == *i* ]] && instantterminalhelp' >>~/.zshrc
413     fi
414 fi
415
416 confkey() {
417     [ -n "$2" ] || return
418     iconf -i "$1" || return
419     xdotool key "$2"
420 }
421
422 # run command if iconf option is set
423 confcommand() {
424     if iconf -i "$1"; then
425         shift 1
426         "$@"
427     fi &
428 }
429
430 if iconf savebright; then
431     export NOBRIGHTMESSAGE=true
432     /usr/share/instantassist/utils/b.sh 2 "$(iconf savebright)"
433 fi
434
435 confkey highfps "super+alt+shift+d"
436 confkey noanimations "super+alt+shift+s"
437
438 if iconf -i alttab; then
439     instantwmctrl alttab 3
440 else
441     instantwmctrl alttab 1
442 fi
443
444 # desktop icons
445 confcommand desktopicons rox --pinboard Default
446 # auto mount disks
447 confcommand udiskie udiskie -t
448 # clipboard manager
449 confcommand clipmanager clipmenud
450
451 # user declared autostart
452 if [ -e ~/.config/instantos/autostart.sh ]; then
453     bash ~/.config/instantos/autostart.sh
454 fi &
455
456 # update notifier
457 if ! iconf -i noupdates && [ -z "$ISLIVE" ]; then
458     sleep 2m
459     if checkinternet; then
460         instantupdatenotify
461     else
462         if command -v checkinternet; then
463             while :; do
464                 sleep 5m
465                 if checkinternet; then
466                     instantupdatenotify
467                     break
468                 fi
469             done
470         fi
471     fi
472 fi &
473
474 # needed for things like the pamac auth prompt
475 while :; do
476     lxpolkit
477     sleep 2
478 done &
479
480 # start processes that need to be kept running
481 while :; do
482     sleep 2
483     # check if new device has been plugged in
484     XINPUTSUM="$(xinput | md5sum)"
485     if ! [ "$OLDXSUM" = "$XINPUTSUM" ]; then
486         OLDXSUM="$XINPUTSUM"
487         instantmouse gen
488         applymouse
489     fi
490     sleep 2
491     if iconf -i wifiapplet && ! pgrep nm-applet; then
492         echo "starting wifi applet"
493         nm-applet &
494     fi
495     sleep 2
496     if iconf -i bluetoothapplet && ! pgrep blueman-applet; then
497         echo "starting bluetooth applet"
498         blueman-applet &
499     fi
500
501     if iconf -i alttab && ! pgrep alttab; then
502         alttab -fg "#ffffff" -bg "#292F3A" -frame "#5293E1" -d 0 -s 1 -t 128x150 -i 127x64 -w 1 -vp pointer &
503     fi
504
505     sleep 1m
506 done