#!/bin/bash


  SRC=all4dev.mdsh
  OUTDIR=grafts
  MDSH2HTML="./150905_mkhtml.sh"
  ALLEPNAMESURL="http://pipelines.local/etherdump/allpads.txt"
  EPBASE="http://pipelines.local:9001/p"
  EPDUMPBASE="http://pipelines.local/etherdump"

# COMMON NAME PATTERN FOR TMP FILES (EASIFY CLEAN UP)
# ----------------------------------------------------------------- #
  TMPDIR=. ; TMPID=$TMPDIR/`date +%Y%m%d``echo $RANDOM | cut -c 1-3`

# ----------------------------------------------------------------- #
# DOWNLOAD THINGS
# ----------------------------------------------------------------- #
  wget --no-check-certificate \
        -O ${TMPID}.epnames $ALLEPNAMESURL > /dev/null 2>&1


# ----------------------------------------------------------------- #
# GET A COMBINED DUMP OF ALL PADS
# ----------------------------------------------------------------- #
  for EPNAME in `cat ${TMPID}.epnames`
   do 
      EPLINK="$EPBASE/$EPNAME"
      EPEXPORTURL=$EPBASE/$EPNAME/export/txt
      wget -O - -o /dev/null $EPEXPORTURL              | # DOWNLOAD
      sed '$s/$/\n\n/g'                                | # ENSURE EMPTY LAST LINE
      sed "s,^[ \t]*$,% \[_${EPNAME}_\]($EPLINK)\n\n,g" | # ADD LINK AT EACH END OF PARAGRAPH
      sed '/^% GRAFT:/s/$/  :::/g'                     | # MARK END OF LINE
      tee  >> ${TMPID}.epall                             # WRITE FILE
  done

# ----------------------------------------------------------------- #
# SEPARATE AND PROCESS GRAFTS (INDEXED PARAGRAPHS)
# ----------------------------------------------------------------- #
# TODO:
# keywords.last <-> keywords.current
# running multiple times for same scion??

  SRC=${TMPID}*.epall

  for GRAFT in `grep "^% GRAFT:" $SRC  | #
                sort -u | sed 's/ /XxYcv/g'`
   do
    for SCION in `echo $GRAFT     | #
                  cut -d ":" -f 2 | #
                  sed 's/,/ /g'`
     do

     GRAFT=`echo $GRAFT     |  # 
            sed 's/XxYcv/ /g'` #  
     SCION=`echo $SCION     |  #
            sed 's/XxYcv/ /g'` #
     if [ `echo $SCION | wc -c` -gt 1 ]
     then
         OUTPUT=`echo $SCION        | #
                 tr [:upper:] [:lower:] | #
                 sed 's/ /_/g'          | #
                 sed 's/[^a-z_]//g'     | #
                 iconv -f utf-8 -t ascii//translit` #
         OUTPUT=$OUTDIR/${OUTPUT}.txt
         HTML=_/graft/${OUTPUT}.html

         SCION=`echo $SCION | sed 's/,[ \t]*//g'`

         echo "% GRAFT: $SCION"
         GRAFT="% GRAFT:.*${SCION}.* :::"

#        sed "s/^[ \t]*$/\n/" $SRC    | # SECURITY LINEBREAK
#        sed -e "/./{H;$!d;}" \
#            -e "x;/$GRAFT/!d;"       | # SELECT PARAGRAPH
#        sed 's/^% GRAFT:.*:::$/&XXX\n/' | # ADD LINEBREAK AFTER GRAFT COMMAND
#        sed -e '/./{H;$!d;}' -e 'x;/% GRAFT:/d;' | #
#        tee > $OUTPUT

         echo "% SCION: $SCION"       > $OUTPUT

         sed "s/^[ \t]*$/\n/" $SRC    | # SECURITY LINEBREAK
         sed -e "/./{H;$!d;}" \
             -e "x;/$GRAFT/!d;"       | # SELECT PARAGRAPH
         sed 's/^% GRAFT:.*:::$/&\n/' | # ADD LINEBREAK AFTER GRAFT COMMAND
         sed -e '/./{H;$!d;}' -e 'x;/% GRAFT:/d;' | #
         tee >> $OUTPUT

         EHREF=`cat $OUTPUT     | #
                tail -n 1       | #
                cut -d "(" -f 2 | #
                cut -d ")" -f 1`
         EHREF="% FROM: $EHREF"
       # echo $EHREF

         sed -i "1i$EHREF" $OUTPUT

         $MDSH2HTML $OUTPUT _/graft # > /dev/null 2>&1

       # LINKIFY HTTP
       # sed -i 's,http://[^ ]*,<a href="&">&</a>,g' $HTML

      fi
    done
  done

  echo "----------------------------------------------------"
# ----------------------------------------------------------------- #
# SEPARATE AND PROCESS BINDS (COMBINATION OF GRAFTS)
# ----------------------------------------------------------------- #

  for BIND in `grep "^% BIND:" $SRC  | #
               sort -u | sed 's/ /XxYcv/g'`
   do
      BIND=`echo $BIND | sed 's/XxYcv/ /g'`
      echo $BIND
      GREP="-e 'x;/"
      OUTPUT=`echo $BIND             | #
              cut -d ":" -f 2        | #
              sed 's/^[ \t]*//g'     | # 
              sed 's/,[ \t]*/,/g'    | #
              tr [:upper:] [:lower:] | #
              sed 's/ /_/g'          | #
              sed 's/[^a-z_,]//g'    | #
              sed 's/,/+/g'          | #
              iconv -f utf-8 -t ascii//translit` #
      OUTPUT=$OUTDIR/${OUTPUT}.txt
    # echo $OUTPUT

      for SCION in `echo $BIND        | #
                    cut -d ":" -f 2   | #
                    sed 's/ /XxYcv/g' | #
                    sed 's/,[ \t]*/ /g'`
       do
          SCION=`echo $SCION       | #
                 sed 's/XxYcv/ /g' | #
                 sed 's/^[ \t]*//'`
          GREP="${GREP}% GRAFT:.*${SCION}.* :::/b' -e '/"
      done

      SEDX=`echo "sed -e '/./{H;$!d;}' $GREP" | #
            sed "s/'\/$/d/"`

      sed "s/^[ \t]*$/\n/" $SRC                | # SECURITY LINEBREAK
      eval "$SEDX"                             | # EXECUTE GENERATED SED COMMAND
      sed 's/^% GRAFT:.*:::$/&XXX\n/'          | # ADD LINEBREAK AFTER GRAFT COMMAND
      sed -e '/./{H;$!d;}' -e 'x;/% GRAFT:/d;' | # DO NOT PRINT % GRAFT
      tee > $OUTPUT

      $MDSH2HTML $OUTPUT _/bind > /dev/null 2>&1

  done


# ----------------------------------------------------------------- #


# CLEAN UP (PREFER NO SUPER WILDCARD RM)
# ----------------------------------------------------------------- #
  rm ${TMPID}*.epnames
  rm ${TMPID}*.epall


exit 0;

