#! /bin/sh
#
#+
# .. program:: checkmain.sh
#
# ============
# checkmain.sh
# ============
#
# SYNOPSIS
# ========
#
# ::
#
#  $ checkmain.sh [--debug] -i filein
#
# DESCRIPTION
# ===========
#
# .. option:: -i <filein>
#
# Check SUPERBIB requirements in main.xml **filein** XML/DocBook.
# 
# project
#   Should be defined :samp:`//d:keyword[@xml:id='project']`
#
#   Is used for ++
# title
#   Should be defined :samp:`d:article/d:title`
#
#   Is used for ++
# homepage
#   Should be defined :samp:`//d:bibliosource[@xml:id='homepage']`
#
#   Is used for ++
# contact
#   Should be defined :samp:`/d:article/d:info/d:authorgroup/d:author[1]/d:email`
#
#   Is used by PHP pages to send emails.
#
# .. warning::
#
#    There is now check on email and homepage. 
#    They **must** be real to avoid black holes of mails and 404 HTPP errors.
#
# EXAMPLES
# ========
#
# To check the SUPERBIB demo main file::
#
#   $ ./checkmain.sh -i data/maindemo1.xml
#   Title : superbib Demo1
#   Project : superbib demo1
#   HOME page : http://forge.ipsl.jussieu.fr/superbib/
#   Contact : Francoise.Pinsard@locean-ipsl.upmc.fr
#
# To check the NEMO demo main file::
#
#   $ ./checkmain.sh -i data/mainnemo.xml
#   Title : NEMO survey
#   Project : NEMO
#   HOME page : http://www.nemo-ocean.eu/
#   Contact : opatlod@locean-ipsl.upmc.fr
#
# SEE ALSO
# ========
#
# :ref:`using`
#
# :ref:`components_db.xsl`
# :ref:`form_db.xsl`
# :ref:`main_html.xsl`
# :ref:`main_rest.xsl`
# :ref:`superbib01_html.xsl`
# :ref:`superbib01_rest.xsl`
# :ref:`superbib01_xml.xsl`
# :ref:`superbib02_html.xsl`
# :ref:`superbib02_xml.xsl`
# :ref:`superbib03_xml.xsl`
# :ref:`superbibmany01_html.xsl`
# :ref:`superbibmany01_xml.xsl`
# :ref:`superbibmany02_html.xsl`
# :ref:`superbibmany02_xml.xsl`
# :ref:`template_db.xsl`
# :ref:`user_db.xsl`
# :ref:`user_html.xsl`
# :ref:`user_rest.xsl`
#
# TODO
# ====
#
# improve description
#
# XML/Docbook compliant : pb with element form: Schemas validity error : Element '{http://www.w3.org/1999/xhtml}form': This element is not expected.
# .. so as I do not know haox to get rid of this I comment this test (see XFORM and so one)
#
# check for news on DocBook realase (now 20110701 5.0)
#
#
# EVOLUTIONS
# ==========
#
# $URL$
#
# $Id$
#
# - fplod 20120227
#
#   * xmlstarlet vs xml
#
# - fplod 20110701T113247Z cratos.locean-ipsl.upmc.fr (Linux)
#
#   * creation
#
#-
system=$(uname)
case "${system}" in
   AIX|IRIX64)
      echo "www : no specific posix checking"
      date_cmd=date
      xmlcmd=xml
   ;;
   Darwin)
      set -o posix
      date_cmd=gdate
      xmlcmd=xmlstarlet
   ;;
   Linux)
      set -o posix
      date_cmd=date
      xmlcmd=xml
   ;;
   *)
     set -o posix
   ;;
esac
unset system
#
set -u
#
LANG=POSIX
#
command=$(basename ${0})
#
usage=" Usage : ${command} [--debug] -i filein "
#
hostname=$(hostname)
#
# default
debug=0
#
minargcount=2
if [ ${#} -lt ${minargcount} ]
then
   echo "${command} : eee : not enought arguments"
   echo "${usage}"
   exit 1
fi
#
while [ ${#} -gt 0 ]
do
   case ${1} in
      --debug)
         debug=1
      ;;
      -i)
         filein=${2}
         shift
      ;;
      *)
        # anything else
        echo "${command} : eee : unknown option ${1}"
        echo "${command} : eee : ${usage}"
        exit 1
      ;;
   esac
   # next flag
   shift
done
#
# check parameters
#
# check for filein
if [ ! -f ${filein} ]
then
   echo "eee : ${filein} not found"
   exit 1
fi
#
# test if xml available
tool=${xmlcmd}
type ${tool} 1> /dev/null 2>&1
status=${?}
if [ ${status} -ne 0 ]
then
   echo "${command} : eee : ${tool} not found"
   exit 1
fi
unset status
unset tool
#
#++# check XML/DocBook conformity
#++${xmlcmd} val --err \
#++--xsd http://www.docbook.org/xml/5.0/xsd/docbook.xsd \
#++${filein}
#++status=${?}
#++if [ ${status} -ne 0 ]
#++then
#++   echo "${command} : eee : ${filein} is not XML/DocBook compliant"
#++   exit 1
#++fi
#++unset status
#
# title
xpath="d:article/d:title"
title=$(${xmlcmd} sel -N d="http://docbook.org/ns/docbook" -t -m "${xpath}" -v . ${filein})
if [ "${title}" = "" ]
then
   echo "${command} : www : title not defined"
   echo "${command} : www : fix ${xpath}"
   exit 1
else
   echo "Title : ${title}"
fi
unset title
#
#
# project
project_ok=0
xpath="//d:keyword[@xml:id='project']"
project=$(${xmlcmd} sel -N d="http://docbook.org/ns/docbook" -t -m "${xpath}" -v . ${filein})
if [ "${project}" = "" ]
then
   echo "${command} : www : project not defined"
   echo "${command} : www : fix ${xpath}"
else
   echo "Project : ${project}"
   project_ok=1
fi
unset project
#
# homepage
homepage_ok=0
xpath="//d:bibliosource[@xml:id='homepage']"
homepage=$(${xmlcmd} sel -N d="http://docbook.org/ns/docbook" -t -m "${xpath}" -v . ${filein})
if [ "${homepage}" = "" ]
then
   echo "${command} : www : homepage not defined"
   echo "${command} : www : fix ${xpath}"
else
   echo "HOME page : ${homepage}"
   homepage_ok=1
fi
unset homepage
#
# contact
contact_ok=0
xpath="/d:article/d:info/d:authorgroup/d:author[1]/d:email"
contact=$(${xmlcmd} sel -N d="http://docbook.org/ns/docbook" -t -m "${xpath}" -v . ${filein})
if [ "${contact}" = "" ]
then
   echo "${command} : www : contact not defined"
   echo "${command} : www : fix ${xpath}"
else
   echo "Contact : ${contact}"
   contact_ok=1
fi
unset contact
#
unset command
unset hostname
unset usage
#
# end
#++set
exit 0
