#---
## @Synopsis libfreshmeatxml
#---

function quill_fmxml_grab_from_xml() {
  xml_grep --text_only "$1" "${QUILL_TMP_DIR}/${2}.xml"
}


function quill_fmxml_hunt_src_url() {
  local URL
  local SPELL_NAME=$1
  URL=$(curl -I $2 2>/dev/null|grep Location|awk '{print $2}'|sed 's/\r//g')
  # hacky attempt to get source url from sourceforge
  if echo $URL|grep -q sourceforge; then
    curl $URL 2>/dev/null|tr '<>' '\n\n'|grep http|grep sourceforge|\
                              grep download|grep -i $SPELL_NAME|\
                              cut -f2 -d\"|cut -f1 -d?|head -n 1
  else
    echo $URL
  fi
}

function quill_fmxml_core(){
  # if the argument looks like a url then use that, otherwise
  # assume the argument is the name of the spell and construct a
  # url for where we think freshmeat.net stores their xml data
  if echo $1|grep -q http://; then
    FRESHMEAT_XML_URL="$1"
  else
    FRESHMEAT_XML_URL=http://freshmeat.net/projects-xml/${1}/${1}.xml
  fi
  # check if we already have the xml file, if we dont, download it
  if test -f ${QUILL_TMP_DIR}/${1}.xml ; then
    FRESHMEAT_FILE=${1}.xml
  else
    if ! curl -o ${QUILL_TMP_DIR}/${1}.xml ${FRESHMEAT_XML_URL} ; then
      message "${PROBLEM_COLOR}Failed to get ${1}${DEFAULT_COLOR}"
      exit 1
    fi
    FRESHMEAT_FILE=$FRESHMEAT_FILE
  fi

  # check that the project was found - exsists
  local not_found='Error: project not found.'
  if grep -q "$not_found" "$QUILL_TMP_DIR/$1.xml"; then
    message "$PROBLEM_COLOR$not_found$DEFAULT_COLOR"
    rm "$QUILL_TMP_DIR/$1.xml"
    exit 2
  fi

  # fill in variables from xml file
  if [[ $1 ]] ; then
    SPELL_NAME=$(quill_fmxml_grab_from_xml projectname_short $1|tr 'A-Z' 'a-z')
    SPELL_DESC_NAME="${SPELL_NAME}"
    for each in url_bz2 url_tgz; do
      TMP=$(quill_fmxml_grab_from_xml $each $1)
      if [[ $TMP ]] ; then
        SPELL_SRC_URL=$(quill_fmxml_hunt_src_url $SPELL_NAME $TMP)
        break
      fi
    done
    SPELL_URL=$(curl -I $(quill_fmxml_grab_from_xml url_homepage $1) 2>&1|\
                grep Location|awk '{print $2}'|sed 's/\r//g')

    SPELL_LICENSE=$(quill_fmxml_grab_from_xml license $1)
    if grep -q "(.*)" <<< "$SPELL_LICENSE"; then
      SPELL_LICENSE=$(awk '{print $NF}' <<< "$SPELL_LICENSE" | tr -d '()')
    fi
    SPELL_SHORT_DESCRIPTION=$(quill_fmxml_grab_from_xml desc_short $1|sed 's/\r//g')
    quill_fmxml_grab_from_xml desc_full $1 |fmt > ${QUILL_TMP_DIR}/${SPELL_NAME}
  fi
}
#---
##
## 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
##
#---

