#!/usr/bin/env bash
###############################################################################
# Baskfile
#
# Run With Bask: https://github.com/xwmx/bask
###############################################################################

export LANG=en_us.UTF-8
export LC_ALL=en_us.UTF-8

###############################################################################
# Helpers
###############################################################################

# _sed_i()
#
# `sed -i` takes an extension on macOS, but that extension can cause errors in
# GNU `sed`.
#
# https://stackoverflow.com/q/43171648
# https://stackoverflow.com/a/16746032
_sed_i() {
  if sed --help >/dev/null 2>&1
  then # GNU
    sed -i "${@}"
  else # BSD
    sed -i '' "${@}"
  fi
}

###############################################################################
# Tasks
###############################################################################

# g:web ################################################################# g:web

desc "g:web" <<HEREDOC
Usage:
  bask g:web

Description:
  Regenerate index.markdown from README.md combined with existing front matter.
HEREDOC
g:web() {
  local _front_matter_lines=()
  local _in_front_matter=0
  local _target_file="./docs/index.markdown"

  [[ ! -f "${_target_file:?}" ]] &&
    _exit_1 printf "./docs/index.markdown not found.\\n"

  while IFS= read -r __line || [[ -n "${__line}" ]]
  do
    if [[ "${__line}" =~ ^---$ ]]
    then
      if ((_in_front_matter))
      then
        _in_front_matter=0
      else
        _in_front_matter=1
      fi
    elif ((_in_front_matter))
    then
      _front_matter_lines+=("${__line}")
    elif [[ -n "${_front_matter_lines[*]:-}" ]]
    then
      break
    fi
  done < "${_target_file:?}"

  if [[ -z "${_front_matter_lines[*]:?}" ]]
  then
    _exit_1 printf "Front matter not found.\\n"
  fi

  {
    cat ./README.md
  } | {
    # remove last 6 lines
    sed -n -e ':a' -e "1,6"'!{P;N;D;};N;ba'
  } | {
    cat << HEREDOC > "${_target_file:?}"
---
${_front_matter_lines[*]}
---

$(cat)
HEREDOC
  }

  printf "Rebuilt: %s\\n" "${_target_file:?}"
}

# npm:publish ##################################################### npm:publish

desc "npm:publish" <<HEREDOC
Usage:
  bask npm:publish

Description"
  npm mangles HTML h1 elements, so convert to a normal markdown h1, publish
  to npm, then revert the heading back to the HTML h1.
HEREDOC
npm:publish() {
  local _h1="<h1 align=\"center\" id=\"nb\"><code>nb<\/code><\/h1>"

  printf "Publishing to npm.\\n"
  while true
  do
    read -r -p "Proceed? [y/N] " __yn
    case ${__yn} in
      [Yy]*)
        break
        ;;
      *)
        printf "Exiting...\\n"
        exit 0
        ;;
    esac
  done

  _sed_i -e "s/^${_h1}$/# \`nb\`/g" "README.md"

  npm publish

  _sed_i -e "s/^# \`nb\`$/${_h1}/g" "./README.md"
}

# v ######################################################################### v

desc "v" <<HEREDOC
Usage:
  bask v
  bask v down
  bask v halt
  bask v ssh
  bask v up

Description:
  Shortcuts for Vagrant that can be called from any subdirectory.
HEREDOC
v() {
  local _baskfile_path="${BASH_SOURCE[0]}"
  local _root_dir="${_baskfile_path%/Baskfile}"
  local _vagrant_dir="${_root_dir}/etc"

  case "${1:-}" in
    d*) # destroy / down
      (cd "${_vagrant_dir}" && vagrant destroy)
      ;;
    h*) # halt
      (cd "${_vagrant_dir}" && vagrant halt)
      ;;
    s*) # ssh
      (cd "${_vagrant_dir}" && vagrant ssh)
      ;;
    u*) # up
      (cd "${_vagrant_dir}" && vagrant up)
      ;;
    *)
      (
        cd "${_vagrant_dir}" && {
          vagrant ssh        || {
            vagrant up && vagrant ssh
          }
        }
      )
      ;;
  esac
}
