#!/usr/bin/env dash

# shellcheck source=/dev/null
SHELL_LIBRARY_VERSION="1.4.2" . %SHELL_LIBRARY_PATH

# these are used in version()
AUTHORS="Soispha"
YEARS="2023"

NAME="neorg"

help() {
cat << EOF
This is a quick wrapper to make starting neorg easier.

Usage:
    $NAME [--help|--version] [<neorg workspace>]

Options:
    --help      | -h
                            Display this help and exit.
    --version   | -v
                            Display version and copyright information and exit.
    --task  ID  | -t ID
                            Open the neorg project associated with the current context and
                            the uuid of the task with id ID.
                            If no context is set, drops you to the selection prompt
    <neorg workspace>
                            The neorg workspace to open at startup, an empty value drops
                            you at a prompt to enter the workspace yourself.
                            If unset (and no other options apply), opens the current task
                            context.
EOF
}
get_current_context() {
    current_context="$(task _get rc.context)";
    printf "%s\n" "$current_context";
}
get_current_context_path() {
    current_context="$1";
    context_path="$(task _get rc.context."$current_context".rc.neorg_path 2>/dev/null)";
    if ! [ "$context_path" ]; then
        context_path="$(grep "context.$current_context.rc.neorg_path" "%HOME_TASKRC" | awk 'BEGIN {FS="="} {print $2}')";
        [ "$context_path" ] || die "All contexts should have a 'neorg_path' set!"
    fi
    printf "%s\n" "$context_path"
}
get_neorg_project_dir() {
    # Perform shell expansion of Tilde
    neorg_project_dir="$(sed "s|~|$HOME|" "$(ptmp "%DEFAULT_NEORG_PROJECT_DIR")")";
    printf "%s\n" "$neorg_project_dir"
}

open_neorg_workspace() {
    nvim -c "NeorgStart" -s "$(ptmp ":Neorg workspace $1\n")"
}
open_neorg_workspace_prompt() {
    nvim -c "NeorgStart" -s "$(ptmp ":Neorg workspace ")"
}
open_current_task_context() {
    current_context="$(get_current_context)"
    if [ "$current_context" ]; then
        context_path="$(get_current_context_path "$current_context")";

        extended_neorg_project_dir="$(get_neorg_project_dir)";
        cd "$extended_neorg_project_dir" || die "(BUG?): Can not access the project dir: $extended_neorg_project_dir";

        nvim "$extended_neorg_project_dir/$context_path";

        git add .;
        git commit --message="chore($(dirname "$context_path")): Update" --no-gpg-sign
    else
        warn "No context active";
        open_neorg_workspace_prompt;
    fi
}
open_current_task_context_at_task_id() {
    task_id="$1";
    current_context="$(get_current_context)"
    if [ "$current_context" ]; then
        context_path="$(get_current_context_path "$current_context")";
        extended_neorg_project_dir="$(get_neorg_project_dir)";
        task_uuid="$(task "$task_id" uuids)"

        cd "$extended_neorg_project_dir" || die "(BUG?): Can not access the project dir: $extended_neorg_project_dir";

        if ! grep -q "% $task_uuid" "$extended_neorg_project_dir/$context_path"; then
            echo "* TITLE (% $task_uuid)" >> "$extended_neorg_project_dir/$context_path"
        fi

        nvim "$extended_neorg_project_dir/$context_path" -c "/% $task_uuid";

        git add .;
        git commit --message="chore($(dirname "$context_path")): Update" --no-gpg-sign
    else
        warn "No context active";
        open_neorg_workspace_prompt;
    fi
}

for arg in "$@"; do
    case "$arg" in
        "--help" | "-h")
            help;
            exit 0;
            ;;
        "--version" | "-v")
            version;
            exit 0;
            ;;
    esac
done

while [ "$#" -ne 0 ]; do
    case "$1" in
        "--task" | "-t")
            shift 1;
            task_id="$1";
            [ "$task_id" ] || die "You must have a task_id set! See --help for more"
            open_current_task_context_at_task_id "$task_id";
            exit 0;
            ;;
        *)
            open_neorg_workspace "$1";
            exit 0;
            ;;
    esac
done

open_current_task_context;

# vim: ft=sh