OSDN Git Service

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