#!/bin/bash

if [ -t 1 ]; then
    pick() { fzf --height 10; }
    run() { /usr/bin/abduco -A "$@" "$@"; clear; }
else
    pick() { dmenu; }
    run() { exec st -e /usr/bin/abduco -A "$@" "$@"; }
fi

result="$(case "$1" in
    list)      find ~/.abduco -not -executable -type s -exec basename \{\} \; | cut -d@ -f1 | pick ;;
    run|watch) dmenu_path | pick ;;
    *)         echo "other" ;;
esac)"

[ -z "$result" ] && exit 1

if test "$result" = "other"; then
    exec /usr/bin/abduco "$@"
fi

case "$1" in
    watch)      result="watch $result" ;&
    list|run)   run $result ;;
esac