#!/bin/sh

command="`basename \"$0\"`"
matchdir="$HOME/.matchbox"
mkdir -p "$HOME/.matchbox"
chown $(stat -C %U "$HOME"): "$HOME/.matchbox"
startup="$matchdir/startup"

if [ -x "$startup" ]; then
    exec "$startup"
elif [ -r "$startup" ]; then
    exec sh "$startup"
else
    if [ ! -r "$startup" ]; then
        ( cat << EOF
#!/usr/bin/sh
# Upon typing startx in a console aka tty, this script starts
# matchbox-window-manager.

# Background image
xloadimage -onroot -fullscreen /usr/share/backgrounds/misc/3840x2160-desert.jpg &

# Keyboard and autostarts
setxkbmap 
grep -q "Hidden=false" ~/.config/autostart/startplank.desktop && plank &
grep -q "Hidden=false" ~/.config/autostart/startorca.desktop && orca -r &
grep -q "Hidden=false" ~/.config/autostart/startonboard.desktop && onboard &

# When you are OK with your X settings, possibly from another graphical
# environment, type: dump_xsettings > ~/.xsettingsd
# then xsettingsd will restore these settings.
xsettingsd &

# ---------------------------
# SCREEN BLANKING SETTINGS
# ---------------------------

# Enable DPMS
xset +dpms

# Blank after 1 minute (60 seconds)
xset s 60
xset s blank

# Turn monitor off after blanking (also 60 sec)
xset dpms 0 0 60

# Force screen off after 1 minute of real inactivity.
xautolock -detectsleep -time 1 -locker "xset dpms force off" &

# ---------------------------
# START WINDOW MANAGER
# ---------------------------
exec dbus-run-session matchbox-window-manager -use_desktop_mode decorated

#Themes shipped in Slint, arguments of -theme option of matchbox-window manager 
# Default
# Industrialbof
# Clearlooks
# expose rond rouge à droite
# mbcrystal bad
# MBOpus bad
# blondie bleu ciel
# bluebox bad
# borillo bad
# Set up xautolock for key press to wake up the screen
#xautolock -detectsleep -time 5 -locker "xset dpms force off" &  # Delay of 5 mins for blanking

# To know more, read /usr/doc/matchbox-window-manager*/matchbox-manual.html,
# useful altghough possibly outdated.

EOF
    ) > "$startup"
    fi
    chmod 644 "$startup"
    exec sh "$startup"
fi
