#!/bin/sh

swaymsg -t subscribe -m '[ "window" ]' | while read -r event ; do
    if [ "$(jq -r '.change' <<< $event)" = 'focus' ] ; then 
        current_workspace="$(swaymsg -t get_workspaces | jq -r '.[] | select(.focused)')"
        workspace_num="$(jq -r '.num' <<< $current_workspace)"
        workspace_name="$(jq -r '.name' <<< $current_workspace)"

        app_name="$(jq -r '.container.app_id' <<< $event)"
        if [ $app_name = 'null' ] ; then
            app_name="$(jq -r '.container.window_properties.instance' <<< $event)"
        fi

        if [ "$app_name" = "kitty" ] ; then
            app_name=""
        elif [[ "$app_name" == *"chrom"* ]] || [[ "$app_name" == *"Chrom"* ]]; then
            case "$(jq -r '.container.name' <<< $event)" in
                *YouTube*)
                    app_name=""
        	    ;;
        	*)
        	    app_name=""
        	    ;;
            esac
        elif [ "$app_name" = "discord" ] ; then
            app_name=""
        elif [ "$app_name" = "spotify" ] ; then
            app_name=""
        elif [ "$app_name" = "GEMOC Studio" ] ; then
            app_name=""
        fi

        swaymsg rename workspace "$workspace_name" to "$workspace_num:$app_name"
    fi
done
