#!/bin/bash
# cb-compton
# Openbox Pipe Menu for use with compton compositor
# Written for CrunchBang Linux <http://crunchbang.org/>
# by Philip Newborough <corenominal@corenominal.org>


# ------------- Set xcompmgr command options -----------------------------------
if glxinfo | egrep -iq 'direct rendering: yes'; then
    EXECXCOMP='compton --vsync opengl'
else
    EXECXCOMP='compton'
fi

# Edit xcompmgr settings
if [ "$1" = "--edit" ]; then
    if [ ! -f /home/$USER/.config/compton.conf ]; then
        cp /etc/xdg/compton.conf /home/$USER/.config/compton.conf
    fi
    if [ -x "/usr/bin/geany" ]; then
        geany /home/$USER/.config/compton.conf &
    else
        terminator --command="nano /home/$USER/.config/compton.conf"
    fi
    exit 0
fi

# Toggle compositing with compton. Called with "cb-compositor --toggle"
if [ "$1" = "--toggle" ] || [ "$1" = "--start" ]; then 
    if [ ! "$(pidof compton)" ]; then
      $EXECXCOMP &
    else
      killall compton
    fi
    exit 0
fi

if [ "$1" = "--restart" ]; then
    if [ "$(pidof compton)" ]; then
        killall compton && sleep 1s
        cb-compositor --start
    fi
    exit 0
fi
if [ "$1" = "--watch" ]; then
    while inotifywait -e modify /home/$USER/.config/compton.conf; do
        cb-compositor --restart
    done
    exit 0
fi
# Output Openbox menu
if [ ! "$(pidof compton)" ]; then
    cat << _EOF_
    <openbox_pipe_menu>
	    <item label="Enable Compositing">
            <action name="Execute">
				<command>
					cb-compositor --start
				</command>
			</action>
		</item>
		<item label="Edit Compositing Settings">
            <action name="Execute">
				<command>
					cb-compositor --edit
				</command>
			</action>
		</item>
    </openbox_pipe_menu>
_EOF_
else
    cat << _EOF_
    <openbox_pipe_menu>
				<item label="Restart Compositing">
					<action name="Execute">
						<command>
							cb-compositor --restart
						</command>
					</action>
				</item>
				<item label="Disable Compositing">
					<action name="Execute">
						<command>
							cb-compositor --toggle
						</command>
					</action>
				</item>
                <separator/>
                <item label="Edit Compositing Settings">
                    <action name="Execute">
                        <command>
                            cb-compositor --edit
                        </command>
                    </action>
                </item>
    </openbox_pipe_menu>
_EOF_
fi
exit 0
