#! /bin/sh
#+
#
# .. _mailtouser.sh:
#
# .. program:: mailtouser.sh
#
# =============
# mailtouser.sh
# =============
#
# SYNOPSIS
# ========
#
# .. code-block:: sh
#
#    mailtouser.sh -m mailbodyfile -x xmloutput
#
# DESCRIPTION
# ===========
#
# .. option:: -m <mailbodyfile>
# .. option:: -x <xmloutput>
#
# convert email text in xml form following :file:`user.dtd`
#
# SEE ALSO
# ========
#
# :ref:`bibopa.sh`
#
# EXAMPLES
# ========
#
# .. code-block:: sh
#
#    mailtouser.sh -m data/mail2007-05-10T09:01:56Z -x ginette.xml
#
# TODO
# ====
#
# ++ gestion des comments
#
# ++ gestion de la signature
#
# EVOLUTIONS
# ==========
#
# $Id$
#
# - fplod 2008-09-16T15:42:41Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#  * comments in reStructuredText
#
# - fplod 2007-06-06T09:27:55Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#  * correction for Off-Line and PC Cluster
#
# - fplod 2007-05-18T08:31:48Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#  * add off-line and agrif components
#
# - fplod 2007-05-09T15:03:15Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#  * reprise + ajout middlename + ajout components_date
#
# - fplod 2007-04-25T09:15:38Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#  * creation
#
#-
system=$(uname)
case "${system}" in
    AIX|IRIX64)
        echo " www : no specific posix checking"
    ;;
    *)
        set -o posix
    ;;
esac
unset system
#
set -u
#
command=$(basename ${0})
log_date=$(date -u +"%Y%m%dT%H%M%SZ")
log=/tmp/$(basename ${command} .sh).log.${log_date}
#
usage=" Usage : ${command} -m mailbodyfile -x xmloutput"
#
# test if xmllint is available
tool=xmllint
type ${tool} 1> /dev/null 2>&1
status=${?}
if [ ${status} -ne 0 ]
then
    echo " eee : ${tool} not found"
    exit 1
fi
unset tool
unset status
#
# check for user.dtd
if [ ! -f user.dtd ]
then
    echo "eee : user.dtd not found"
    exit 1
fi
#
while [ ${#} -gt 0 ]
do
    case ${1} in
        -m)
            mailbodyfile=${2}
            shift
        ;;
        -x)
            xmloutput=${2}
            shift
        ;;
        *)
            # other choice
            echo "eee : unknown option ${1}"
            echo "${usage}"
            exit 1
        ;;
    esac
    # next flag
    shift
done
unset usage
#
# check for mailbodyfile
if [ ! -f ${mailbodyfile} ]
then
    echo "eee : ${mailbodyfile} not found"
    exit 1
fi
#
# check for xmloutput
#++ err si exist
#
echo "<user>" > ${xmloutput}
echo "<!-- mailbodyfile : ${mailbodyfile} " >> ${xmloutput}
cat ${mailbodyfile} >> ${xmloutput}
echo "-->" >> ${xmloutput}
echo "<!-- ${log_date} -->" >> ${xmloutput}
author_id=$(grep "author_id=" ${mailbodyfile} | awk -F"=" '{print $2}')
surname=$(grep "personal_surname=" ${mailbodyfile} | awk -F"=" '{print $2}')
firstname=$(grep "personal_firstname=" ${mailbodyfile} | awk -F"=" '{print $2}')
middlename=$(grep "personal_middlename=" ${mailbodyfile} | awk -F"=" '{print $2}')
if [ "${author_id}" = "template" ]
then
    author_id=$( echo ${surname} | \
    tr "[:lower:]" "[:upper:]" | \
    tr " " "_"  | \
    recode -d -f UTF-8..flat)
    author_id=${author_id}_$( echo ${firstname:0:1} | \
    tr "[:upper:]" "[:lower:]" | \
    recode -d -f UTF-8..flat)
fi
echo "<userid>${author_id}</userid>" >> ${xmloutput}
#
echo "<personname>" >> ${xmloutput}
#
echo "<surname>${surname}</surname>" >> ${xmloutput}
#
echo "<firstname>${firstname}</firstname>" >> ${xmloutput}
echo "<othername role='mi'>${middlename}</othername>" >> ${xmloutput}
#
echo "</personname>" >> ${xmloutput}
#
email=$(grep "personal_email=" ${mailbodyfile} | awk -F"=" '{print $2}')
echo "<email>${email}</email>" >> ${xmloutput}
#
elements=components
element=component
#++ récupérer les users/user[child::userid='template']/components/component/name
unset jlist
unset jlist_name
j=1
jlist[${j}]="OPA"
jlist_name[${j}]="OPA"
j=$((${j} + 1))
jlist[${j}]="LIM"
jlist_name[${j}]="LIM"
j=$((${j} + 1))
jlist[${j}]="TOP"
jlist_name[${j}]="TOP"
j=$((${j} + 1))
jlist[${j}]="TAM"
jlist_name[${j}]="TAM"
j=$((${j} + 1))
jlist[${j}]="SAXO"
jlist_name[${j}]="SAXO"
j=$((${j} + 1))
jlist[${j}]="offline"
jlist_name[${j}]="Off-line"
j=$((${j} + 1))
jlist[${j}]="AGRIF"
jlist_name[${j}]="AGRIF"
echo "<${elements}>" >> ${xmloutput}
components_date=$(grep "components_date=" ${mailbodyfile} | awk -F"=" '{print $2}')
echo "<components_date>${components_date}</components_date>" >> ${xmloutput}
## find the element
j=1
jlistsize=${#jlist[@]}
while [ ${j} -le ${jlistsize} ]
do
    jlist_min=$(echo ${jlist[j]} | tr [:upper:] [:lower:])
    # recherche de ${jlist[j]}
    grep -q "${element}_${jlist_min}" ${mailbodyfile}
    ok=${?}
    if [ ${ok} -eq 0 ]
    then
        echo "<${element} code='${jlist_min}'>" >> ${xmloutput}
        echo "<name>${jlist_name[j]}</name>" >> ${xmloutput}
        echo "<value>yes</value>" >> ${xmloutput}
        echo "</${element}>"  >> ${xmloutput}
    else
        echo "<${element} code='${jlist_min}'>" >> ${xmloutput}
        echo "<name>${jlist_name[j]}</name>" >> ${xmloutput}
        echo "<value>no</value>" >> ${xmloutput}
        echo "</${element}>"  >> ${xmloutput}
    fi
    j=$(( ${j} + 1 ))
done
echo "</${elements}>" >> ${xmloutput}
#
element=platform
elements=platforms
#++ récupérer les users/user[child::userid='template']/platforms/platform/name
unset jlist
unset jlist_name
j=1
jlist[${j}]="CRAY"
jlist_name[${j}]="CRAY"
j=$((${j} + 1))
jlist[${j}]="IBM"
jlist_name[${j}]="IBM"
j=$((${j} + 1))
jlist[${j}]="FUJITSU"
jlist_name[${j}]="FUJITSU"
j=$((${j} + 1))
jlist[${j}]="MAC"
jlist_name[${j}]="MAC"
j=$((${j} + 1))
jlist[${j}]="NEC"
jlist_name[${j}]="NEC"
j=$((${j} + 1))
jlist[${j}]="SGI"
jlist_name[${j}]="SGI"
j=$((${j} + 1))
jlist[${j}]="SUN"
jlist_name[${j}]="SUN"
j=$((${j} + 1))
jlist[${j}]='pccluster'
jlist_name[${j}]="PC Cluster"
echo "<${elements}>" >> ${xmloutput}
## find the element
j=1
jlistsize=${#jlist[@]}
while [ ${j} -le ${jlistsize} ]
do
    # recherche de ${jlist[j]}
    jlist_min=$(echo ${jlist[j]} | tr [:upper:] [:lower:])
    grep -q "${element}_${jlist_min}" ${mailbodyfile}
    ok=${?}
    if [ ${ok} -eq 0 ]
    then
        echo "<${element} code='${jlist_min}'>" >> ${xmloutput}
        echo "<name>${jlist_name[j]}</name>" >> ${xmloutput}
        echo "<value>yes</value>" >> ${xmloutput}
        echo "</${element}>"  >> ${xmloutput}
    else
        echo "<${element} code='${jlist_min}'>" >> ${xmloutput}
        echo "<name>${jlist_name[j]}</name>" >> ${xmloutput}
        echo "<value>no</value>" >> ${xmloutput}
        echo "</${element}>"  >> ${xmloutput}
    fi
    j=$(( ${j} + 1 ))
done
#
# gestion des "others"
j=1
jlistsize=${#jlist[@]}
grep_cmd="grep ${element}_zzz_other ${mailbodyfile} "
while [ ${j} -le ${jlistsize} ]
do
    grep_cmd="${grep_cmd} | grep -v ${element}=${jlist[j]}" #++ -v inutile
    j=$(( ${j} + 1 ))
done
other=$(eval ${grep_cmd} | awk -F "=" '{print$2}')
echo "<${element} code='zzz_other'>" >> ${xmloutput}
echo "<name>Other</name>" >> ${xmloutput}
echo "<value>${other}</value>" >> ${xmloutput}
echo "</${element}>"  >> ${xmloutput}
echo "</${elements}>" >> ${xmloutput}
#
processors=$(grep "processors=" ${mailbodyfile} | awk -F"=" '{print $2}')
echo "<processors>${processors}</processors>" >> ${xmloutput}
#
element=compiler
elements=compilers
unset jlist
unset jlist_name
j=1
jlist[${j}]="g95"
jlist_name[${j}]="g95"
j=$((${j} + 1))
jlist[${j}]="ifort"
jlist_name[${j}]="ifort"
j=$((${j} + 1))
jlist[${j}]="pgf"
jlist_name[${j}]="pgf"
j=$((${j} + 1))
jlist[${j}]="sxf90"
jlist_name[${j}]="sxf90"
j=$((${j} + 1))
jlist[${j}]="xlf"
jlist_name[${j}]="xlf"
echo "<${elements}>" >> ${xmloutput}
## find the element
j=1
jlistsize=${#jlist[@]}
while [ ${j} -le ${jlistsize} ]
do
    # recherche de ${jlist[j]}
    jlist_min=$(echo ${jlist[j]} | tr [:upper:] [:lower:])
    grep -q "${element}_${jlist_min}" ${mailbodyfile}
    ok=${?}
    if [ ${ok} -eq 0 ]
    then
        echo "<${element} code='${jlist_min}'>" >> ${xmloutput}
        echo "<name>${jlist_name[j]}</name>" >> ${xmloutput}
        echo "<value>yes</value>" >> ${xmloutput}
        echo "</${element}>"  >> ${xmloutput}
    else
        echo "<${element} code='${jlist_min}'>" >> ${xmloutput}
        echo "<name>${jlist_name[j]}</name>" >> ${xmloutput}
        echo "<value>no</value>" >> ${xmloutput}
        echo "</${element}>"  >> ${xmloutput}
    fi
    j=$(( ${j} + 1 ))
done
#
# gestion des "others"
j=1
jlistsize=${#jlist[@]}
grep_cmd="grep ${element}_zzz_other= ${mailbodyfile} "
while [ ${j} -le ${jlistsize} ]
do
    grep_cmd="${grep_cmd} | grep -v ${element}=${jlist[j]}" #++ -v inutile
    j=$(( ${j} + 1 ))
done
other=$(eval ${grep_cmd} | awk -F "=" '{print $2}')
echo "<${element} code='zzz_other'>" >> ${xmloutput}
echo "<name>Other</name>" >> ${xmloutput}
echo "<value>${other}</value>" >> ${xmloutput}
echo "</${element}>"  >> ${xmloutput}
echo "</${elements}>" >> ${xmloutput}
#
echo "</user>" >> ${xmloutput}
#
xmloutputfull=/tmp/${xmloutput} # ++
echo '<?xml version="1.0" encoding="iso-8859-1"?>' > ${xmloutputfull}
echo '<!DOCTYPE users SYSTEM "user.dtd">' >> ${xmloutputfull}
echo '<users>' >> ${xmloutputfull}
echo '<date>bidon</date>' >> ${xmloutputfull}
cat ${xmloutput} >> ${xmloutputfull}
echo '</users>' >> ${xmloutputfull}
#
# ++ parce que je ne sais pas dire où est la dtd dans la commande xmllint
cp user.dtd /tmp/
xmllint --noout --valid ${xmloutputfull} 1>> ${log} 2>> ${log}
status=${?}
if [ ${status} -ne 0 ]
then
    echo " eee : pb DTD conformance of ${xmloutputfull}"
    echo " eee : see ${log}"
    exit 1
else
    echo " iii : you can include ${xmloutput} in user.xml" #++ filename path
    echo " iii : modify date in user.xml" #++ filename path
fi
#
exit 0
