#!/bin/bash -p
#
# momdoc v1.2, based on /vobs/cello/emas/tools/momdoc
# converts MOM from xml to html format
#
# syntax: momdoc <xml file> <destination directory>
# A bunch of html files (one for each MO,struct,enum,exception) are created and put in the output directory
#
# Finn Magnusson 2013-08-04

###########################################################################################
moshelldir=`dirname "$0"`
if [[ $moshelldir != /* ]] ; then moshelldir=`pwd`/$moshelldir ; fi
vobsinstallation=0
unamea=$(uname -a)
gawkext=""
gawklib=""
if [[ $unamea = [Ll][iI][nN][uU][xX]*x86_64* ]] ; then gawklib="lin64"   ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".lin64" ; fi
elif [[ $unamea = [Ll][iI][nN][uU][xX]* ]]      ; then gawklib="linux"   ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".linux" ; fi
elif [[ $unamea = SunOS*sparc* ]]               ; then gawklib="solaris" ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".solaris" ; fi
elif [[ $unamea = SunOS* ]]                     ; then gawklib="sol86"   ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".sol86" ; fi
else gawklib="cygwin" ; gawkext=".exe"
fi
gawkprog="gawk${gawkext}" 
gawk="$moshelldir/$gawkprog"
#special case where moshell has been installed on a linux 32 bit machine but should be run on linux 64 sharing the same file system
if [[ $unamea = [Ll][iI][nN][uU][xX]*x86_64* && $vobsinstallation != 1 && `file "$moshelldir/gawk"` = *32-bit* ]] ; then gawklib="linux" ; fi
filefunc="$moshelldir/commonjars/lib/${gawklib}/filefuncs"
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}$moshelldir/commonjars/lib/${gawklib}
export LANG=C
export LC_ALL=C

common=$moshelldir/commonjars
bsf=$common/bsf.jar
xerces=$common/xerces.jar
xalan=$common/xalan.jar
dev_null=/dev/null
###########################################################################################

function parse_varfile()
{
$gawk ' { gsub("\r",""); if ($1 ~ /^java=/) java=gensub(/^.*=|#.*$/,"","g",$1) };END{printf java} ' $1
}
#Path to java read from moshell file and/or moshellrc file
tempvar=$(parse_varfile $moshelldir/moshell) ; if [[ -n $tempvar ]] ; then java=$(eval echo $tempvar) ; fi
tempvar=$(parse_varfile $HOME/.moshellrc) ; if [[ -n $tempvar ]] ; then java=$(eval echo $tempvar) ; fi 


case $# in 
 2) xmlfile=$1 ; destdir=$2 
	case $(uname) in 
	 *[cC][yY][gG]*) if [[ $xmlfile == /* || $destdir == /* ]] ; then echo "Absolute pathes not supported in momdoc on cygwin. Please use relative pathes instead. Exiting..." ; exit 1 ; fi ;;
	esac
    ;;
 *) echo "Syntax: `basename $0` <xml file> <destination directory>" 
    echo "Purpose: To convert a MOM from xml to html format"
    exit 1;;
esac

#if ! test -w $common ; then 
#  echo "You need to have write permission to the directory $common. "
#  echo "You cannot run this program without write permission ."
#  echo "Exiting..." ; exit 1
#fi

missing_jar_files=""
if ! test -f $bsf ; then missing_jar_files="bsf.jar" ; fi
if ! test -f $xerces ; then missing_jar_files="$missing_jar_files xerces.jar" ; fi
if ! test -f $xalan  ; then missing_jar_files="$missing_jar_files xalan.jar"  ; fi
if [[ $missing_jar_files != "" ]] ; then
	echo "!! Following jar files are missing from the moshell/commonjars directory: $missing_jar_files !!"
	echo "Please copy them into the moshell/commonjars directory before running the momdoc script."
	echo "These files can be found in an older moshell version (5.1 or lower) "
	echo "or can be downloaded from http://newtran01.au.ao.ericsson.se/moshell/momdoc_jar_files.zip"
	exit 1
fi

if ! test -f $xmlfile ; then
    echo "Cannot find xml file. Exiting..." 
    exit 1
fi

if test -d $destdir ; then
  if test `ls $destdir | wc -l` -gt 0 ; then echo "Destination directory is not empty. Exiting..." ; exit 1 ; fi
  if ! test -w $destdir ; then echo "You do not have write permission to destination directory ( $destdir ). Exiting..." ; exit 1; fi
else
  echo "Destination directory does not exist. Creating it..."
  if ! mkdir -p $destdir ; then echo "Cannot create destination directory. Exiting..." ; exit 1 ; fi
fi

cp $xmlfile $destdir
xmlfile=$destdir/`basename $xmlfile`
cp $common/mp.dtd $destdir
cp $common/mom2html.xsl $destdir
cp $common/*.htm $destdir

classpath=$bsf":"$xerces":"$xalan
case $(uname) in 
 *[cC][yY][gG]*) 
 	classpath=$(cygpath -m $xalan)";"$(cygpath -m $xerces)";"$(cygpath -m $bsf)
 	dev_null=$(cygpath -m $dev_null)
 	#destdir=$(cygpath -m $destdir)
 	#xmlfile=$(cygpath -m $xmlfile)
 	;;
esac

echo ""
echo "generating html files..."
#echo $java -classpath $classpath org.apache.xalan.xslt.Process -in $xmlfile -xsl $destdir/mom2html.xsl -out $dev_null  
$java -classpath $classpath org.apache.xalan.xslt.Process -in $xmlfile -xsl $destdir/mom2html.xsl -out $dev_null  

if test $? -eq 0 ; then
#mv $common/*.html $destdir
echo "Done!"
echo "Start Netscape or Explorer and open the file $destdir/index.htm to view MOM."
else
echo "Failed" ; exit 1
fi



