#!/bin/sh
#set -x
#
# fluxbox startup-script:
#
# Lines starting with a '#' are ignored.

# --- start logging setup ---
LOGFILE="${HOME}/.fluxbox/startup.log"
mkdir -p "$(dirname "$LOGFILE")"
exec >"$LOGFILE" 2>&1
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Fluxbox startup script started"
# --- end logging setup ---

# menu generation based on the current locale
fbmenu >${HOME}/.fluxbox/menu

# desktop file generation based on the current locale
fbdesktop

# Change your keymap:
xmodmap "${HOME}/.Xmodmap"

# merge xresource settings
xrdb -merge ~/.Xresources

# set keyboard layout
echo "$(setxkbmap -query | grep "layout" | awk '{print $2}')" >~/.fluxbox/kblayout
if [ -n $(cat ~/.fluxbox/kblayout) ]; then
    fbsetkb $(cat ~/.fluxbox/kblayout)
fi

# setup bookmarks for file manager

if [ "$(id -u)" -eq 0 ]; then
    gtk-bookmarks-update
fi

if [ "$(id -u)" -eq 0 ]; then
    # Share common directories with guest user. This is necessary
    # because some apps like chromium must be running under guest
    for dir in Desktop Documents Downloads Music Pictures Public Templates Videos; do
        if ! mountpoint ${HOME}/$dir; then
            mount --bind /home/guest/$dir ${HOME}/$dir
        fi
    done
fi

# set background color and big wait mouse cursor
xsetroot -solid '#111111'
xsetroot -xcf /usr/share/icons/breeze_cursors/cursors/watch 37

# disable screen blanking
xset s off
xset -dpms

# preload compton and fluxbox to cache
(
    compton --help
    fluxbox --help
    xfce4-panel --help
) >/dev/null 2>&1

# Keep black screen for first second while the sound plays.
# This slows startup a little, but it is nicer effect.
SND=/usr/share/sounds/startup.wav
if [ -r $SND ]; then
    cat $SND >/dev/null # preload
    aplay $SND &
    sleep 1 &
    SOUNDPID=$!
fi

# Debian-local change:
#   - fbautostart has been added with a quick hack to check to see if it
#     exists. If it does, we'll start it up by default.
which fbautostart >/dev/null
if [ $? -eq 0 ]; then
    fbautostart &
fi

# We need to postpone compton after fluxbox starts, else it won't set
# the transparency of toolbar properly... So we check the fehbg file,
# which is created by fluxbox at the phase when it sets background.
# Once the file exist, fluxbox-toolbar is already started

SIGNAL1=~/.fehbg
SIGNAL2=~/.fehbg2

rm -f $SIGNAL1 2>/dev/null
rm -f $SIGNAL2 2>/dev/null

(
    while [ ! -e $SIGNAL1 ]; do
        sleep 0.1
    done
    touch $SIGNAL2

    exec compton --sw-opti --shadow-exclude 'class_g="xlunch-windowed"' --fade-exclude 'role*="fluxbox-toolbar"' --opacity-rule '70:role*="fluxbox-toolbar"' --no-fading-destroyed-argb -D 5 -c -f -l -2 -t -2 -r 3 -o 1 -z --shadow-exclude 'bounding_shaped'
) &

# We wait after background is set and compton is started
(
    while [ ! -e $SIGNAL2 ]; do
        sleep 0.1
    done

    rm -f $SIGNAL2

    exec volumeicon
) &

# gen screen resolutions to fluxbox menu
xrandr 2>/dev/null | fgrep x | fgrep . | sort -n | tr -s " " | cut -d " " -f 2 |
    sed -r "s:(.*):[exec] (\\1) {fbscreensize \\1}:" >~/.fluxbox/menu_resolution

# In all cases, wait for the sound sleeper.
# If startup took longer (sound completed already), no extra wait
wait $SOUNDPID

# If we are running inside virtual environment,
# switch to bigger screen size right away

# Function to check if running in a virtual environment
is_virtual() {
    grep -qEi "(VirtualBox|VMware|KVM|QEMU|Xen|microsoft corporation|Bochs)" /sys/class/dmi/id/product_name 2>/dev/null || grep -qEi "(Oracle|microsoft corporation|Bochs)" /sys/class/dmi/id/sys_vendor 2>/dev/null
}

# Function to check if guest utilities are installed and running
guest_utils_active() {
    pgrep -f 'vboxadd|vmtoolsd|qemu-ga|hv_utils' >/dev/null 2>&1 || [[ -d /proc/xen ]]
}

if is_virtual && ! guest_utils_active; then
    #    fbscreensize 1440x900 -n
    fbscreensize 1280x800 -n
fi

# start xfce4 panel in background
xfce4-panel --disable-wm-check --sm-client-disable &

(
    while [ "$(wmctrl -l | grep xfce4-panel)" = "" ]; do
        sleep 0.1
    done
    xprop -id $(wmctrl -l | grep xfce4-panel | cut -d " " -f 1) -format _NET_WM_STRUT 32c -set _NET_WM_STRUT "0,0,0,52"
) &

# start keyboard layout switcher if available
case $(cat ~/.fluxbox/kblayout) in
us) ;;
us,us) echo "us" >~/.fluxbox/kblayout ;;
*)
    command -v fbxkb >/dev/null 2>&1 && fbxkb &
    ;;
esac

# start network manager applet if available
command -v nm-applet >/dev/null 2>&1 && nm-applet &

# And last but not least we start fluxbox.
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Fluxbox startup script finished"

# Set QT themes
export QT_STYLE_OVERRIDE=gtk
export QT_QPA_PLATFORMTHEME=gtk2

exec fluxbox
