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

echo "generating complete file"

echo "creating folders"
if [ ! -d ../output/images ]; then
  mkdir ../output/images
fi

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

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

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

if [ ! -d temp ]; then
  rm -rf tmp
fi
mkdir tmp
touch tmp/content.textile
touch tmp/summary

# echo "" > tmp/summary

echo "reading content"
 #                  _             _
 #                 | |           | |
 #   ___ ___  _ __ | |_ ___ _ __ | |_
 #  / __/ _ \| '_ \| __/ _ \ '_ \| __|
 # | (_| (_) | | | | ||  __/ | | | |_
 #  \___\___/|_| |_|\__\___|_| |_|\__|
echo "<section id='main'>" >> tmp/content.textile
for i in ../content/*; do
  # if folder if not sys if not modele
  if [ -d "$i" ] && [[ ! "$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 "  $i"

      # 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 tmp/content.textile
      echo "<div id='$u' class='user'>" >> tmp/content.textile
      # add user content to main content
      echo "<section class='content'>"$'\n' >> tmp/content.textile
      # add content to main tmp/content.textile
      cat "$i/readme.textile" >> tmp/content.textile
      echo $'\n'"</section>" >> tmp/content.textile

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

      #   _
      #  (_)
      #   _ _ __ ___   __ _  __ _  ___  ___
      #  | | '_ ` _ \ / _` |/ _` |/ _ \/ __|
      #  | | | | | | | (_| | (_| |  __/\__ \
      #  |_|_| |_| |_|\__,_|\__, |\___||___/
      #                      __/ |
      #                     |___/
      echo "    images"
      images=""
      l=0
      echo "<section class='images'>"$'\n' >> tmp/content.textile
      for img in $i/images/*; do
        # echo "images : $img"
        if [ -f "$img" ] && [[ ! "$img" =~ ".db" ]] && [[ ! "$img" =~ ".avi" ]]; 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
          # convert thumbs
          convert ../output/images/$fimg -resize 400x400\>  ../output/thumbs/$fimg
          # replace image name in main tmp/content.textile file
          # pandoc can't render textile links
          sed -i "s/!images\/$fn!/<a data-lightbox='$folder' href='images\/$fimg'>!thumbs\/$fimg!<\/a>/g" tmp/content.textile
          # sed -i "s/!images\/$fn!/thumbs\/$fimg/g" tmp/content.textile


          # find "tmp/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  "* <a data-lightbox='$folder' href='images/$fimg'>!thumbs/$fimg!</a>" >> tmp/content.textile
          fi
        fi
      done
      echo $'\n'"</section>" >> tmp/content.textile


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

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

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

 #  _                    _
 # | |                  | |
 # | |__   ___  __ _  __| | ___ _ __
 # | '_ \ / _ \/ _` |/ _` |/ _ \ '__|
 # | | | |  __/ (_| | (_| |  __/ |
 # |_| |_|\___|\__,_|\__,_|\___|_|
echo "header"
touch tmp/header
echo "<header id='header'>"$'\n' > tmp/header
echo "h1. OLA#4 Documentation : OSP html2print"$'\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 - tmp/content.textile > tmp/out && mv tmp/out tmp/content.textile

echo "Pandoc"
pandoc -s \
  -f textile \
  -t html5 -o ../output/index.html \
  -H style.tpl.html \
  -A script.tpl.html \
  tmp/content.textile

  # -H css/main.css \

cp -r css ../output/
cp -r fonts ../output/
cp -r bower_components ../output/


# 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 bower_components/lightbox2/dist/css/lightbox.min.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
