#!/bin/bash


  OUTDIR=grafts
  ALLEPNAMESURL="http://pipelines.local/etherdump/allpads.txt"
  EPBASE="http://pipelines.local:9001/p"

# 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
     SCION=`echo $SCION     |  #
            sed 's/XxYcv/ /g'` #
     if [ `echo $SCION | wc -c` -gt 1 ]
     then
           echo "SCION: $SCION"
      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
  done


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


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


exit 0;

