OSDN Git Service

fix groups error message
[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         setxkbmap -layout "$(iconf layout)"
246     else
247         CURLOCALE=$(locale | grep LANG | sed 's/.*=\(.*\)\..*/\1/')
248         case "$CURLOCALE" in
249         de_DE)
250             setxkbmap -layout de
251             ;;
252         *)
253             echo "no keyboard layout found for your locale"
254             ;;
255         esac
256     fi
257
258     if ! iconf -i noconky; then
259         shuf /usr/share/instantwidgets/tooltips.txt | head -1 >~/.cache/tooltip
260         conky -c /usr/share/instantwidgets/tooltips.conf &
261     fi
262
263 else
264     echo "live session detected"
265     instantmonitor
266
267     echo "disabling compositing for qxl graphics"
268     if lshw -c video | grep -i 'qxl' || xrandr | grep -i '^qxl'; then
269         iconf -i potato 1
270     fi
271
272     sudo systemctl start NetworkManager
273
274     iconf -b welcome 1
275     iconf -i wifiapplet 1
276     instantwallpaper set /usr/share/instantwallpaper/defaultphoto.png
277     conky -c /usr/share/instantwidgets/install.conf &
278     sleep 0.3
279     while :; do
280         if ! pgrep python; then
281             installapplet
282         fi &
283         sleep 6m
284     done &
285     sleep 1
286 fi
287
288 # make built in status optional
289 if ! iconf -i nostatus; then
290     source /usr/bin/instantstatus &
291 fi
292
293 offerdpi() {
294     HEIGHT=$(iconf max | grep -o '[0-9]*$')
295     WIDTH=$(iconf max | grep -o '^[0-9]*')
296     RESOLUTION="$((HEIGHT * WIDTH))"
297     DPIMESSAGE="HiDpi settings can be found in settings->display->dpi"
298     if ! imenu -C <<<"high resolution display detected
299 would you like to enable HiDpi?"; then
300         if imenu -c "ask again next time?"; then
301             return
302         fi
303         iconf -i nohidpi 1
304         imenu -m "$DPIMESSAGE"
305         return
306     fi
307
308     DPI=$(imenu -i 'enter dpi (default is 96)')
309     while ! [ "$DPI" -eq "$DPI" ] || [ "$DPI" -gt 500 ] || [ "$DPI" -lt "20" ]; do
310         imenu -m "please enter a number between 20 and 500 (default is 96), enter q to skip hidpi"
311         DPI=$(imenu -i 'enter dpi (default is 96)')
312         if grep -q 'q' <<<"$DPI"; then
313             imenu -m "$DPIMESSAGE"
314             return
315         fi
316     done
317
318     iconf dpi "$DPI"
319
320     instantdpi
321     xrdb ~/.Xresources
322     imenu -m "a restart is needed to globally apply dpi"
323
324 }
325
326 if ! iconf -i nohidpi && iconf max; then
327     if [ "$RESOLUTION" -gt 8294000 ]; then
328         offerdpi
329     fi
330 fi
331
332 # compositing
333 if iconf -i potato || iconf -i nocompositing; then
334     echo "compositing disabled"
335 else
336     ipicom &
337 fi
338
339 xfce4-power-manager &
340
341 # auto open menu when connecting/disconnecting monitor
342 if ! (iconf -i noautoswitch && iconf -i islaptop) || iconf -i autoswitch; then
343
344     if nvidia-xconfig --query-gpu-info; then
345         DISPLAYCOUNT="$(nvidia-xconfig --query-gpu-info | grep -oi 'number of dis.*' | grep -o '[0-9]*')"
346     else
347         DISPLAYCOUNT="$(xrandr | grep -c '[^s]connected')"
348     fi
349
350     if [ "$DISPLAYCOUNT" -eq "$DISPLAYCOUNT" ]; then
351         while :; do
352             NEWDISPLAYCOUNT="$(xrandr | grep -c '[^s]connected')"
353             if ! [ "$DISPLAYCOUNT" = "$NEWDISPLAYCOUNT" ]; then
354                 notify-send "display changed"
355                 echo "displays changed"
356                 if [ "$NEWDISPLAYCOUNT" -gt 1 ]; then
357                     instantdisper
358                     echo "multi monitor setup"
359                 else
360                     disper -e
361                 fi
362                 DISPLAYCOUNT="$NEWDISPLAYCOUNT"
363                 # todo: open menu
364             fi
365             sleep 10
366         done &
367     else
368         echo "error detecting display count"
369     fi
370 fi
371
372 # welcome app
373 if iconf -b welcome; then
374     instantwelcome
375 fi &
376
377 # prompt to fix configuration if installed from the AUR
378 if ! iconf -i norootinstall && ! islive; then
379     if ! command -v imenu || ! command -v instantmenu; then
380         notify-send "please install instantmenu and imenu"
381     else
382         if ! [ -e /opt/instantos/rootinstall ]; then
383             imenu -m "instantOS is missing some configuration"
384             while ! [ -e /tmp/rootskip ]; do
385                 if imenu -c "would you like to fix that?"; then
386                     touch /tmp/topinstall
387                     instantsudo bash -c "instantutils root"
388                     touch /tmp/rootskip
389                 else
390                     if imenu -c "Are you sure? this will prevent parts of instantOS from functioning correctly"; then
391                         touch /tmp/rootskip
392                     fi
393                 fi
394
395             done
396         fi
397     fi
398 fi
399
400 TODAY="$(date '+%d%m')"
401 OTHERTODAY="$(iconf today)"
402
403 if [ -z "$OTHERTODAY" ]; then
404     iconf today "$(date '+%d%m')"
405     OTHERTODAY="$(iconf today)"
406 fi
407
408 if ! [ "$TODAY" = "$OTHERTODAY" ]; then
409     iconf today "$(date '+%d%m')"
410     echo "running daily routine"
411     menuclean
412 fi &
413
414 # displays message user opens the terminal for the first time
415 if ! iconf -i nohelp; then
416     if ! grep -q 'instantterminalhelp' ~/.zshrc; then
417         echo '[[ $- == *i* ]] && instantterminalhelp' >>~/.zshrc
418     fi
419 fi
420
421 confkey() {
422     [ -n "$2" ] || return
423     iconf -i "$1" || return
424     xdotool key "$2"
425 }
426
427 # run command if iconf option is set
428 confcommand() {
429     if iconf -i "$1"; then
430         shift 1
431         "$@"
432     fi &
433 }
434
435 if iconf savebright; then
436     export NOBRIGHTMESSAGE=true
437     /usr/share/instantassist/utils/b.sh 2 "$(iconf savebright)"
438 fi
439
440 confkey highfps "super+alt+shift+d"
441 confkey noanimations "super+alt+shift+s"
442
443 if iconf -i alttab; then
444     instantwmctrl alttab 3
445 else
446     instantwmctrl alttab 1
447 fi
448
449 # desktop icons
450 confcommand desktopicons rox --pinboard Default
451 # auto mount disks
452 confcommand udiskie udiskie -t
453 # clipboard manager
454 confcommand clipmanager clipmenud
455
456 # user declared autostart
457 if [ -e ~/.config/instantos/autostart.sh ]; then
458     bash ~/.config/instantos/autostart.sh
459 fi &
460
461 # update notifier
462 if ! iconf -i noupdates && [ -z "$ISLIVE" ]; then
463     sleep 2m
464     if checkinternet; then
465         instantupdatenotify
466     else
467         if command -v checkinternet; then
468             while :; do
469                 sleep 5m
470                 if checkinternet; then
471                     instantupdatenotify
472                     break
473                 fi
474             done
475         fi
476     fi
477 fi &
478
479 # needed for things like the pamac auth prompt
480 while :
481 do
482     lxpolkit
483     sleep 2
484 done &
485
486 # start processes that need to be kept running
487 while :; do
488     sleep 2
489     # check if new device has been plugged in
490     XINPUTSUM="$(xinput | md5sum)"
491     if ! [ "$OLDXSUM" = "$XINPUTSUM" ]; then
492         OLDXSUM="$XINPUTSUM"
493         instantmouse gen
494         applymouse
495     fi
496     sleep 2
497     if iconf -i wifiapplet && ! pgrep nm-applet; then
498         echo "starting wifi applet"
499         nm-applet &
500     fi
501     sleep 2
502     if iconf -i bluetoothapplet && ! pgrep blueman-applet; then
503         echo "starting bluetooth applet"
504         blueman-applet &
505     fi
506
507
508     if iconf -i alttab && ! pgrep alttab; then
509         alttab -fg "#ffffff" -bg "#292F3A" -frame "#5293E1" -d 0 -s 1 -t 128x150 -i 127x64 -w 1 -vp pointer &
510     fi
511
512     sleep 1m
513 done