#!/bin/bash

# tint2session - script to start user save tint2 config files
# modfied by MX Linux
# based on bl-tint2-session script by damo <damo@bunsenlabs.org>
#
#----------------------------------------------------------------------
#    bl-tint2-session: a script to read a saved Tint2 session file
#     and start the tint2s
#    Copyright (C) 2015 damo    <damo@bunsenlabs.org>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#   To start the chosen Tint2 session at login, add the following line
#   to config/openbox/autostart:
#
#      bl-tint2-session
#
########################################################################
VERSION="2021-08-04.02"
TINT2PATH="$HOME/.config/tint2"
SESSIONFILE="$TINT2PATH/tint2-sessionfile"
USAGE='tint2session is a script to parse a saved Tint2 session file
and start the tint2s.

Usage: tint2session
   or: tint2session --help|-h | --version|-v 

Optional arguments:
    --help | -h
        show this message
    --version | -v
        show version

The script uses the default tint2 session file
~/.config/tint2/tint2-sessionfile if it exists,
otherwise the default tint2rc is used.

To start the tint2 session at login,
add the following line to autostart:

    tint2session
'

# Kill only with exact match, and only if process belongs to executor.
# Usage: safeKill <processname>
function safeKill() {
    #pkill -x -u "$USER" "$1"
    killall tint2 2>/dev/null
}

findArgs(){     # get command args
    for arg in "$@";do
        if [[ $1 = "-h" ]] || [[ $1 = "--help" ]]; then
            echo "$USAGE"
            exit 0
        elif [[ $1 = "-v" ]] || [[ $1 = "--version" ]]; then
            echo "version $VERSION"
            exit 0
        else
            echo -e "\tERROR: sorry I don't understand the input"
            echo "$USAGE"
            exit 0
        fi
    done
}

testSessionfile(){
    safeKill tint2
    if test -f  "$1" ;then # sessionfile found
        while read -r line; do
            [ -f "$line" ] || continue
            tint2 -c "$line" >/dev/null 2>&1 & disown
            #sleep 1s
        done < <( sed -r -e '
                  s!(^[[:space:]]*|[[:space:]]*$)!!;          # remove leading/trailing spaces
                  s!^([$]HOME|[$][{]HOME[}]|~)!'"$HOME"'!;       
                  s!^.config/tint2!'"$HOME"'/.config/tint2!;
                  '  "$1"
                )
    else
        [ -d "$TINT2PATH" ] || mkdir -p "$TINT2PATH"
        
        echo -e "Created sessionfile \"$1\"\nRunning default tint2rc" 2>&1
        echo '$HOME'"/.config/tint2/tint2rc" > "$SESSIONFILE"
        tint2 -c "$TINT2PATH/tint2rc" >/dev/null 2>&1 &  disown
    fi
}

if [ $# -eq 0 ];then    # if no args, then run default sessionfile
    testSessionfile "$SESSIONFILE"
else
    findArgs "$@"
fi

# restart compositor in case of rendering problems
#bl-compositor --restart

exit 0
