#!/bin/bash

# High-level bspwm controls

usage ()
{
    echo "Usage :"
    echo "- discard [--follow]"
    echo "- goto prev|next|last|n [--carry]"
    echo "- peek left|right"
    echo "- popup classname [bin]"
    echo "- unhide"
    exit 0
}

# Functions

clear_desktop ()
{
    last=`cat /tmp/lastdesk`
    namelast=$last
    namecurrent=`bspc query -d focused -D --names`
    wincurrent=`bspc query -d focused -N`

    if [[ "$last" -gt "1" ]]; then
        del_desks=0

        # Delete any empty desk and re-numerate
        for desk in `seq 1 $last`; do
            if [[ -z `bspc query -N -d "$desk"` ]]; then
                let "del_desks += 1"
                bspc desktop "$desk" -r
            else
                let "newname = desk - del_desks"
                bspc desktop "$desk" -n "$newname"
            fi
        done

        let "last = last - del_desks"

        # Always keep desktop 1 existing, even if empty
        # and allow for an additional empty desktop
        if [[ "$last" -eq "0" ]]; then
            bspc monitor -a 1
            bspc desktop -f 1
            last=1
        elif [[ "$namecurrent" -eq "$namelast" && -z "$wincurrent" ]]; then
            let "last=$last + 1"
            bspc monitor -a "$last"
            bspc desktop -f "$last"
        fi
    fi

    echo "$last" > /tmp/lastdesk
}

popup ()
{
    if [ $# -eq 0 ];then
        usage
    fi

    name=$1
    shift

    # If the program is not yet running, launch it and monitor the node creation
    if [ -z `xdotool search --classname $name` ];then
        bspc subscribe node_add -c 1 | cut -d ' ' -f 5 > /tmp/${name}_win &
        kitty --name $name $*
        sleep .5
    else
        win=`cat /tmp/${name}_win`
        if [ -z `bspc query -N -n ${win}.hidden` ];then
            bspc node ${win} -g hidden
        else
            bspc node ${win} -g hidden -f
        fi
    fi
}

peek ()
{
    case $1 in
        left|right)
            if [ `bspc config -d focused $1_padding` -gt 0 ]; then
                start=500
                step=-20
                end=0
            else
                start=0
                step=20
                end=500
            fi

            for pad in `seq $start $step $end`; do
                bspc config -d focused $1_padding $pad
            done
            ;;
        *) usage;;
    esac
}

discard ()
{
    # Save current desktop in case we --follow
    # echo `bspc query -D -d focused --names` > /tmp/desk

    # Create a new desktop at the end
    last=`cat /tmp/lastdesk`
    let "last += 1"
    echo $last > /tmp/lastdesk
    bspc monitor -a $last

    # Move the node and optionally --follow it
    bspc node -d "$last" $1

    clear_desktop
}

goto ()
{
    current=`bspc query -D -d focused --names`

    case $1 in
        next) # Create a new desktop if needed
            last=`cat /tmp/lastdesk`
            if [ `bspc query -D -d focused --names` -eq "$last" ]; then
                let "last += 1"
                echo $last > /tmp/lastdesk
                bspc monitor -a $last
            fi
            togo=$1
            ;;
        last) togo=`cat /tmp/lastdesk` ;;
        prev| | |1|2|3|4|5|6|7) togo=$1 ;;
        *) usage ;;
    esac

    # Go back to previous desktop if destination is the current desktop
    if [[ "$togo" == "$current" ]]; then
        # target=`cat /tmp/desk`
        target="last"
    else
        target="$togo"
        # echo $current > /tmp/desk
    fi

    # Carry a node or just focus desktop
    case $2 in
        --carry) bspc node -d "$target" --follow ;;
        *) bspc desktop -f "$target" ;;
    esac

    clear_desktop
}

unhide ()
{
    bspc node any.hidden -g hidden
}

# UI

case $1 in
    discard) discard $2 ;;
    goto) goto $2 $3 ;;
    peek) peek $2 ;;
    popup) shift; popup $* ;;
    unhide) unhide ;;
    clear) clear_desktop ;;
    *) usage ;;
esac
