#!/usr/bin/env bash
#
# polyzen: polybar session manager script for Archlabs
# Copyright (C) 2017 Nathaniel Maia <natemaia10@gmail.com>
#
# forked by manjaro <fhatmanjaroorg>
#

readonly COMMON_INCLUDE="/usr/lib/manjaro/common/include.cfg"
if ! . "$COMMON_INCLUDE" 2>/dev/null; then
    echo $"Error: Failed to source $COMMON_INCLUDE" >&2 ; exit 1
fi

readonly TITLE="Polybar Manager"
readonly ICON="--window-icon=/usr/share/icons/manjaro/maia/48x48.png"
readonly CHECKLIST="zenity --width=400 --height=500 $ICON --list --checklist --multiple"
readonly QUESTION="zenity --question $ICON --width=300"
readonly CONFIG_PATH="$HOME/.config/polybar"
readonly AUTOSTART="$HOME/.config/openbox/autostart"
readonly USAGE='
USAGE: manjaro-polyzen [OPTIONS] [SESSIONFILE]
\nOPTIONS:
\t-z, --session\tUse entered SESSIONFILE rather than default
\t-h, --help\tPrint this usage message and exit
\nWith no command options, the script uses the current WM-sessionfile
To start session at login, add the following line to autostart:
\tsleep 1; manjaro-polybar-session &'

declareDependencies polybar zenity find read

if ! [[ $WM ]]; then
    readonly window_Managers=(bspwm i3 openbox xfce awesome)
    for i in "${window_Managers[@]}"; do
        if [[ $(wmctrl -m | grep -i name | awk '{print tolower($2)}') == "$i" ]]; then
            readonly WM=$i ; break
        elif [[ $(xprop -root -notype | grep "WM_NAME =" | tr -d '"' | awk '{print tolower($3)}') == "$i" ]]; then
            readonly WM=$i ; break
        elif [[ $(awk '{print tolower($0)}' <<< "$XDG_CURRENT_DESKTOP") == "$i" ]]; then
            readonly WM=$i ; break
        fi
    done
fi

# SESSIONFILE is mutable by user
if [[ $WM ]]; then
    SESSIONFILE="$CONFIG_PATH/sessions/$WM-sessionfile"
else
    SESSIONFILE="$CONFIG_PATH/sessions/sessionfile"
fi
case $1 in
    -h|-H|--help) echo -e "$USAGE" ; exit 0 ;;
    -z|--session)
        if [[ $2 ]]; then
            SESSIONFILE=$2
        else
            echo "Invalid Session.. Exiting" ; exit 2
        fi
esac

if ! [[ -d $CONFIG_PATH ]] && [[ -d /etc/skel/.config/polybar ]]; then
    cp -rf /etc/skel/.config/polybar "$HOME/.config/"
elif ! [[ -d $CONFIG_PATH ]]; then
    mkdir -p "$CONFIG_PATH" ; cp -f /usr/share/doc/polybar/config "$CONFIG_PATH/config"
fi

running_Bars() {
    if [[ $(pidof polybar) ]]; then
        TEMPFILE=$(mktemp --tmpdir polybars.XXXX)
        pgrep -a polybar >> "$TEMPFILE"
    fi
}

readonly FILES=($(find -L "$CONFIG_PATH" -maxdepth 4 -type f))
find_Bars() {
    fill_Arrays() {
        barPath[$1]="$2"
        barArray[$1]="$3"
        if [[ -e $TEMPFILE ]] && grep -q "$2 $3" "$TEMPFILE"; then
            checkMark[$1]="TRUE"
        else
            checkMark[$1]="FALSE"
        fi
    }

    local n=0
    for config in "${FILES[@]}"; do
        bar_Names=($(grep '^\[bar/.*\]$' "$config" | sed 's/]//' | cut -d '/' -f2))
        for bar in "${bar_Names[@]}"; do
            [[ "$bar" != "master" ]] && fill_Arrays "$n" "$config" "$bar"
            n=$((n + 1))
        done
    done

    BARS=""
    for ((i=0; i<=${#barArray[@]}; i++)); do
        BARS="$BARS ${checkMark[$i]} ${barArray[$i]}"
    done
}


create_session() {
    mkdir -p "$CONFIG_PATH/sessions"
    echo -e "# Polybar $WM Sessionfile
# DO NO edit this file, it will be overwritten by manjaro-polyzen
# Instead make a custom sessionfile and use the -z FILE or --session FILE option
# To load session at startup use the following line
#
# sleep 1; manjaro-polybar-session &
#" > "$SESSIONFILE"
    for bar in $ANSWER; do
        for config in "${barPath[@]}"; do
            if grep -q "^\[bar/$bar\]" "$config" && ! grep -q "$config $bar" "$SESSIONFILE"; then
                echo "$config $bar" >> "$SESSIONFILE" ; break
            fi
        done
    done
    [[ $(pidof polybar) ]] && pkill polybar
    while read -r c b; do
        if [[ "$c $b" =~ ^#.*$ ]] || ! ([[ $c ]] && [[ $b ]]) || ! [[ -f "$c" ]]; then
            continue
        else
            polybar --reload --config="$c" "$b" &
        fi
    done < "$SESSIONFILE"

    sleep 0.5
    if [[ $(pidof polybar) ]]; then
        if [[ -f $AUTOSTART ]] && ! grep -q "manjaro-polybar-session" "$AUTOSTART"; then
            if grep -q "polybar" "$AUTOSTART"; then
                sed -i '/polybar/ c sleep 1; manjaro-polybar-session &' "$AUTOSTART"
            elif grep -q "tint2" "$AUTOSTART"; then
                sed -i '/tint2/ a sleep 1; manjaro-polybar-session &' "$AUTOSTART"
                # sed -i '/tint2/d' "$AUTOSTART"
            elif grep -q "nitrogen" "$AUTOSTART"; then
                sed -i '/nitrogen/ a sleep 1; manjaro-polybar-session &' "$AUTOSTART"
            else
                sed -i '1s/^/sleep 1; manjaro-polybar-session & \n/' "$AUTOSTART"
            fi
        fi
    fi
}

main() {
    while ! [[ $ANSWER ]]; do
        running_Bars
        find_Bars
        MSG="<big><b>Select bars to launch</b></big>\n\nRunning bars are check marked\n\nTo disable a bar:"
        MSG="$MSG <b>uncheck and click Ok</b>\n\nSession will be saved to: <b>$(basename "$SESSIONFILE")</b>\n"
        ANSWER=$($CHECKLIST --title="$TITLE" --text="$MSG" --separator=" " --column="Select" --column="Bars" $BARS)
        if [[ $? == 1 ]]; then
            ANSWER=NONE
        elif ! [[ $ANSWER ]]; then
            if [[ $(pidof polybar) ]]; then
                MSG="<big><b>No Bars Selected</b></big>\n\nWhat would you like to do?"
                if $QUESTION --title="$TITLE" --ok-label="Go Back" --cancel-label="Stop Bars" --text="$MSG" &>/dev/null; then
                    ANSWER="" ; continue
                else
                    pkill polybar ; echo -n "" > "$SESSIONFILE" ; ANSWER=NONE
                fi
            else
                ANSWER=NONE
            fi
        else
            create_session
        fi
        [[ -e $TEMPFILE ]] && rm -r "$TEMPFILE"
    done
}

main

exit 0
