#! /bin/bash

# toggletint2 - script to switch between fluxbox-toolbar and tint2-panel
# fehlix, 28. Aug. 2020
# 30. Aug. 2020 - adjusted togglerefresh call
# 06. Sep. 2020 - adjusted marker line handling
# 09. Sep. 2020 - adjusted tint2 startup
# 23. Jul. 2021 - added tint2session

FLBINIT="$HOME/.fluxbox/init"
REFRESH="togglerefresh"
STARTUP="$HOME/.fluxbox/startup"
BACKDIR="$HOME/.restore/fluxbox"
INITBAK="$BACKDIR/init.$(date +%Y%m%d_%H%M%S).bak"
STRTBAK="$BACKDIR/startup.$(date +%Y%m%d_%H%M%S).bak"

# make backup dir
if [ ! -d "$BACKDIR" ]; then
    mkdir -p "$BACKDIR"
fi

# check tint2 is running
if pgrep -x tint2 >/dev/null; then

    # stop tint2
    pkill -x tint2 2>/dev/null

    # disable tint2 autostart
    if grep -sq -E '^[[:space:]]*(/usr/bin/)?tint2' "$STARTUP"; then
       cp "$STARTUP" "$STRTBAK"
       sed  -i -r '\!^[[:space:]]*((/usr/bin/)?tint2(session)?\b.*)!s::#\1:' "$STARTUP"
    fi

    # toogle fluxbox  toolbar on: enable systray and make visible
    #
    # check we have a marker line starting with '#toggled-off#'
    if grep -sq -E '^#toggled-off#session.screen[0-9].toolbar.tools:'   "$FLBINIT"; then
        cp "$FLBINIT" "$INITBAK"
        sed -i '/session.screen[0-9].toolbar.visible:/d; # remove any visible line
                /^session.screen[0-9].toolbar.tools:/d;  # remove none marked lines
                0,/^#toggled-off#session.screen[0-9].toolbar.tools:/{ # only first marked
                  /^#toggled-off#session.screen[0-9].toolbar.tools:/{ # only first marked
                    s/^#toggled-off#//;           # remove the marker
                    h;                            # copy to hold
                    p;                            # print out
                    x;                            # xchage hold
                    s/tools:.*/visible: true/;    # make visible
                    };
                };
                /^#toggled-off#/d;                 # remove any other marked
                ' "$FLBINIT"
        # refresh fluxbox
        "$REFRESH"
    else
       # no marker lines found
       # check for toolbar-tools line
        if grep -sq -E '^session.screen[0-9].toolbar.tools'   "$FLBINIT"; then
           # make it visible
            sed -i '/session.screen[0-9].toolbar.visible/d; # remove visible line
                    /^session.screen[0-9].toolbar.tools/{
                    h;                                       # copy to hold
                    # insert systray if not already
                    /systemtray/!s/clock/systemtray, clock/; # in front of clock
                    /systemtray/!s/$/, systemtray/;          # else append
                    p;                                       # print out
                    x;                                       # xchange hold
                    s/tools:.*/visible: true/;               # make visible
                    }' "$FLBINIT"
        # refresh fluxbox
        "$REFRESH"
        fi
    fi

else
    # enable tint2 autostart
    cp "$STARTUP" "$STRTBAK"
    # check tint2 startup line exists
    if grep -sq -E '^[[:space:]#]*(/usr/bin/)?tint2' "$STARTUP"; then
       # uncomment tint2 startup line
       #
       sed -i  -r '  \!^[[:space:]#]*((/usr/bin/)?tint2\b.*)!s::#\1:;        # comment out all tint2 lines
                   0,\!^[[:space:]#]*((/usr/bin/)?tint2\b.*)!s::\1:;         # uncomment first found only
                     \!^[[:space:]#]*((/usr/bin/)?tint2session\b.*)!s::#\1:; # comment out all tint2session lines
                   0,\!^[[:space:]#]*((/usr/bin/)?tint2session\b.*)!s::\1:;  # uncomment first found only
                  ' "$STARTUP"
    else
       # inject tint2 startup line after "export QT_PLATFORM_PLUGIN" line
       if grep -sq -E '^[[:space:]]*export[[:space:]]+QT_PLATFORM_PLUGIN' "$STARTUP"; then
          # add tint startup line after first QT_PLATFORM_PLUGIN line found
          #
          sed -i -r '0,/export QT_PLATFORM_PLUGIN/{
				       /export QT_PLATFORM_PLUGIN/a\\n#add tint2 panel\ntint2 & 1>/dev/null 2>/dev/null\n
				     }' "$STARTUP"
       else
		  # no line found with QT_PLATFORM_PLUGIN
		  # so add before exec fluxbox
          sed -i -r '\!^[[:space:]]*exec[[:space:]]+(/usr/bin/)?fluxbox!{
				   	 i\#add tint2 panel\ntint2 & 1>/dev/null 2>/dev/null\n
				     }'  "$STARTUP"
	   fi
    fi
    # prepare tint2
    #
    # check toolbar.tools line exists
    # create copy marked with '#toggled-off'
    # set toolbar.visble to false
    if grep -sq  -E '^session.screen[0-9].toolbar.tools' "$FLBINIT" ; then
       sed -i '/^#toggled-off#/d;                        # remove any marker lines
               /^session.screen[0-9].toolbar.visible/d;  # remove any visible lines
               /^session.screen[0-9].toolbar.tools/{
               h;                                        # copy to hold buffer
               s/^/#toggled-off#/;                       # mark as toggled-off
               p;                                        # print out
               x;                                        # xchange with hold buffer
               h;                                        # copy back to hold buffer
               s/[[:space:]]*systemtray[[:space:]]*//;   # remove systray
               s/[[:space:]]*,,[[:space:]]*/, /;         # remove double comma
               s/,[[:space:]]*$//;                       # remove trailing comma
               p;                                        # print out
               x;                                        # get hold buffer
               s/tools:.*/visible: false/;               # make visible false
               }' "$FLBINIT"
    fi
    # refresh fluxbox
    "$REFRESH"
    # add tint2 to panel
    tint2 2>/dev/null >/dev/null & disown
fi
exit 0

