#---
## @Synopsis libhistory
#---

## @param entry without the leading tab, bullet and space
##
## @Globals HISTORY_DATE GURU_NAME GURU_EMAIL
function add_history()
{
  local title="$HISTORY_DATE $GURU_NAME <$GURU_EMAIL>" files

  message "Generating HISTORY file ..."

  files=$(echo * | sed -e 's, \S*\.sign*\>,,g; s, \S*\.asc\>,,g' -e 's/ /, /g')
  echo > HISTORY
  sed -i "1 s%^.*$%$title\n\t* ${files/, HISTORY/}: $1\n%" HISTORY

  message "Done."

}

#---
## @Synopsis Adds a new HISTORY entry. Uses idempotent date title handling.
## @param entry without the leading tab, bullet and space
## @param file, defaults to HISTORY - used for adding Changelog entries
##
## @Globals HISTORY_DATE GURU_NAME GURU_EMAIL
#---
function add_history_entry()
{
  local title="$HISTORY_DATE $GURU_NAME <$GURU_EMAIL>" file
  local entry=$1
  local history_file=${2:-HISTORY}

  if [[ ! -e $history_file ]]; then
    error_msg "Spell is missing a $history_file file!"
    [[ -e DETAILS.orig ]] && mv DETAILS.orig ..
    add_history "added missing $history_file file"
    [[ -e ../DETAILS.orig ]] && mv ../DETAILS.orig .
  fi

  # make the entry wrap nicely (74 == 80-tab-star-space)
  if (( $(wc -c <<< "$entry") > 74 )); then
    entry=$(fmt -w 74 <<< "$entry" | sed -e '1! s|^|\t  |' -e '$! s|$|\\|')
  fi

  if grep -q "$title" $history_file;
  then
    # check if we already have an entry for the same file
    file="${1%%:*}"

    if [[ -z $(sed -n "1,/^\s*$/ s%\* $file:%&%p" $history_file) ]]
    then # we don't
      sed -i "/$title/ a\\\t* $entry" $history_file
    else
      # we do, but first check that the new addition is unique
      local first_entry_line=$(head -n1 <<< "$entry" | sed 's,\\$,,')
      if ! sed -n "1,/^\s*$/p" $history_file | grep -qF "* $first_entry_line" &&
         ! sed -n "1,/^\s*$/p" $history_file | grep -qF " ${first_entry_line#*:}"
      then
        sed -i "1,/^\s*$/ {
                 /\* $file:/ a\\\t ${entry#*:}
               }" $history_file
      fi
    fi
  else # first time today
    sed -i "1 s%^.*$%$title\n\t* $entry\n\n&%" $history_file
  fi
}

#---
## @Synopsis Adds new HISTORY entries.
## @param entry without the leading tab, bullet and space
##
## @Globals none
#---
function add_history_entries()
{
  local each
  message "Updating HISTORY ..."

  for each in "$@"; do
    add_history_entry "$each"
  done

  message "Done."
}

#---
## @Synopsis Adds arbitrary HISTORY entries.
##
## @Globals SPELL_UPDATED
#---
function add_user_history_entries()
{
  local entry modified

  message "Enter the text without the leading tab, spaces or stars. Example:"
  message "DETAILS: changed SOURCE3_URL[4]"
  message "When you're done adding new entries, just hit enter on the next prompt."
  while true; do
    query_string entry "What do you want to add? "
    [[ -z $entry ]] && break
    add_history_entry "${entry//&/\&}"
    modified=1
  done
  echo
  if [[ $modified == "1" ]]; then
    query "Do you want to review the HISTORY changes?" n &&
    quill_edit HISTORY
    echo
    SPELL_UPDATED=y
  fi
}

#---
## @Synopsis Adds a new Changelog entry
## @param entry without the leading tab, bullet and space
##
## WARNING: currently only modifies the live grimoire!
##
## @Globals SPELL_NAME
#---
function add_changelog_entry()
{
  local tmpf=cl.$$
  codex_get_spell_paths $(codex_find_spell_by_name $SPELL_NAME) &&
  [[ -w $GRIMOIRE/ChangeLog ]] && add_history_entry "$@" $GRIMOIRE/ChangeLog
  unset_spell_paths

  echo > $tmpf
  add_history_entry "$@" $tmpf
  message "The Changelog entry was added directly to the grimoire (if writable)."
  message "Either copy it over or add this to your git grimoire (if applicable)"
  message "and fix the initial tab:"
  cat $tmpf
  rm $tmpf
}

#---
##
## This software is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This software is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this software; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##
#---

