#!/usr/bin/env bash
###############################################################################
#     __                __                        __
#    / /_  ____  ____  / /______ ___  ____ ______/ /__
#   / __ \/ __ \/ __ \/ //_/ __ `__ \/ __ `/ ___/ //_/
#  / /_/ / /_/ / /_/ / ,< / / / / / / /_/ / /  / ,<
# /_.___/\____/\____/_/|_/_/ /_/ /_/\__,_/_/  /_/|_|
#
# bookmark -- Command line bookmarking with tagging, encryption,
# full-text page content search with regular expression support,
# GUI and terminal browser support, and data stored in plain text
# Markdown files with Git-backed versioning and syncing.
#
# Part of:
# __          _
# \ \   _ __ | |__
#  \ \ | '_ \| '_ \
#  / / | | | | |_) |
# /_/  |_| |_|_.__/
#
# [nb] Command line and local web note-taking, bookmarking, and archiving with
# plain text data storage, encryption, filtering and search, pinning, #tagging,
# Git-backed versioning and syncing, Pandoc-backed conversion, global and local
# notebooks, customizable color themes, [[wiki-style linking]], plugins, and
# more in a single portable, user-friendly script.
#
# https://github.com/xwmx/nb
#
# Based on Bash Boilerplate: https://github.com/xwmx/bash-boilerplate
#
# Copyright (c) 2015-present William Melody • hi@williammelody.com
# AGPLv3 • See LICENSE for details.
###############################################################################

export _NB_PATH="${_NB_PATH:-}"

if [[ -z "${_NB_PATH:-}" ]]
then
  _NB_PATH="$(command -v nb)"
fi

if [[ -z "${_NB_PATH:-}" ]]
then
  cat <<HEREDOC
\`bookmark\` is part of \`nb\`. To install \`nb\`, see:
  https://github.com/xwmx/nb
HEREDOC
fi

_main() {
  if [[ -z "${1:-}" ]]
  then
    local _count=0
    while IFS= read -r __line
    do
      _count=$((_count+1))

      if [[ "${_count}" == 1 ]] ||
         [[ "${_count}" == 6 ]] ||
         [[ "${_count}" == 8 ]]
      then
        printf "%s\\n" "${__line}"                      \
          | sed -e 's/nb help bookmark/bookmark help/'  \
          | sed -e 's/nb/bookmark/'
      elif [[ "${_count}" == 2 ]]
      then
        printf "%s\\n" "${__line}"                      \
          | sed -e 's/-------------------------/----------------------------/'
      else
        printf "%s\\n" "${__line}"
      fi
    done < <("${_NB_PATH:-}" bookmark)
  elif [[   "${1:-}" =~ ^-h$|^--help$|^h$|^help$  ]] || {
         [[ "${1:-}" =~ ^q$|^search$              ]] &&
         [[ -z "${2:-}"                           ]]

       }
  then
    {
      cat <<HEREDOC
    __                __                        __
   / /_  ____  ____  / /______ ___  ____ ______/ /__
  / __ \\/ __ \\/ __ \\/ //_/ __ \`__ \\/ __ \`/ ___/ //_/
 / /_/ / /_/ / /_/ / ,< / / / / / / /_/ / /  / ,<
/_.___/\\____/\\____/_/|_/_/ /_/ /_/\\__,_/_/  /_/|_|

bookmark -- Command line bookmarking with tagging, encryption,
full-text page content search with regular expression support,
GUI and terminal browser support, and data stored in plain text
Markdown files with Git-backed versioning and syncing.

HEREDOC
      "${_NB_PATH:-}" help bookmark                 \
        | sed -e "/  nb example/d"                  \
        | sed -e "s/  nb https/  bookmark https/g"  \
        | sed -e "s/  nb bookmark/  bookmark/g"     \
        | sed -e "/  nb b$/d"                       \
        | sed -e "/  nb bk$/d"                      \
        | sed -e "/  nb bm$/d"                      \
        | sed -e "s/Shortcut Alias: \`b\`//"        \
        | sed -e "s/Shortcut Alias: \`bk\`//"       \
        | sed -e "s/Shortcut Aliases.*//"           \
        | sed -e '$ d'

      cat <<HEREDOC
------------------------------------------
Part of \`nb\` (https://github.com/xwmx/nb).
For more information, see: \`nb help\`.
HEREDOC
    } | if [[ -n "${PAGER:-}" ]] && [[ ! "${PAGER:-}" =~ less ]]
        then
          "${PAGER}"
        elif hash "less" 2>/dev/null
        then
          less -R           \
            --CLEAR-SCREEN  \
            --prompt="> scroll for more, h for help, or q to quit"
        else
          cat
        fi
  else
    "${_NB_PATH}" bookmark "${@:-}"
  fi
} && _main "${@:-}"
