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

function query_spell_name(){
if ! [[ $SPELL_NAME ]] ||
   ! query "Is $SPELL_NAME the proper spell name?" y; then
  message "${QUERY_COLOR}Please enter the spell name:${DEFAULT_COLOR}"
  read "SPELL_NAME"
fi
}

function query_spell_source_url(){
if ! [[ $SPELL_SRC_URL ]] ||
   ! query "Is $SPELL_SRC_URL the proper source url?" y; then
  message "${QUERY_COLOR}Please enter the url of the source:${DEFAULT_COLOR}"
  read "SPELL_SRC_URL"
fi
}

function query_spell_signature(){
if [[ $SPELL_SIGNATURE ]] &&
   ! query "Is $SPELL_SIGNATURE the proper signature key url?" y; then
  message "${QUERY_COLOR}Please enter the url of the signature key:${DEFAULT_COLOR}"
  read "SPELL_SIGNATURE"
fi
if ! [[ $SPELL_SIGNATURE ]]; then
  if query "Do you want to use an upstream signature key?" n; then
    message "${QUERY_COLOR}Please enter the url of the signature key:${DEFAULT_COLOR}"
    read "SPELL_SIGNATURE"
  fi
fi
if [[ $SPELL_SIGNATURE ]] &&
   ! [[ $DOWNLOAD_SIGNATURE ]]; then
  if query "Use downloadable upstream signature key?" y; then
    DOWNLOAD_SIGNATURE="y"
  fi
fi
}

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

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

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
  message "${QUERY_COLOR}Please enter a short description of the spell:${DEFAULT_COLOR}"
  read "SPELL_SHORT_DESCRIPTION"
fi
}

function query_spell_description(){
local description=${QUILL_TMP_DIR}/${SPELL_DESC_NAME:-${SPELL_NAME}}
if ! test -f $description ||
   ! ( message "${QUERY_COLOR}Is${DEFAULT_COLOR}"
       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.
#---
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")
}

#---
## @Synopsis Print on screen info gathered from source 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 string
## @Synopsis with corresponding variable names, store resulting string
## @Synopsis in $SUBSTITUTIONS. $1 is target variable, $2 and so on are
## @Synopsis variables to substitute with var names. Omit $, give only
## @Synopsis var names. Example:
## @Synopsis substitute_with_variables SPELL_SRC_URL SPELL VERSION
#---
function substitute_with_variables() {
if [ -n "$1" ]; then
  local TARGET_STRING="${!1}"
  shift 1
  while [ -n "$1" ]; do
    TARGET_STRING=${TARGET_STRING//${!1}/\$\{$1\}}
    shift 1
  done
  SUBSTITUTIONS=$TARGET_STRING
fi
}

#---
## @Synopsis Process SPELL_SRC_URL: Subsitute expanded with escaped
## @Synopsis SPELL and VERSION, Substitute mirror urls with mirror url
## @Synopsis variables.
#---
function substitute_url_variables(){
  substitute_with_variables SPELL_SRC_URL SPELL VERSION
  SPELL_SRC_URL="$SUBSTITUTIONS"
  substitute_with_mirror_variables SPELL_SRC_URL
  SPELL_SRC_URL="$SUBSTITUTIONS"
}

#---
## @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
#---
function substitute_with_mirror_variables(){
if [ -n "$1" ]; then
  local TARGET_STRING="${!1}"
  local MIRROR_LIST_FILE
  local MIRROR_VARIABLE
  local MIRROR_ENTRY
  for MIRROR_LIST_FILE in $(ls /etc/sorcery/mirrors/); do
    MIRROR_VARIABLE="${MIRROR_LIST_FILE}_URL"
    for MIRROR_ENTRY in $(cat /etc/sorcery/mirrors/${MIRROR_LIST_FILE}); do
      if [[ "$MIRROR_ENTRY" =~ "://" ]]; then
        TARGET_STRING=${TARGET_STRING//$MIRROR_ENTRY/\$\{$MIRROR_VARIABLE\}}
      fi
    done
  done
  SUBSTITUTIONS="$TARGET_STRING"
fi
}

#---
## @Synopsis Check if file is a tarball.
## @return 0 if a tarball
## @return 1 if not a tarball
#---
function check_if_tarball(){
if [[ -z $(tar tf "${QUILL_TMP_DIR}/${SPELL_SRC_FILE}" 2>&1 | grep "^tar: " | head -n1) ]]; then
  return 0
else
  return 1
fi
}

#---
## @Synopsis Assign to SPELL_SRC_DIR the source dir inside a tarball,
## @Synopsis substitute expanded with escaped SPELL and VERSION.
## @return 0 if a tarball and has a src dir
## @return 1 if a tarball but has no src dir
## @return 2 if not a tarball.
#---
function hunt_src_dir(){
if check_if_tarball; then
  SPELL_SRC_DIR=$(tar tf "${QUILL_TMP_DIR}/${SPELL_SRC_FILE}" | grep / | sed -e "s=/.*$==g;q")
  if [ -n "$SPELL_SRC_DIR" ]; then
    SPELL_SRC_DIR=${SPELL_SRC_DIR//${SPELL_NAME}/\$\{SPELL\}}
    SPELL_SRC_DIR=${SPELL_SRC_DIR//${SPELL_VERSION}/\$\{VERSION\}}
    return 0
  else
    return 1
  fi
else
  unset SPELL_SRC_DIR
  return 2
fi
}

#---
## @Synopsis generate DETAILS file
#---
function add_details(){

  SPELL_DATE=$(date +%Y%m%d)

  message "Generating DETAILS file..."

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

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