#!/bin/bash
# 06-07-2016
# Licence : GPLv3


if [ ! -d ../output/images ]; then
  mkdir ../output/images
fi

if [ ! -d ../output/sources ]; then
  mkdir ../output/sources
fi

if [ -f content.textile ]; then
  rm content.textile
fi
touch content.textile

echo "" > /tmp/summary

 #                  _             _
 #                 | |           | |
 #   ___ ___  _ __ | |_ ___ _ __ | |_
 #  / __/ _ \| '_ \| __/ _ \ '_ \| __|
 # | (_| (_) | | | | ||  __/ | | | |_
 #  \___\___/|_| |_|\__\___|_| |_|\__|
echo "<section id='main'>" >> content.textile
for i in ../content/*; do
  # if folder if not sys if not modele
  if [ -d "$i" ]; then # && [ "$i" != "../model" ]; then
    # if readme exists
    if [ -f "$i/readme.textile" ]; then
      # user name
      # record readme content on variable
      cont=$(cat "$i/readme.textile")

      # echo "$cont"
      # user=$(grep "h1\.\s*.+" "$i/readme.textile")
      # user="${cont% h1}"
      # user=$(expr "$cont" : "h1. \(.*\)$")
      # echo "user : $user"

      u=${i/..\//}
      # record link for menu
      # echo "<li><a href='#$u'>$u</a></li>" >> /tmp/summary
      echo "* \"$u\":#$u" >> /tmp/summary

      # right html and contents on main content.textile
      echo "<div id='$u' class='user'>" >> content.textile
      # add user content to main content
      echo "<section class='content'>"$'\n' >> content.textile
      # add content to main content.textile
      cat "$i/readme.textile" >> content.textile
      echo $'\n'"</section>" >> content.textile

      # get folder name
      folder=$(basename "$i")

      #   _
      #  (_)
      #   _ _ __ ___   __ _  __ _  ___  ___
      #  | | '_ ` _ \ / _` |/ _` |/ _ \/ __|
      #  | | | | | | | (_| | (_| |  __/\__ \
      #  |_|_| |_| |_|\__,_|\__, |\___||___/
      #                      __/ |
      #                     |___/
      images=""
      l=0
      echo "<section class='images'>"$'\n' >> content.textile
      for img in $i/images/*; do
        # echo "images : $img"
        if [ -f "$img" ]; then
          # record file name
          fn=$(basename "$img")
          counter=$(printf %02d $l)
          # record extension
          ext="${fn##*.}"
          # record new image name
          fimg="$folder-$counter.$ext"
          # copy image file with new name
          cp -f "$img" ../output/images/$fimg
          # replace image name in main content.textile file
          sed -i "s/images\/$fn/images\/$fimg/g" content.textile
          # find "content.textile" -print0 | xargs -0 sed -i '' -e "s/images\/$fn/images\/$fimg/g"
          l=$(($l+1))
          # if image is not on content ...
          if [[ ! "$cont" =~ "images/$fn" ]]; then
            # ... add it at end of content
            echo  "* !images/$fimg!" >> content.textile
          fi
        fi
      done
      echo $'\n'"</section>" >> content.textile


      #    _________  __  _______________  _____
      #   / ___/ __ \/ / / / ___/ ___/ _ \/ ___/
      #  (__  ) /_/ / /_/ / /  / /__/  __(__  )
      # /____/\____/\__,_/_/   \___/\___/____/
      #
      echo "<section class='sources'>"$'\n' >> content.textile
      for src in $i/sources/*; do
        # echo "patch : $src"
        if [ -f "$src" ]; then
          # record file name
          fn=$(basename "$src")
          cp -f "$src" ../output/sources/$fn
          # echo $'\r'"<li>" >> content.textile
          echo  "* \"$fn\":sources/$fn" >> content.textile
          # echo $'\r'"</li>" >> content.textile
        fi
      done
      echo $'\n'"</section>" >> content.textile

      echo "</div>" >> content.textile
      echo '<div class="pagebreak"></div>' >> content.textile
    fi

  fi
done
# end main section
echo "</section>" >> content.textile

 #  _                    _
 # | |                  | |
 # | |__   ___  __ _  __| | ___ _ __
 # | '_ \ / _ \/ _` |/ _` |/ _ \ '__|
 # | | | |  __/ (_| | (_| |  __/ |
 # |_| |_|\___|\__,_|\__,_|\___|_|
touch /tmp/header
echo "<header id='header'>"$'\n' > /tmp/header
echo "h1. OLA#3 Documentation"$'\n' >> /tmp/header
echo "<nav id='menu'>"$'\n' >> /tmp/header
cat /tmp/summary >> /tmp/header
echo $'\n'"</nav>" >> /tmp/header
echo "</header>" >> /tmp/header

cat /tmp/header|cat - content.textile > /tmp/out && mv /tmp/out content.textile


pandoc -s \
  -f textile \
  -t html5 -o ../output/index.html \
  -H css/main.css \
  -A script.tpl.html \
  content.textile

# echo "export markdown as html"
# pandoc -s \
#   -f markdown+hard_line_breaks+auto_identifiers+ascii_identifiers+tex_math_dollars+pipe_tables+all_symbols_escapable \
#   -t html5 -o index.html \
#   -c fonts/amiri/amiri.css \
#   -H css/styles.css \
#   -B body-base-top.tpl.html \
#   -A body-base-bot.tpl.html \
#   -A js/jquery.min.js \
#   -A js/script.js \
#   # -A js/css-regions-polyfill.min.js \
#   -A body-base-end.tpl.html \
#   pad.md
