#---
## @Synopsis libdetails
## for all the fields that could be filled in, check if they were filled in
## and if so, give the user an opportunity to change the value
#---

##
## @Globals SPELL_NAME
function query_spell_name(){
  #downcase before query
  SPELL_NAME=$(tr '[[:upper:]]' '[[:lower:]]' <<< "$SPELL_NAME")
  if ! [[ $SPELL_NAME ]] ||
    ! query "Is $SPELL_NAME the proper spell name?" y; then
    query_msg "Please enter the spell name:"
    read "SPELL_NAME"
#downcase after query
    SPELL_NAME=$(tr '[[:upper:]]' '[[:lower:]]' <<< "$SPELL_NAME")
  fi
}

##
## @Globals SPELL_SRC_URL
function query_spell_source_url(){
  # misc sf fix
  # try to handle as many sf url formats as possible
  # I tested this with various URLs and it seems to work quite well
  # if any oter are found should be included
  # possibly split the code into a separate function???
  SPELL_SRC_URL=$(sed -e "s,http://prdownloads.sourceforge.net,$SOURCEFORGE_URL," \
                      -e "s,http://downloads.sourceforge.net/\(.*\)?.*,$SOURCEFORGE_URL/\1," \
                      -e "s,http://downloads.sourceforge.net/\(.*\),$SOURCEFORGE_URL/\1," \
                      -e "s,http://prdownload.berlios.de/\(.*\),http://download.berlios.de/\1," \
                      -e "s,http://sourceforge.net/project/showfiles.php.*,," \
                      -e "s,^\(.*\)/\(.*\)?.*,\1/\2," \
                      <<< $SPELL_SRC_URL)
if ! [[ $SPELL_SRC_URL ]] ||
   ! query "Is $SPELL_SRC_URL the proper source url?" y; then
  query_msg "Please enter the url of the source:"
  read "SPELL_SRC_URL"
  SPELL_SRC_URL=$(sed -e "s,http://prdownloads.sourceforge.net,$SOURCEFORGE_URL," \
                      -e "s,http://downloads.sourceforge.net/\(.*\)?.*,$SOURCEFORGE_URL/\1," \
                      -e "s,http://downloads.sourceforge.net/\(.*\),$SOURCEFORGE_URL/\1," \
                      -e "s,http://prdownload.berlios.de/\(.*\),http://download.berlios.de/\1," \
                      -e "s,http://sourceforge.net/project/showfiles.php.*,," \
                      -e "s,^\(.*\)/\(.*\)?.*,\1/\2," \
                      <<< $SPELL_SRC_URL)
fi
}

## used in update mode only, elsewhere the version is parsed from the filename
##
## @Globals SPELL_VERSION
function query_spell_version(){
  if ! [[ $SPELL_VERSION ]] ||
    ! query "Is $SPELL_VERSION the proper spell version?" y; then
    query_msg "Please enter the new version:"
    read SPELL_VERSION
  fi
}

##
## @Globals SPELL_LICENSE
function query_spell_license(){
if ! [[ $SPELL_LICENSE ]] ||
   ! query "Is $SPELL_LICENSE the proper license?" y; then
  query_msg "Please enter the license of the spell:"
  read "SPELL_LICENSE"
fi
}

##
## @Globals SPELL_URL
function query_spell_url(){
if ! [[ $SPELL_URL ]] ||
   ! query "Is $SPELL_URL the proper website?" y; then
  query_msg "Please enter a website for the spell:"
  read "SPELL_URL"
fi
}

##
## @Globals SPELL_SHORT_DESCRIPTION
function query_spell_short_description(){
if ! [[ $SPELL_SHORT_DESCRIPTION ]] ||
   ! ( message "${QUERY_COLOR}Is${DEFAULT_COLOR}\n$SPELL_SHORT_DESCRIPTION"
       query "the proper short description?" y ; ) ; then
  query_msg "Please enter a short description of the spell:"
  read "SPELL_SHORT_DESCRIPTION"
fi
}

##
## @Globals EDITOR SPELL_NAME SPELL_DESC_NAME QUILL_TMP_DIR
function query_spell_description(){
local description=${QUILL_TMP_DIR}/${SPELL_DESC_NAME:-${SPELL_NAME}}
if ! test -f $description ||
   ! ( query_msg "Is"
       cat $description
       query "the proper long description?" y ; ) ; then
  test -s $description ||
  echo "Please enter a description of the spell here" > $description

  $EDITOR $description
fi
}

#--
## @Synopsis Assign to SPELL_SRC_FILE the filename and to VERSION the
## @Synopsis version number parsed from source url.
##
## @Globals SPELL_SRC_FILE SPELL_SANITIZED_FILE_NAME SPELL_VERSION
#---
function parse_spell_source_file_info(){
  SPELL_SRC_FILE=$(expr "$SPELL_SRC_URL" : '.*/\(.*\)')
  SPELL_SANITIZED_FILE_NAME=$(expr "$SPELL_SRC_FILE" : '\(.*\)[-|_][0-9]')
  SPELL_VERSION=$(expr "$SPELL_SRC_FILE" : "${SPELL_SANITIZED_FILE_NAME}[-|_]\(.*\)\.[t|z|r]")
}

#---
## @Synopsis Print on screen info gathered from source url.
##
## @Globals SPELL_SANITIZED_FILE_NAME SPELL_VERSION SPELL_SRC_URL
#---
function show_spell_source_file_info(){
  message "Sanitized filename: ${SPELL_SANITIZED_FILE_NAME}"
  message "Version: ${SPELL_VERSION}"
  message "URL: ${SPELL_SRC_URL}"
}

#---
## @Synopsis Substitute any number of expanded variables in given variable
## @Synopsis with corresponding variable names
## @param target variable (must not be target or target_value)
## @param the string to search for (variable contents)
## @param the replacement for said string (variable name)
## @param the string to search for (variable contents)
## @param the replacement for said string (variable name)
## @param ...
##
## @Globals none
#---
function substitute_with_variables() {
if [[ -n $1 ]]; then
  local target=$1 target_value
  eval target_value="\$$1"
  shift 1
  while [[ -n $2 ]]; do
    target_value="${target_value//$1/\$$2}"
    shift 2
  done
  eval "$target=\"\$target_value\""
fi
}

#---
## @Synopsis Substitute download mirror urls with variable names in
## @Synopsis given variable. Store resulting string in
## @Synopsis SUBSTITUTIONS. Urls and variables are parsed from contents
## @Synopsis of /etc/sorcery/mirrors/. Example:
## @Synopsis substitute_with_mirror_variables SPELL_SRC_URL
##
## @Globals none
#---
function substitute_with_mirror_variables(){
if [[ -n $1 ]]; then
  local target=$1 target_value mirror_list mirror_variable mirror_entry
  eval target_value="\$$1"
  for mirror_list in /etc/sorcery/mirrors/*; do
    mirror_variable="$(basename $mirror_list)_URL"
    while read mirror_entry; do
      if [[ $mirror_entry != "Custom" ]]; then
        target_value=${target_value/$mirror_entry/\$$mirror_variable}
      fi
    done < <(sed 's,^.*\s\s*,,' $mirror_list)
  done
  eval "$target=\"\$target_value\""
fi
}

#---
## @Synopsis Assign to SPELL_SRC_DIR the source dir inside a tarball,
## @return 0 if found supported fromat and suceeded
## @return 1 if failed
## @return 2 if unsupported format
##
## @Globals QUILL_TMP_DIR SPELL_SRC_FILE SPELL_NAME SPELL_VERSION SPELL_SRC_DIR
#---
function hunt_src_dir(){
  local compressor
  if [[ ! -e ${QUILL_TMP_DIR}/${SPELL_SRC_FILE} ]] ; then
    SPELL_SRC_DIR=${SPELL_NAME}-${SPELL_VERSION}
    error_msg "Error: no tarball, setting default SPELL-VERSION construct"
    return 1
  else
    message "Checking for the source directory..."
    compressor=$(guess_compressor ${QUILL_TMP_DIR}/${SPELL_SRC_FILE})
    case "$compressor" in
      bzip2|gzip|compress*|tar) SPELL_SRC_DIR=$(tar tf "${QUILL_TMP_DIR}/${SPELL_SRC_FILE}" | sed -n "1 s=/.*$==p") ||
                                { error_msg "Error: getting path from tarball" && return 1; } ;;
                           Zip) SPELL_SRC_DIR=$(zipinfo -1 "${QUILL_TMP_DIR}/${SPELL_SRC_FILE}" | sed -n "1 s=/.*$==p") ||
                                { error_msg "Error: getting path from zipfile" && return 1; }
                                if [[ $SPELL_SRC_DIR == "" ]] ; then
                                  SPELL_SRC_DIR=${SPELL_NAME}-${SPELL_VERSION}
                                  PREBUILDISON="PRE_BUILD, "
                                  rm -f ${QUILL_TMP_DIR}/${SPELL_NAME}-PRE_BUILD
                                  touch ${QUILL_TMP_DIR}/${SPELL_NAME}-PRE_BUILD
                                  echo "mk_source_dir \$SOURCE_DIRECTORY  &&" >> ${QUILL_TMP_DIR}/${SPELL_NAME}-PRE_BUILD
                                  echo "cd \$SOURCE_DIRECTORY  &&" >> ${QUILL_TMP_DIR}/${SPELL_NAME}-PRE_BUILD
                                  echo "unpack_file ''" >> ${QUILL_TMP_DIR}/${SPELL_NAME}-PRE_BUILD
                                fi ;;
                             *) SPELL_SRC_DIR=${SPELL_NAME}-${SPELL_VERSION}
                                message "Unsupported format..."
                                return 2 ;;
    esac
    message "Done.\n"
  fi
}

#---
## @Synopsis generate DETAILS file
##
## @Globals SPELL_SRC_URL SPELL_NAME SPELL_VERSION SPELL_SRC_DIR SPELL_SRC_FILE
## @Globals QUILL_TMP_DIR QUILL_SPELL_HASH SPELL_URL SPELL_LICENSE HISTORY_DATE
## @Globals SPELL_SHORT_DESCRIPTION SPELL_DESC_NAME
#---
function add_details(){
  message "Generating the DETAILS file ..."
  local spell_src_url="$SPELL_SRC_URL"
  local spell_src_dir="$SPELL_SRC_DIR"
  local spell_src_file="$SPELL_SRC_FILE"

  QUILL_SPELL_HASH="$(quill_hash_get $QUILL_TMP_DIR/$SPELL_SRC_FILE)"

  substitute_with_mirror_variables spell_src_url
  substitute_with_variables spell_src_url "$spell_src_file" {SOURCE} \
    "$SPELL_NAME" {SPELL} "$SPELL_VERSION" {VERSION}
  substitute_with_variables spell_src_dir "$SPELL_NAME" {SPELL} "$SPELL_VERSION" {VERSION}
  substitute_with_variables spell_src_file "$SPELL_NAME" {SPELL} "$SPELL_VERSION" {VERSION}

echo \
"           SPELL=${SPELL_NAME}
         VERSION=${SPELL_VERSION}
          SOURCE=\"$spell_src_file\"
   SOURCE_URL[0]=$spell_src_url
     SOURCE_HASH=sha512:${QUILL_SPELL_HASH}
SOURCE_DIRECTORY=\"\${BUILD_DIRECTORY}/$spell_src_dir\"
        WEB_SITE=\"${SPELL_URL}\"
      LICENSE[0]=${SPELL_LICENSE}
         ENTERED=${HISTORY_DATE//-/}
           SHORT=\"${SPELL_SHORT_DESCRIPTION}\"
cat << EOF
$(fmt -u -w80 ${QUILL_TMP_DIR}/${SPELL_DESC_NAME:-$SPELL_NAME})
EOF" > DETAILS
  rm ${QUILL_TMP_DIR}/${SPELL_DESC_NAME:-$SPELL_NAME}

  clear_html_specials DETAILS

  message "Done.\n"
}
#---
##
## 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
##
#---
