#!/usr/bin/env bash

# Exit on error. Append "|| true" if you expect an error.
set -o errexit
# Exit on error inside any functions or subshells.
set -o errtrace
# Do not allow use of undefined vars. Use ${VAR:-} to use an undefined VAR
set -o nounset
# Catch the error in case mysqldump fails (but gzip succeeds) in `mysqldump |gzip`
set -o pipefail

function command_exists() {
  type "$1" &> /dev/null
}

function list_groups() {
  groups="$("${TOOL}" list-groups)"
  if ! echo "${groups}" | grep -qE '^$'; then
    echo ''
  fi
  echo "${groups}"
}

# The default group name is the empty string, so in order to make it clearer we
# replace it with this string when presenting it to the user.
DEFAULT_GROUP_ITEM='<default>'
# NOTE: This used to have a span tag for adding transparency (see commented line
# below), but I removed it because Rofi has a bug where pango markup
# is being matched in dmenu mode, which is confusing. See also:
# https://github.com/DaveDavenport/rofi/issues/597
# DEFAULT_GROUP_ITEM='<span alpha="50%">(Default group)</span>'

DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
TOOL="${DIR}/i3-workspace-groups"
command_exists "${TOOL}" || TOOL="i3-workspace-groups"
if selected_group="$(list_groups |
  sed -r 's|^$|'"${DEFAULT_GROUP_ITEM}"'|' |
  rofi -dmenu -i -p 'Switch to workspace group' -lines 10 -width 30
)"; then
  if [[ "${selected_group}" == "${DEFAULT_GROUP_ITEM}" ]]; then
    selected_group=''
  fi
  "${TOOL}" "$@" switch-active-group "${selected_group}"
else
  exit $?
fi
