#!/bin/bash

swaymsg fullscreen disable

if pgrep wofi;then
  killall wofi
fi

sel_app=$(wofi --show drun -O alphabetical -a --insensitive --allow-images)
[[ -z ${sel_app} ]] && exit 1
app_name=$(basename "${sel_app[0]}")

ws_nums=($(swaymsg -t get_workspaces | jq -r '.[].num'))

for n in {1..10};do
  if [[ ! $( printf "%s\n" ${ws_nums[@]} | fgrep -w ${n} ) ]];then
    swaymsg workspace "${n}"
    notify-send "Launching ${app_name}"
    ${sel_app[@]}
    exit
  fi
done

notify-send "All workspaces are currently in use"
