#!/bin/bash
# mabox-compositor
# Openbox Pipe Menu for use with picom compositor
# Written for CrunchBang Linux <http://crunchbang.org/>
# by Philip Newborough <corenominal@corenominal.org>
# Ported to #!++ <https://crunchbangplusplus.org>
# by Ben Young <computermouth@crunchbangplusplus.org>
# Ported to Manjaro <https://manjaro.github.io/>
# by Daniel Napora <napcok@gmail.com>

RESTART_ATTEMPTS=20

if ! . mabox-include.cfg 2> /dev/null; then
    echo '  Failed to locate mabox-include.cfg in PATH' >&2
    exit 1
fi

case $LANG in
    pl*)
    ENABLE="Włącz Kompozytora"
    RESTART="Restartuj Kompozytora"
    DISABLE="Wyłącz Kompozytora"
    EDIT="Edytuj plik ustawień Kompozytora"
    EDIT_GUI="Ustawienia Kompozytora (GUI)"
    ;;
    es*)
    ENABLE="Activar Compositor"
    RESTART="Reiniciar Compositor"
    DISABLE="Desactivar Compositor"
    EDIT="Editar archivo de ajustes del Compositor"
    EDIT_GUI="Ajustes gráfico (GUI)"
    ;;
    *)
    ENABLE="Enable Compositor"
    RESTART="Restart Compositor"
    DISABLE="Disable Compositor"
    EDIT="Edit Compositor settings file"
    EDIT_GUI="Settings (GUI)"
    ;;
esac

# ------------- Set xcompmgr command options -----------------------------------
EXECXCOMP='picom'
#if glxinfo | grep 'direct rendering: Yes'; then
#    EXECXCOMP+=' --vsync opengl'
#fi

# Edit xcompmgr settings
if [[ $1 = '--edit' ]]; then
    [[ ! -f $HOME/.config/picom.conf ]] &&
        cp '/etc/xdg/picom.conf' "$HOME/.config/picom.conf"

    if [[ -x /usr/bin/geany ]]; then
        geany "$HOME/.config/picom.conf" &
    else
        terminator --command='nano "$HOME/.config/picom.conf"'
    fi
elif [[ $1 = '--toggle' || $1 = '--start' ]]; then # Toggle compositing with picom.
    [[ ! -f $HOME/.config/picom.conf ]] &&
        ln -sf /dev/null "$HOME/.config/picom.conf"
    # TODO why --toggle and --start act exactly the same?
    if ! pidof picom > /dev/null; then
        $EXECXCOMP &
    else
        killall picom
    fi
elif [[ $1 = '--restart' ]]; then
    if pidof picom > /dev/null;then
        killall -q picom
        for (( i=0; i < RESTART_ATTEMPTS; i++ )); do
            pidof picom > /dev/null || # no process found! Safe to start again
                break

            (( i == RESTART_ATTEMPTS - 1 )) && # still didn't die? Probably hangs. Force it to die!
                killall -q -S KILL picom

            sleep 0.25
        done
        mabox-compositor --start
    fi
elif [[ $1 = '--watch' ]]; then
    while inotifywait -e modify "$HOME/.config/picom.conf"; do
        mabox-compositor --restart 
    done
else
    # Output Openbox menu
    menuStart
    menuSeparator "Picom"
    if ! pidof picom > /dev/null; then
        menuItem "$ENABLE" 'mabox-compositor --start'
    else
        menuItem "$RESTART" 'mabox-compositor --restart'
        menuItem "$DISABLE" 'mabox-compositor --toggle'
    fi
    menuSeparator
    menuItem "$EDIT" 'mabox-compositor --edit'
    #menuItem "$EDIT_GUI" 'compton-conf'
    menuEnd
fi
exit 0