#---
## @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 download_spell_source(){
  mkdir -p ${QUILL_TMP_DIR}
  cd ${QUILL_TMP_DIR}
  wget -c "${SPELL_SRC_URL}"
  cd ${QUILL_SPELL_DIR}/${SPELL_NAME}
}

#---
## @Synopsis hash_get function by afreydknot
##
#---

function hash_get() {
  gpg --print-md sha512 \
  "$@" |\
  tr -d '\n ' | tr 'A-F' 'a-f' |\
  awk -F: '{printf $2}'
}

#---
## @Synopsis Get's gurus info and stuff and puts it into
## @Synopsis ~/.sourcemage/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
    touch ${QUILL_QUILLRC}
    message "${PROBLEM_COLOR}This will create a ${QUILL_QUILLRC} file for you${DEFAULT_COLOR}"
    message "${QUERY_COLOR}Please enter your name for the HISTORY entry.${DEFAULT_COLOR}"
    read "GURU_NAME"
    message "${QUERY_COLOR}Please enter your email for the HISTORY entry.${DEFAULT_COLOR}"
    read "GURU_EMAIL"
    message "${QUERY_COLOR}Where do you want to store generated spells(absolute path).${DEFAULT_COLOR}"
    read "QUILL_SPELL_DIR"
    message "Thank you. Now generating ${QUILL_QUILLRC}"
    echo -e "QUILL_VERSION=\"${QUILL_VERSION}\"\nGURU_NAME=\"${GURU_NAME}\"\nGURU_EMAIL=\"${GURU_EMAIL}\"\nQUILL_SPELL_DIR=\"${QUILL_SPELL_DIR}\"" > ${QUILL_QUILLRC}
  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."
  message "Does not create a \$SPELL-\$VERSION SOURCE construct but uses real filename"
  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:-nano} to edit spell files."
  cd ${QUILL_SPELL_DIR}/${SPELL_NAME}
  for SPELL_FILE in *
  do
    ${EDITOR:-nano} ${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 put the spell into the grimoire
#---
function quill_final_put_in_grimoire() {
  message "You have selected to put the spell into the grimoire"
  message "Please note there will also be a spell tarball(if so desired) in" 
  message "${USER_HOME}/${QUILL_SPELLDIR} as will be a spelldir"
  message "-----------------------------------------------------------------------------"
  message "Into which grimoire do you wish to put the spell(FULL path to it):"
#  quill_list_grimoires
  read QUILL_GRIM_NAME
  message "Into which section do you wish to put the spell:"
#  quill_list_sections
  read QUILL_SECT_NAME
  message "Now scribling spell into ${QUILL_GRIM_NAME}/${QUILL_SECT_NAME}/${QUILL_SPELL_NAME}"
  cd ${QUILL_SPELL_DIR}/${QUILL_SPELL_NAME}
  mkdir -p ${QUILL_GRIM_NAME}/${QUILL_SECT_NAME}
  cp -r ${SPELL_NAME} ${QUILL_GRIM_NAME}/${QUILL_SECT_NAME}/
  if $? != 0 ; then
     message "Scribbling failed"
  else
     message "Scribbling succeded"
     message "Now running scribe reindex on ${QUILL_GRIM_NAME}"
     scribe reindex $(basename ${QUILL_GRIM_NAME})
  fi
}

#---
## @Synopsis function that displays help and exits
#---
function quill_help() {
  message "USAGE: quill [OPTIONS]

OPTIONS:  
  --fmxml, -f <SPELL>		get spell data from Freshmeat if possible
  --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(DOES NOT WORK YET)
"
  exit 1
}

