#!/bin/bash

# THIS IS A FRAGILE SYSTEM, HANDLE WITH CARE.                                #
# --------------------------------------------------------------------------- #

  PADBASE="http://pipelines.local:9001"
  OUTDIR=$2
  MAIN="$1"; if [ `echo $MAIN | wc -c` -lt 2 ]; then
                   echo "please provide input file"; 
                   exit 0;fi
  HTML=$OUTDIR/`echo "$MAIN"            | #
                sed 's,/export/txt$,X,g'| #
                rev                     | #
                cut -d "/" -f 1 | rev   | #
                cut -d "." -f 1`.html
  TMPDIR=. ; TMPID=XYZ ; export $TMPID
  SRCDUMP=$TMPDIR/${TMPID}.`echo $RANDOM | cut -c 1-8`.dump
  if [ -f $SRCDUMP ]; then rm $SRCDUMP ;
  echo "deleting"; fi


  REFURL="http://pipelines.local:9001/p/references.bib/export/txt"
  wget --no-check-certificate \
        -O ${TMPID}bib.bib $REFURL > /dev/null 2>&1

  echo "converting $MAIN"

# =========================================================================== #
# CONFIGURATION                                                               #
# --------------------------------------------------------------------------- #
  FUNCTIONSBASIC=000002_basic.functions
   FUNCTIONSPLUS=150905_html.functions
       FUNCTIONS=$TMPDIR/$TMPID.functions.tmp
  cat $FUNCTIONSBASIC $FUNCTIONSPLUS > $FUNCTIONS
  source $FUNCTIONS


# --------------------------------------------------------------------------- #
  PANDOCACTION="pandoc --ascii -S -r markdown -w html"
# --------------------------------------------------------------------------- #
# FOOTNOTES
# \footnote{the end is near, the text is here}
# --------------------------------------------------------------------------- #
  FOOTNOTEOPEN="FOOTNOTEOPEN$RANDOM{" ; FOOTNOTECLOSE="}FOOTNOTECLOSE$RANDOM"
# CITATIONS
# \cite{phillips:2004:vectoraesthetic}
# --------------------------------------------------------------------------- #
  CITEOPEN="CITEOPEN$RANDOM" ; CITECLOSE="CITECLOSE$RANDOM"
# \cite[1-8]{phillips:2004:vectoraesthetic}
# --------------------------------------------------------------------------- #
  CITEPOPEN="${CITEOPEN}[" ; CITEPCLOSE="]"
# =========================================================================== #


# --------------------------------------------------------------------------- #
# ACTION HAPPENS HERE!
# --------------------------------------------------------------------------- #

  mdsh2src $MAIN

  if [ -f $SRCDUMP ]; then

# --------------------------------------------------------------------------- #
# EDIT RAW/PANDOC HTML
# --------------------------------------------------------------------------- #
  BREAKFO0=B`echo $RANDOM | cut -c 1-2`R
  BREAKFO1=N`echo $RANDOM | cut -c 1-2`L
# REMOVE NEWLINES (EASIFY PARSING)
  sed -i ":a;N;\$!ba;s/\n/$BREAKFO1/g"                               $SRCDUMP
# REMOVE EMPTY PARAGRAPHS
  sed -i "s/>[$BREAKFO1]*[ ]*</></g"                                 $SRCDUMP
  sed -i "s/<p>[$BREAKFOO]*<\/p>//g"                                 $SRCDUMP
# RESTORE LINEBREAKS
  sed -i "s/$BREAKFO0/\n/g"                                          $SRCDUMP
  sed -i "s/$BREAKFO1/\n/g"                                          $SRCDUMP

# CITATIONS TO FOOTNOTES
  sed -i "s/$CITEOPEN/${FOOTNOTEOPEN}CITE:/g"                        $SRCDUMP
  sed -i "s/$CITECLOSE/\n$FOOTNOTECLOSE/g"                           $SRCDUMP


# --------------------------------------------------------------------------- #
# MAKE CITATIONS
# --------------------------------------------------------------------------- #
  sed -i '/^%/ d' ${TMPID}bib.bib
  bibtex2html ${TMPID}bib.bib
  sed ":a;N;\$!ba;s/\n//g" ${TMPID}bib.html | #
  sed 's/<\/tr>/\n<\/tr>/g'                 | #
  sed 's/<tr>/\n<tr>/g' > ${TMPID}bibmod.html #

  for CITATION in `sed  "s/${FOOTNOTEOPEN}CITE:/\n&/g" $SRCDUMP | #
                   grep -n "^${FOOTNOTEOPEN}CITE:"`
   do
      CITEREF=`echo $CITATION   | #
               cut -d ":" -f 3- | #
               cut -d "]" -f 2-`
      CITATION=`echo $CITATION      | #
                sed 's/&/\\\\&/g'   | #
                sed 's/\\//\\\\\//g'| #
                sed 's/]/\\\\]/g'   | #
                sed 's/\[/\\\\\[/g' | #
                cut -d ":" -f 2-`
      CITETEXT=`grep $CITEREF ${TMPID}bibmod.html | #
                sed 's/<td/\n/g'          | #
                sed 's/\[\&nbsp;/\n/g'    | #
                grep 'class="bibtexitem"' | #
                cut -d ">" -f 2-          | #
                sed 's/&/\\\\&/g'         | #
                sed 's/\\//\\\\\//g'      | #
                sed 's/]/\\\\]/g'         | #
                sed 's/\[/\\\\\[/g'`
      sed -i "s/$CITATION/${FOOTNOTEOPEN}$CITETEXT/" $SRCDUMP
  done


# --------------------------------------------------------------------------- #
# REMOVE ERRORS/CORRECT SPACES FOR HREF
# --------------------------------------------------------------------------- #
 #S=XYX${RANDOM}SP ; UN=YXY${RANDOM}UN
  TMB=X${RANDOM}X
 #sed -i  "s/$S/ /g"                  $SRCDUMP # RESTORE SPACEFOO
 #sed -i  "s/$UN//g"                  $SRCDUMP # RESTORE UNID
  sed -ir "s/<a[^>]*>[ ]*/ \n$TMB&/g" $SRCDUMP # MOVE TO NEWLINE AND MARK
  sed -i  "/^$TMB/s/> />/"            $SRCDUMP # REMOVE >SPACE IF MARKED
  sed -i -e :a \
      -e "$!N;s/\n$TMB//;ta" \
      -e 'P;D'                        $SRCDUMP # APPEND IF MARKED
  sed -i  's/[ ]*<\/a>/<\/a> /g'      $SRCDUMP # CORRECT SPACE
  sed -i  's/,<\/a>/<\/a>,/g'         $SRCDUMP # CORRECT ,
  sed -i  's/\.<\/a>/<\/a>\./g'       $SRCDUMP # CORRECT .
  sed -i  's/:<\/a>/<\/a>:/g'         $SRCDUMP # CORRECT :
  sed -ir "s/> </></g"                $SRCDUMP # MOVE TO NEWLINE AND MARK

# --------------------------------------------------------------------------- #
# MAKE FOOTNOTES
# --------------------------------------------------------------------------- #
  echo "<hr/>"                              >> $SRCDUMP
  echo "<ol class=\"footnotes\">"           >> $SRCDUMP
  COUNT=1
  for FOOTNOTE in `sed "s/$FOOTNOTEOPEN/\n&/g" $SRCDUMP | #
                   sed 's/ /5P4C3XX/g'                  | #
                   sed "s/$FOOTNOTECLOSE/&\n/"          | # 
                   grep "^$FOOTNOTEOPEN"`
   do
      ID=`echo $COUNT | md5sum | cut -c 1-8`
      FOOTNOTETXT=`echo $FOOTNOTE    | #
                   cut -d "{" -f 2   | #
                   cut -d "}" -f 1   | #
                   sed 's/5P4C3XX/ /g'`
      FOOTNOTE=`echo $FOOTNOTE       | #
                sed 's/5P4C3XX/ /g'  | #
                sed 's/\[/\\\[/g'    | #
                sed 's/|/\\|/g'`
      OLDFOOTNOTE=$FOOTNOTE
      LINE=`grep -n "$OLDFOOTNOTE" $SRCDUMP | #
            head -n 1 | cut -d ":" -f 1`
      NEWFOOTNOTE="<sup><a href=\"#$ID\">$COUNT</a><\/sup>"
      sed -i "${LINE}s|$OLDFOOTNOTE|$NEWFOOTNOTE|g" $SRCDUMP
      echo "<li id=\"$ID\"> $FOOTNOTETXT </li>" >>  $SRCDUMP
      COUNT=`expr $COUNT + 1`
  done
  echo "</ol>"           >> $SRCDUMP

  sed -i "s|$FOOTNOTECLOSE||g" $SRCDUMP # WORKAROUND (BUG!!)

# --------------------------------------------------------------------------- #
# WRITE HTML
# echo "<html><body>"    >  $HTML
# cat $SRCDUMP           >> $HTML
# echo "</body></html>"  >> $HTML
  cat $SRCDUMP           >  $HTML

  fi

# =========================================================================== #
# CLEAN UP
  if [ `ls ${TMPID}.* | wc -l` -gt 0 ]; then rm ${TMPID}* ; fi



exit 0;

