#---
## @Synopsis query_list function from init-manager
## $1 - the question
## $2 - the VARIABLE to which to store the answer
## $3...$n - the options for the list
#---
function query_list()
{
  local question="$1"
  shift
  message "$question"
  select_list "$@"
}

#---
## @Synopsis generate basic spell dir
#---
function create_spell_base(){
  mkdir -p ${QUILL_SPELL_DIR}/${SPELL_NAME}
  cd ${QUILL_SPELL_DIR}/${SPELL_NAME}
  touch DETAILS
  chmod +x DETAILS
  DEPENDSISON=""
  CONFLICTSON=""
}

#---
## @Synopsis download spell's source file
#---

function get_spell_source(){
  mkdir -p ${QUILL_TMP_DIR}
  cd ${QUILL_TMP_DIR}
  if [[ -e $SPELL_SRC_FILE ]]; then
    message "Found the spell tarball in the quill dir, continuing ..."
  elif [[ -e $SOURCE_CACHE/$SPELL_SRC_FILE ]]; then
    message "Found the spell tarball in the sorcery cache, copying it over ..."
    cp $SOURCE_CACHE/$SPELL_SRC_FILE .
    message "Done."
  else
    wget -c "${SPELL_SRC_URL}"
  fi
  cd ${QUILL_SPELL_DIR}/${SPELL_NAME}
}

#---
## @Synopsis Print the sha512 hash, a simplified gpg_hashsum
##
#---

function quill_hash_get() {
  gpg --print-md sha512 "$@" | tr -d '\n ' | tr 'A-F' 'a-f' | cut -d: -f2
}

#---
## @Synopsis Get's gurus info and stuff and puts it into
## @Synopsis $QUILL_QUILLRC or reads it from there
#---

function quill_rc() {
  if [[ -f ${QUILL_OLD_QUILLRC} ]]
  then
    message "${QUERY_COLOR}With accordance to Source Mage standards moving${DEFAULT_COLOR}"
    message "${QUERY_COLOR}${QUILL_OLD_QUILLRC} to ${QUILL_QUILLRC}${DEFAULT_COLOR}"
    mkdir -p ~/.sourcemage/quill
    cp ${QUILL_OLD_QUILLRC} ${QUILL_QUILLRC}
    if [[ -f ${QUILL_QUILLRC} ]]
    then
      message "${QUERY_COLOR}Removing ${QUILL_OLD_QUILLRC}${DEFAULT_COLOR}"
      rm ${QUILL_OLD_QUILLRC}
    else
      message "${PROBLEM_COLOR}Ugghhh... something went wrong...${DEFAULT_COLOR}"
    fi
  fi
  if [[ ! -f ${QUILL_QUILLRC} ]] ||
     [[ $QUILL_MODE == reconfigure ]]
  then
    mkdir -p $QUILL_HOME_DIR
    touch $QUILL_QUILLRC
    local quill_version=$QUILL_VERSION # can get overridden by the sourcing
    . $QUILL_QUILLRC # get the contents if there are any (for defaults)
    message "${PROBLEM_COLOR}This will (re)create ${QUILL_QUILLRC} for you${DEFAULT_COLOR}"
    query_string GURU_NAME "${QUERY_COLOR}Please enter your name for the HISTORY entries.${DEFAULT_COLOR}" "$GURU_NAME"
    query_string GURU_EMAIL "${QUERY_COLOR}Please enter your email for the HISTORY entries.${DEFAULT_COLOR}" "$GURU_EMAIL"
    query_string QUILL_SPELL_DIR "${QUERY_COLOR}Where do you want to store generated spells (absolute path)?${DEFAULT_COLOR}" "$QUILL_SPELL_DIR"
    message "Thank you. Now generating ${QUILL_QUILLRC}"
    echo -e "QUILL_CONFIG_VERSION=\"${quill_version}\"" \
            "\nGURU_NAME=\"${GURU_NAME}\"" \
            "\nGURU_EMAIL=\"${GURU_EMAIL}\"" \
            "\nQUILL_SPELL_DIR=\"${QUILL_SPELL_DIR}\"" > $QUILL_QUILLRC
    exit 0
  else
     . ${QUILL_QUILLRC}
  fi
}

#---
## @Synopsis The welcome message for quill
function quill_welcome() {
  message "Welcome to SourceMage GNU/Linux quill - a spell creator script."
  message "-----------------------------------------------------------------------------"
  message "This makes an immediately useable spell from some minor data or "
  message "updates an exsisting one.."
  message "-----------------------------------------------------------------------------"
  message "The spell will be put into a grimoire/section you define(if you choose to)"
  message "and a tar.bz2 file will be created in $QUILL_SPELL_DIR."
  message "-----------------------------------------------------------------------------"
  message "All lists should be space delimited(dependencies and optional dependencies)"
  message "-----------------------------------------------------------------------------"
}

#---
## @Synopsis used to edit spell files
#---
function quill_edit() {
  message "Now invoking $EDITOR to edit spell files."
  cd ${QUILL_SPELL_DIR}/${SPELL_NAME}
  for SPELL_FILE in ${1:-*}
  do
    $EDITOR ${SPELL_FILE}
  done
  message "All modifications complete."
}

#---
## @Synopsis generate a tarball of the spell
#---
function quill_final_tarball() {
  message "Now creating a bziped2 tarball of the spell files"
  cd ${QUILL_SPELL_DIR}
  tar -jcvf ${SPELL_NAME}.tar.bz2 ${SPELL_NAME}
  if [[ $? != 0 ]]; then
     message "Failed to create spell tarball"
  else
     message "Spell tarball created successfully"
  fi
}

#---
## @Synopsis function to chmod +x all the relevant spell files
#---
function quill_set_executable_bit() {
  for SPELL_FILE in ${QUILL_SPELL_DIR}/${SPELL_NAME}/*
  do
    if [[ "$SPELL_FILE" != "${QUILL_SPELL_DIR}/${SPELL_NAME}/HISTORY" ]]
    then
      chmod +x $SPELL_FILE
    fi
  done
}

#---
## @Synopsis function to put the spell into the grimoire
#---
function quill_final_put_in_grimoire() {

  query_list "Into which grimoire do you wish to put the spell:" QUILL_GRIM_NAME \
    $(codex_get_all_grimoires | get_basenames)
  echo
  QUILL_GRIM_NAME=$(codex_canonicalize_grimoire_name $QUILL_GRIM_NAME)

  # can't use query_list, as the section count can be bigger than the available index
  message "Available sections in grimoire $QUILL_GRIM_NAME:"
  codex_get_section_names $QUILL_GRIM_NAME | column
  query_string QUILL_SECT_NAME "Into which section do you wish to put the spell: "

  message "Scribbling spell into $QUILL_GRIM_NAME/$QUILL_SECT_NAME/$SPELL_NAME ..."

  quill_final_put_in_grimoire_sub(){
    mkdir -p $QUILL_GRIM_NAME/$QUILL_SECT_NAME
    cp -r $QUILL_SPELL_DIR/$SPELL_NAME $QUILL_GRIM_NAME/$QUILL_SECT_NAME/
    if [[ $? != 0 ]]; then
      echo "Scribbling failed!"
    else
      echo "Scribbling succeded"
      echo
      echo "Running scribe reindex on $QUILL_GRIM_NAME ..."
      scribe reindex $(basename $QUILL_GRIM_NAME)
    fi
  }
  if [[ $UID == 0 ]]; then
    quill_final_put_in_grimoire_sub
  else
    export QUILL_GRIM_NAME QUILL_SECT_NAME QUILL_SPELL_DIR SPELL_NAME
    su -c "$(declare -f quill_final_put_in_grimoire_sub); \
      quill_final_put_in_grimoire_sub"
  fi

}

#---
## @Synopsis function that displays the version and exits
#---
function quill_version() {


  message "quill - $QUILL_VERSION
Copyright (C) 2006 Source Mage
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Originaly written by:
Andraž 'ruskie' Levstik
Rewritten by:
Andraž 'ruskie' Levstik
Jaka 'lynxlynxlynx' Kranjc
Juuso 'iuso' Alasuutari

Contributions by:
Andrew 'afrayedknot' Stitt
Arjan 'abouter' Bouter
"
  exit 0
}


#---
## @Synopsis function that displays help and exits
#---
function quill_help() {


  message "quill -$QUILL_VERSION
Copyright (C) 2006 Source Mage
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

USAGE: quill [OPTIONS]

OPTIONS:
  --fmxml, -f <SPELL>		get spell data from Freshmeat if possible
  --perlcpan, -c <SPELL>		get spell data from Perl-CPAN if possible
  --update, -u <SPELL>		update exsisting spell (DISABLED)
  --apprentice, -a		apprentice mode	(default)
  --mage, -m			mage mode (advanced)
  --wizard, -w			wizard mode (expert)(DOES NOTHING FOR NOW)
  --help, -h			display this help
  --version, -v			display version
  --reconfigure, -r		reconfigure settings
"
  exit ${1:-0}
}

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

