#!/bin/bash
#---
## @Synopsis Spell creator script for SourceMage GNU/Linux
## @Copyright Copyright 2006 Andrew "ruskie" Levstik <ruskie@mages.ath.cx>
## @Copyright Copyright 2006 SourceMage GNU/Linux
## @License GPL v2 or higher
##
## @ToDo Add option to use upstream gpg signature as included or 
## @ToDo downloadable .sig file
## @ToDo Put a multiversion option
## @ToDo Make an option to edit all the existing files after generation
## @ToDo Make configuring optional depends a must     -     DONE
## @ToDo Add logic to quill to make it possible to use optional_depends     -     DONE
## @ToDo single line or multiline depending on the length of line if 
## @ToDo <80 single else multi
## @ToDo Interface to add CONFIGURE options
## @ToDo Automated bugcli submissions of spells
## @ToDo Make a tar.bz2 of the spell immediately
## @ToDo Automatic inclusion into a grimoire when finished
## @ToDo Make it use the ~/.sourcemage dir for config     -     DONE
## @ToDo Add cmdline options to make it possible to autoscript it
## @ToDo Add a better configuration interface(dialog based I hope)
## @ToDo Make it possible to also update spells using quill
## @ToDo Make quill the be-all for spells
## @ToDo Make quill interface with both sorcery and grimoire to get 
## @ToDo functions
## @ToDo make it possible to reconfigure quill without lossing existing settings
##
## Asks the user in simple questions about generating a spell.
## @Globals BUILDISON, INSTALLISON, PREBUILDISON, TRIGGERSISON
## @Globals SPELL_HISTORY_NAME, SPELL_HISTORY_EMAIL, SPELL_NAME,
## @Globals SPELL_SRC_URL, SPELL_LICENSE, SPELL_URL, SPELL_SHORT_DESCRIPTION,
## @Globals SPELL_DESCRIPTION, SPELL_DEPENDENCIES, SPELL_OPTIONAL_DEPENDENCIES,
## @Globals SPELL_CONFLICTS, HISOTRY_DATE, SPELL_DATE, SPELL_SRC_FILE,
## @Globals SPELL_SANITIZED_FILE_NAME, SPELL_MD5_UNPACKED, SPELL_VERSION,
## @Globals CURRENTPWD, i, j, n, BUILD_DTFILE, DTFILE_MENUENTRY, DTFILE_EXEC
## @Globals DTFILE_MENUPATH, DTFILE_ICON, DTFILE_TERM, SPELL_SRC_DIR
##
## @Thanks BearPerson, dufflebunk, afrayedknot
## @Thanks To all testers
## @Contribution abouter, lynxlynxlynx, iuso
##
#---
QUILL_VERSION="$(cat /var/lib/quill/version)"
#---
## Some basic stuff that should be loaded and set
. /etc/sorcery/config
for i in /var/lib/quill/modules/lib*
do
  . $i
done
QUILL_MODE="apprentice"
CURRENTPWD="$(pwd)"
HISTORY_DATE="$(date +%Y-%m-%d)"
#---
## Not really a function it's supposed to parse the command line parameters
#---
while  [  -n  "$1"  ];  do
  if  echo  "" $1  |  grep  -q  "^ -";  then
    case  $1  in
      --fmxml|-f)
                  [[ -z $2 ]] && 
                  message "${PROBLEM_COLOR}Missing parameter$DEFAULT_COLOR" &&
                  quill_help
                  export QUILL_FMXML_MODE="on"; export QUILL_FMXML_PARAM="$2"
                  shift 2 ;;
 --apprentice|-a) export QUILL_MODE="apprentice"; shift 1 ;;
       --mage|-m) export QUILL_MODE="mage"; shift 1 ;;
     --wizard|-w) export QUILL_MODE="wizard"; shift 1 ;;
--reconfigure|-r) export QUILL_MODE="reconfigure"; shift 1 ;;
       --help|-h) quill_help ;;
    --version|-v) message "Quill version: ${QUILL_VERSION}" && exit 0;;
               *) message "${PROBLEM_COLOR}Invalid parameter $1$DEFAULT_COLOR"
                  quill_help ;;
    esac
  else
    shift
  fi
done

QUILL_HOME_DIR=~/.sourcemage/quill
QUILL_QUILLRC=${QUILL_HOME_DIR}/quillrc
QUILL_OLD_QUILLRC=~/.quillrc
QUILL_SPELL_DIR=~/.sourcemage/spells
QUILL_TMP_DIR=~/.sourcemage/tmp

quill_rc
PROMPT_DELAY="4294967295"
BUILD_API=2
load_build_api
mkdir -p $QUILL_TMP_DIR

## Down to here... from here it's question and answer time
#---

#---
## Questions and stuff

quill_welcome
if [[ "${QUILL_MODE}" == "apprentice" ]] ||
   [[ "${QUILL_MODE}" == "mage" ]] ||
   [[ "${QUILL_MODE}" == "wizard" ]]
then
  if [[ "${QUILL_FMXML_MODE}" == "on" ]]
  then
    QUILL_FMXML_SPELL="$QUILL_FMXML_PARAM"
    quill_fmxml_core $QUILL_FMXML_PARAM
  fi

  query_spell_name
  query_spell_source_url
  query_spell_license
  query_spell_url
  query_spell_short_description
  query_spell_description
  query_spell_dependencies
  query_spell_optional_dependencies
  query_spell_build
  query_spell_install
  query_spell_desktop_file

  if [[ "${QUILL_MODE}" == "mage" ]] || [[ "${QUILL_MODE}" == "wizard" ]]
  then
    query_spell_pre_build
    query_spell_prepare
    query_spell_conflicts
    #-query_spell_configure
    query_spell_provides
    #-query_spell_final
    #-query_spell_triggers
    #-query_spell_solo
    #-query_spell_configs
    #-query_spell_volatiles
    if [[ "${QUILL_MODE}" == "wizard" ]]
    then
      #-  query_spell_pre_install
      #-  query_spell_post_install
      #-  query_spell_pre_remove
      #-  query_spell_post_remove
      #-  query_spell_pre_resurrect
      #-  query_spell_post_resurrect
      true
    fi
  fi


  create_spell_base

  if [[ "${SPELL_DEPENDENCIES}" != "" ]]
  then
    add_dependencies
  fi

  if [[ "${SPELL_OPTIONAL_DEPENDENCIES}" != "" ]]
  then
    add_optional_dependencies
  fi

  # add && where appropriate in DEPENDS
  if [[ ${SPELL_OPTIONAL_DEPENDENCIES} != "" ]] || [[ ${SPELL_DEPENDENCIES} != "" ]]
  then
    sed -e 's/^ *$//' -e 's/[^\\]$/& \&\&/' DEPENDS | tac | 
      awk '{ if (removed!=1){ if (sub("&&$","") > 0){ removed=1 }}; print }' | 
      tac > temporary-DEPENDS
    mv temporary-DEPENDS DEPENDS
  fi

  if [[ "${SPELL_CONFLICTS}" != "" ]]
  then
    add_conflicts
  fi

  if [[ "${SPELL_PROVIDES}" != "" ]]
  then
    add_provides
  fi

  parse_spell_source_file_info

  show_spell_source_file_info

  download_spell_source

  substitute_url_variables

  hunt_src_dir

  add_details

  if [[ "${PREPAREISON}" == "PREPARE, " ]]
  then
    add_prepare
  fi

  #-if [[ "${CONFIGUREISON}" == "CONFIGURE, " ]]
  #-then
  #-  add_configure
  #-fi

  if [[ "${PREBUILDISON}" == "PRE_BUILD, " ]]
  then
    add_pre_build
  fi

  if [[ "${BUILDISON}" == "BUILD, " ]]
  then
    add_build
  fi

  if [[ "${INSTALLISON}" == "INSTALL, " ]]
  then
    add_install
  fi

  #-if [[ "${POSTINSTALLISON}" == "POST_INSTALL, " ]]
  #-then
  #-  add_post_install
  #-fi

  #-if [[ "${TRIGGERSISON}" == "TRIGGERS, " ]]
  #-then
  #-  add_triggers
  #-fi

  if [[ "${BUILD_DTFILE}" != "" ]]
  then
    add_desktop_file
  fi

  add_history
  if query "Do you want to edit all the spell files?" n
  then
    quill_edit
  fi
  if query "Do you want to tarball the spell?" n
  then
    quill_final_tarball
  fi
  if query "Do you want to put the spell into one of the avialable grimoires?" n
  then
    quill_final_put_in_grimoire
  fi
fi
cd ${CURRENTPWD}
#---
##
## 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
##
#---
