#---
## @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/}, 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
##
## @Globals HISTORY_DATE GURU_NAME GURU_EMAIL
#---
function add_history_entry()
{
  local title="$HISTORY_DATE $GURU_NAME <$GURU_EMAIL>" file

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

  if grep -q "$title" HISTORY;
  then
    # check if we already have an entry for the same file
    file="${1%%:*}"
    if [[ -z $(sed -n "1,/^\s*$/ s%\* $file:%&%p" HISTORY) ]]
    then # we don't
      sed -i "/$title/ a\\\t* $1" HISTORY
    else
      # we do, but first check that the new addition is unique
      if ! sed -n "1,/^\s*$/p" HISTORY | grep -qF "* $1"
      then
        sed -i "1,/^\s*$/ {
                /\* $file:/ a\\\t ${1#*:}
               }" HISTORY
      fi
    fi
  else # first time today
    sed -i "1 s%^.*$%$title\n\t* $1\n\n&%" HISTORY
  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
}

#---
##
## 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
##
#---

