#!/bin/sh
fzf=${YATT_FZF:="fzf"}
usage_prefix=${yatt_script_usage_prefix:="Get id of yatt's record"}
script_executable=${yatt_script_executable:=$0}

usage () {
  echo "\
$usage_prefix with preferred fuzzy finder.
By default, 'fzf' is used, but you can change it by setting \
YATT_FZF environment variable value.

USAGE:
    $script_executable [SUBCOMMAND]

SUBCOMMANDS:
    task        Handles tasks
    group       Handles groups
    interval    Handles intervals
    help        Prints this message
"
  exit 0
}

error () {
  echo "Error: invalid argument. Type \
'$script_executable help' to get list of possible commands."
  exit 1
}

[ -z "$1" ] && usage
[ "$1" != "help" ] || usage
echo $1 | grep -E '^task|interval|group$' > /dev/null || error

command=$1s
command=${command/groups/"tasks -g"}

line=$(yatt -c list $command | $fzf)
line="${line%%]*}"

echo "${line#[}"
