#! /usr/bin/env bash
readonly NOTES_DIR="$HOME/Notes/MD/notes" ## TODO Global Variables are bad
readonly RECOLL_CONFIG_DIR="$HOME/.cadmus"
# Author: Ryan Greenup <ryan.greenup@protonmail.com>
# abort on nonzero exitstatus
set -o errexit
# abort on unbound variable
set -o nounset
# don't hide errors within pipes
set -o pipefail


# * Main Functions

main() {

    setvars
    [[ -z "${1:-}" ]] && mainHelp && exit 0
    arguments  "${@:-}"
}

function setvars() {

    IFS=$'\t\n'   # Split on newlines and tabs (but not on spaces)
    readonly script_name=$(basename "${0}")
    readonly script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
    readonly TERMINAL="kitty"
    readonly TERMINAL_EXEC='kitty -- '
}


# * Sub-functions
# ** Help

function mainHelp() {

    echo
    echo -e "    \e[3m\e[1m    Cadmus\e[0m; Helpful Shell Scripts for Markdown Notes"
    echo -e "    \e[1;31m -------------------------\e[0m "
    echo

    echo -e " \e[1;91m    \e[1m Command \e[0m\e[0m \e[1;34m┊┊┊ \e[0m Description "
    echo -e " ..............\e[1;34m┊┊┊\e[0m........................................... "
    echo -e " 🔍 \e[1;93m find \e[0m \e[1;34m    ┊┊┊ 🎆 \e[0m Find Notes based on FileName"
    echo -e " 🔎 \e[1;32m search \e[0m \e[1;34m  ┊┊┊ 📁 \e[0m Search through Notes using Recoll"
    echo -e " 🏷  \e[1;33m tags \e[0m \e[1;34m    ┊┊┊ \e[0m Use TMSU to work with tags"
    echo -e " 🔧 \e[1;34m tools \e[0m \e[1;34m   ┊┊┊ \e[0m Tools for Editing"
    echo -e " 📝 \e[1;35m export \e[0m \e[1;34m  ┊┊┊ \e[0m Export Notes to Different Formats "
    echo -e " ⎋  \e[1;36m convert \e[0m \e[1;34m ┊┊┊ \e[0m Convert Clipboard Contents to Different Formats "
    echo -e " 🧰 \e[1;37m misc \e[0m \e[1;34m    ┊┊┊ \e[0m Miscelanneous Tools nice to have on hand "
    echo -e " 🌏\e[1;92m publish\e[0m \e[1;34m   ┊┊┊ \e[0m Publish with \e[1;34m \e[4m\e[3mMkDocs\e[0m\e[0m🐍"
    echo -e " 🕮 \e[1;93m preview \e[0m \e[1;34m  ┊┊┊ \e[0m Preview with \e[1;34m \e[4m\e[3mMarkServ\e[0m\e[0m "
    echo -e " 🕮 \e[1;94m help \e[0m \e[1;34m     ┊┊┊ \e[0m Open help for correspoding functions "
    echo
    echo -e "    \e[3m\e[1m• Legend\e[0m "
    echo
    echo -e "    🎆: Executes a command, a Suffixed -h argument will print help"
    echo -e "    📁: Prints another Menu"
    echo
    }

# ** Main Arguments - Level 0
# *** Switch
arguments () {

    while test $# -gt 0
    do
        case "$1" in
            --help) mainHelp && exit 0
                ;;
            -h) mainHelp && exit 0
                ;;
            find) shift; NoteFind "${NOTES_DIR}" "${@:-}" ## Don't steal function name
                ;;
            search) shift; NoteSearch "${NOTES_DIR}" ${@:-}
                ;;
            tags) echo "begin tags"
                ;;
            tools) echo "begin tools"
                ;;
            export) echo "begin export"
                ;;
            convert) echo "begin convert"
                ;;
            misc) echo "begin misc"
                ;;
            publish) echo "begin publish"
                ;;
            preview) echo "begin preview"
                ;;
            help) subHelp && exit 0
                ;;
            --*) >&2 echo "bad option $1"
                ;;
            *) >&2 echo -e "argument \e[1;35m${1}\e[0m has no definition."
                ;;
        esac
        shift
    done
}

# *** Find
function NoteFind() {
    COMMAND="NoteFind.sh"
    echo "Running "${COMMAND}" "${1}""

    FILES="$("${COMMAND}" "${@:-}")"

    ## Only try and open something that is non-empty
    if [ "$FILES" != "" ]; then
        ## xdg-open doesn't accept multiple arguments, so create commands and
        ## pipe them back into bash This opens all files in the same tab with
        ## VSCode and Atom The individual selections can also be passed off with
        ## the keybindings to emacsclient.
            # Using XDG-OPEN
#        echo "${FILES}" | sed s/^/xdg-open\ \"/ | sed s/$/\"/ | bash
        echo "${FILES}" | sed -e s+^+\"+ -e s+$+\"+ | xargs -n 1 xdg-open

#            # Getting the .desktop file and cutting off the Extension
#        dex "$(xdg-mime query default text/markdown)" $FILES
        # dex "$(xdg-mime query default text/markdown)" $FILES
            ## This might break if the .desktop file doesn't have the same
            ## name as the binary.
                ## This appears to be a standard though
                    ## https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#file-naming
    fi
}

# ***Search
function NoteSearch() {
    COMMAND="NoteRecollSearch.sh"
    echo "Running "${COMMAND}" "${1}""

    FILES="$("${COMMAND}" "${@:-}")"

    ## Only try and open something that is non-empty
    if [ "$FILES" != "" ]; then
        ## xdg-open doesn't accept multiple arguments, so create commands and
        ## pipe them back into bash This opens all files in the same tab with
        ## VSCode and Atom The individual selections can also be passed off with
        ## the keybindings to emacsclient.
            # Using XDG-OPEN
#        echo "${FILES}" | sed s/^/xdg-open\ \"/ | sed s/$/\"/ | bash
        echo "${FILES}" | sed -e s+^+\"+ -e s+$+\"+ | xargs -n 1 xdg-open 

#            # Getting the .desktop file and cutting off the Extension
#        dex "$(xdg-mime query default text/markdown)" $FILES
        # dex "$(xdg-mime query default text/markdown)" $FILES
            ## This might break if the .desktop file doesn't have the same
            ## name as the binary.
                ## This appears to be a standard though
                    ## https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#file-naming
    fi
}

# *** All the Help
subHelp () {
    echo
    echo
    echo -e "\e[1;35m -------------------------\e[0m "
    echo -e "  \e[1;94m\e[3m\e[1m Cadmus Find \e[0m"
    echo -e "\e[1;35m -------------------------\e[0m "
    NoteFind.sh -h
}

# * Call the Main Function
main "${@}"

