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

##
## @Globals none
function quill_fmxml_grab_from_xml() {
  xml_grep --text_only "${@}"
}

##
## @Globals SPELL_NAME FRESHMEAT_XML_URL FRESHMEAT_PROJECT_URL SPELL_DESC_NAME
## @Globals FRESHMEAT_PROJECT_FILE FRESHMEAT_FILE QUILL_TMP_DIR SPELL_SRC_URL
## @Globals SPELL_SRC_URL SPELL_LICENSE SPELL_SHORT_DESCRIPTION
function quill_fmxml_core() {
  # so it is set even if we end prematurely, useful when adding spells
  # gets properly overwritten later on
  SPELL_NAME="$1"

  # 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 grep -q "http://" <<< "${1}" ; then
    FRESHMEAT_XML_URL="$1"
  else
    FRESHMEAT_XML_URL="http://freshmeat.net/projects/${1}.xml"
    FRESHMEAT_PROJECT_URL="http://freshmeat.net/projects/${1}/"
  fi

  # check if we already have the xml file, if we dont, download it
  FRESHMEAT_FILE="${QUILL_TMP_DIR}/${1}.xml"

  FRESHMEAT_PROJECT_FILE="${QUILL_TMP_DIR}/${1}.fm"

  if [[ ! -e ${FRESHMEAT_PROJECT_FILE} ]]; then
    message -n "Attempting to retrieve project page ... "
    wget -q -Uquill -O ${FRESHMEAT_PROJECT_FILE} "${FRESHMEAT_PROJECT_URL}" ||
      { error_msg "Error: unable to fetch project page" && return 1; }
    tr "\r" " " < ${FRESHMEAT_PROJECT_FILE} > ${FRESHMEAT_PROJECT_FILE}.tmp
    mv ${FRESHMEAT_PROJECT_FILE}.tmp ${FRESHMEAT_PROJECT_FILE}
    message "Done"
  fi

  if [[ ! -e ${FRESHMEAT_FILE} ]]; then
    message -n "Attempting to retrieve project XML page ... "
    wget -q -Uquill -O ${FRESHMEAT_FILE} "${FRESHMEAT_XML_URL}" ||
      { error_msg "Error: unable to fetch project XML page" && return 1; }
    message "Done"
  fi

  # check that the project was found - exsists
  local not_found='404 Not Found'
  if grep -q "$not_found" "$FRESHMEAT_PROJECT_FILE"; then
    error_msg "$not_found"
    rm "$FRESHMEAT_PROJECT_FILE"
    return 1
  fi

  # fill in variables from xml file

  SPELL_NAME=$(quill_fmxml_grab_from_xml permalink ${FRESHMEAT_FILE} |tr 'A-Z' 'a-z')
  SPELL_NAME="${SPELL_NAME:-$1}"
  SPELL_DESC_NAME="${SPELL_NAME}"

  local tmp_url
  for each in "Tar/BZ2" "Tar/GZ" "Zip" ; do
    tmp_url="$(quill_fmxml_grab_from_xml --cond "//approved-url/label" --cond "//approved-url/redirector" ${FRESHMEAT_FILE} | grep -A1 "$each" | tail -n1)"
    if [[ -n $tmp_url ]]; then
      SPELL_SRC_URL="$(wget -T 3 --spider $tmp_url 2>&1 | grep "Location:" | sed -e "s:Location\: \(.*\) .*:\1:" | tail -n1)"
    fi
  done

  local tmp_url
  for each in "Website" "Homepage" "Web Site" "Project Home" "Home Page" ; do
    tmp_url="$(quill_fmxml_grab_from_xml --cond "//approved-url/label" --cond "//approved-url/redirector" ${FRESHMEAT_FILE} | grep -A1 "$each" | tail -n1)"
    if [[ -n $tmp_url ]]; then
      SPELL_URL="$(wget -T 3 --spider $tmp_url 2>&1 | grep "Location:" | sed -e "s:Location\: \(.*\) .*:\1:" | tail -n1)"
    fi
  done

# this seems to be harder and harder to get not in the api :(
#  SPELL_LICENSE="$(quill_fmxml_grab_from_xml license ${FRESHMEAT_FILE})"
#  if grep -q "(.*)" <<< "${SPELL_LICENSE}"; then
#    SPELL_LICENSE="$(awk '{print $NF}' <<< "$SPELL_LICENSE" | tr -d '()')"
#  fi

  SPELL_LICENSE=$(sed -n '/class=\"tag-list padtop licenses\"/,/<\/div>/ p' ${FRESHMEAT_PROJECT_FILE} | tr "\n" " " | sed -e "s:.*class=\"tagSize4\">\(.*\)</a>.*:\1:g")

  SPELL_SHORT_DESCRIPTION="$(quill_fmxml_grab_from_xml oneliner ${FRESHMEAT_FILE}|sed 's/\r//g')"

  quill_fmxml_grab_from_xml description ${FRESHMEAT_FILE} |fmt -w 80 -s > ${QUILL_TMP_DIR}/${SPELL_NAME}

}

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

