#!/bin/bash
#
# Test on converting sourceforge.net wiki export #2
#
# 0. Check/install pre-requisites ...
#  - [jq](https://stedolan.github.io/jq/)
#  - [pandoc](http://pandoc.org/)
# sudo apt-get install jq pandoc

[ ! -x "`which jq`" ] && \
{ echo "`basename $0`: 'jq' executable not found."; exit 1; }

[ ! -x "`which pandoc`" ] && \
{ echo "`basename $0`: 'pandoc' executable not found."; exit 2; }

[ ! -d "wiki" ] && \
{ echo "`basename $0`: 'wiki' directory not found."; exit 3; }

[ ! -f "wiki.json" ] && \
{ echo "`basename $0`: 'wiki.json' file not found."; exit 4; }


# 1. Prepare wiki attachments images ...
[ ! -d "images" ] && \
{ mkdir images; \
  find wiki/ -name "*.png" -type f -exec cp -vp {} images/ \;; \
  find wiki/ -name "*.jpg" -type f -exec cp -vp {} images/ \;; }
[ ! -d "attachments" ] && \
{ mkdir attachments; \
  find wiki/ -name "*.zip" -type f -exec cp -vp {} attachments/ \;; \
  find wiki/ -name "*.mkv" -type f -exec cp -vp {} attachments/ \;; }


# 2. Produce a delimited text file bundle wiki_epub.csv ...
cat wiki.json | sed 's/^{"pages": //;s/}]}$/}]/' \
| jq '.[] | [ ._id, .title, .text ] | @csv' \
| sed 's/^"\\"/"/;s/\\""$/"/;s/\\",\\"/","/g' \
> wiki.csv


# 3. Produce primary markdown files (*.md.0) ...
cat wiki.csv | awk -F'","' '{
  sub("^\"", ""); sub("\"$", "");
  print $3 > $2 ".md.0";
}'


# 4. Convert to plain markdown files (*.md) ...
for x in *.md.0; do
  y=$(basename "$x" .md.0); cat "$x" \
  | sed -r 's/\\r\\n/\n/g;s/&nbsp;/ /g;s/\\"/"/g;s/["]+/"/g' \
  | sed -r 's/\[\[img src=["]*([^"]+)["]*.+alt=["]*([^"]+)["]*\]\]/![*\2*](images\/\1)\n/g' \
  | sed -r 's/\[\[img src=["]*([^"]+)["]*.*\]\]/![](images\/\1)\n/g' \
  | sed -r 's/\(.+\/([^\/]+\.zip)\)/(attachments\/\1)/g' \
  | sed -r 's/\(.+\/([^\/]+\.mkv)\)/(attachments\/\1)/g' \
  | sed -r 's/\[\[([^]]+)\]\]//g' \
  | sed -r 's/\(\/p\/[^\/]+\/wiki\/([^\/\)]+)\)/(\1)/g' \
  | sed -r 's/\(\/([^\)]+)\)/(https:\/\/sourceforge.net\/\1)/g' \
  | sed -r 's/\(([^\/]+)\/\)/(\1)/g' \
  | sed -r 's/^===/---/' \
  | sed -r 's/^([#]+)([^ #]+)/\1 \2/' \
  > "$y".md;
done


# 5. Convert secondary markdown files (*.md.1, 2, 3) ...
for x in *.md; do
  y=$(basename "$x" .md);
  # Secondary markdown files for epub/pdf (*.md.1, 2) ...
  echo "#=# $y" > "$y".md.1;
  echo >> "$y".md.1;
  cat "$x" >> "$y".md.1;
  cat "$y".md.1 \
  | sed -r 's/\[([^]<]+)\]\(([^#\/\)]+)\)/[\1][\2]/g' \
  | sed -r 's/\[([^]<]+)\]\(([^\/]+)\/#[0-9|\-]*([^\)]+)\)/[\1](#\3)/g' \
  | sed -r 's/\[([^]<]+)\]([^\(]+)/[\1]\2/g' \
  | sed -r 's/\[([^]<]+)\]$/[\1]\n/' \
  | sed -r 's/^#/##/;s/^##=#/#/' \
  | sed -r 's/\(attachments\/([^\)]+)\)/(https:\/\/qtractor\.org\/doc\/attachments\/\1)/g' \
  > "$y".md.2;
  # Secondary markdown files for html (*.md.3) ...
  cat "$x" \
  | sed -r 's/\[([^]<]+)\]\(([^#\/\)]+)\)/[\1](\2.html)/g' \
  | sed -r 's/\[([^]<]+)\]\(([^\/]+)\/#[0-9|\-]*([^\)]+)\)/[\1](\2.html#\3)/g' \
  | sed -r 's/\[([^]<]+)\]([^\(]+)/[\1](\1.html)\2/g' \
  | sed -r 's/\[([^]<]+)\]$/[\1](\1.html)\n/' \
  > "$y".md.3;
done


# 6. Convert selected secondary markdown files (*.md.2) to final epub ...
WIKI_TITLE="Qtractor Wiki"
WIKI_TNAME="Qtractor Manual & How-To's"
WIKI_FNAME="qtractor-manual-and-howtos"
WIKI_FUSER=$(whoami)
WIKI_STAMP=$(date +"%Y-%m-%d %H:00%z")
WIKI_URL="https://sourceforge.net/p/qtractor/wiki/Home"

cat <<__EoF1 > wiki_epub.txt
% ${WIKI_TNAME}
% [${WIKI_TITLE}](${WIKI_URL})
% ${WIKI_FUSER} _et al._ ${WIKI_STAMP}
__EoF1

cat <<__EoF2 > wiki_epub.css
pre, code {
  font-family: monospace;
  color: darkred;
}
__EoF2

pandoc -f markdown+smart -t epub \
  --css wiki_epub.css \
  -o "${WIKI_FNAME}".epub \
  wiki_epub.txt \
  Home.md.2 \
  Manual\ -\ Table\ of\ Contents.md.2 \
  Manual\ -\ [0-9]\ *.md.2 \
  Manual\ Draft\ -\ *.md.2 \
  How\ To\ -\ Contents.md.2 \
  How\ To\ -\ [0-9]\ *.md.2 \
  Unofficial\ *.md.2 \
  qtractor\ 0.*.md.2 \
  Qtractor\ User\ Manual*.md.2 \
  Resources.md.2



# 7a. Convert markdown to pdf (newer method)...
#
# zypper in python38-WeasyPrint
#
# python38-CairoSVG python38-Pyphen python38-cairocffi
# python38-cairocffi-pixbuf python38-cssselect2
# python38-pdfrw python38-tinycss2 python38-xcffib
#
cat <<__EoF3 > wiki_epdf.css
pre, code {
  font-family: monospace;
  color: darkred;
}
@page {
  margin: 2cm 2cm 2cm 2cm; 
  font-size: 12pt;
  @bottom-center {
    font-size: 10pt;
    color: gray;
    content: counter(page);
  }
} 
@page :first {
  @bottom-center { content: none; }
}
h1  { page-break-before: always; }
p   { text-align: justify; }
img { max-width: 100%; }
__EoF3

pandoc -f markdown+smart --pdf-engine=weasyprint \
  --css wiki_epdf.css \
  -o "${WIKI_FNAME}".pdf \
  wiki_epub.txt \
  Home.md.2 \
  Manual\ -\ Table\ of\ Contents.md.2 \
  Manual\ -\ [0-9]\ *.md.2 \
  Manual\ Draft\ -\ *.md.2 \
  How\ To\ -\ Contents.md.2 \
  How\ To\ -\ [0-9]\ *.md.2 \
  Unofficial\ *.md.2 \
  qtractor\ 0.*.md.2 \
  Qtractor\ User\ Manual*.md.2 \
  Resources.md.2

# 7b. Convert epub to pdf (older method) ...
#
#pandoc -f epub -t latex \
#  -V geometry:"top=2cm, bottom=2cm, left=2cm, right=2cm" \
#  -o "${WIKI_FNAME}".pdf "${WIKI_FNAME}".epub
#


# 8. Convert all secondary markdown files (*.md.3) to final html ...
cat <<__EoF4 > wiki_style.txt
<meta name="description" content="Qtractor is an Audio/MIDI multi-track sequencer application written in C++ with the Qt framework." />
<meta name="keywords" content="qtractor qt midi audio gui daw linux jack alsa sequencer" />
<meta name="author" content="rncbc aka Rui Nuno Capela" />
<link rel="shortcut icon" href="/qtractor.ico" />
<link rel="stylesheet" type="text/css" href="/style/qtractor.css" />
<style type="text/css">
 pre, img { margin-left: 2em; }
 li p { margin-left: 0; }
</style>
__EoF4

for x in *.md.3; do
  y=$(basename "$x" .md.3)
  cat <<__EoF5 > wiki_header.txt
  <div id="header">
   <h1><img src="/image/qtractor.png" align="right" />
   <big>Qtractor</big> <small>An Audio/MIDI multi-track sequencer</small></h1>
   <ul id="menu">
   <li><a href="/qtractor-index.html#Intro">Home</a>
   <ul>
    <li><a href="/qtractor-index.html#Intro">Intro</a></li>
    <li><a href="/qtractor-index.html#Screenshots">Screenshots</a></li>
    <li><a href="/qtractor-index.html#Features">Features</a></li>
    <li><a href="/qtractor-index.html#Requirements">Requirements</a></li>
    <li><a href="/qtractor-index.html#Downloads">Downloads</a></li>
    <li><a href="/qtractor-index.html#Installation">Installation</a></li>
    <li><a href="/qtractor-index.html#Configuration">Configuration</a></li>
    <li><a href="/qtractor-index.html#Bugs">Bugs</a></li>
    <li><a href="/qtractor-index.html#Support">Support</a></li>
    <li><a href="/qtractor-index.html#Acknowledgements">Acknowledgements</a></li>
   </ul>
   </li>
   <li><a href="/qtractor-downloads.html#Downloads">Downloads</a>
   <ul>
    <li><a href="/qtractor-downloads.html#Git">Git</a></li>
   </ul>
   </li>
   <li><a href="/qtractor-screenshots.html">Screenshots</a></li>
   <li><a href="/doc" class="current">Documentation</a></li>
   </ul>
  </div>
  <h1><a href="${WIKI_URL}">${WIKI_TITLE}</a> <small>$y</small></h1>
  <hr />
__EoF5
  cat <<__EoF6 > wiki_footer.txt
  <br />
  <div id="footer">
   <hr />
   <small><a href="${WIKI_URL}">${WIKI_TITLE}</a> -
    ${WIKI_FUSER} <em>et al.</em> ${WIKI_STAMP}</small>
  </div>
__EoF6
  cat "$x" \
  | pandoc -s -T "${WIKI_TITLE}" -V pagetitle="$y" --css none \
    -M document-css=false \
    -H wiki_style.txt -B wiki_header.txt -A wiki_footer.txt \
    -f markdown -t html \
  > "$y".html;
done


# 9. Cleanup ...
rm *.md* wiki*.txt wiki*.css wiki*.csv


# 10. Archive ...
zip -u wiki.zip *.html *.epub *.pdf images/* attachments/*


# 11. Publish ...
rsync -auv images/*      beta:www/datahub/qtractor-doc/images/
rsync -auv attachments/* beta:www/datahub/qtractor-doc/attachments/
rsync -auv *.html        beta:www/datahub/qtractor-doc/
rsync -auv *.epub *.pdf  beta:www/datahub/qtractor-doc/
rsync -auv wiki*.*       beta:www/datahub/qtractor-doc/
rsync -auv ../wiki*.sh*  beta:www/datahub/qtractor-doc/
#
#rsync -auv images/*      rncbc,qtractor@web.sourceforge.net:htdocs/doc/images/
#rsync -auv attachments/* rncbc,qtractor@web.sourceforge.net:htdocs/doc/attachments/
#rsync -auv *.html        rncbc,qtractor@web.sourceforge.net:htdocs/doc/
#
rsync -auv *.epub *.pdf rncbc,qtractor@frs.sourceforge.net:/home/frs/project/q/qt/qtractor/qtractor/docs/
#
sftp rncbc@qtractor.org <<__Eof6
  cd public_html/doc/images
  put -p images/*
  cd
  cd public_html/doc/attachments
  put -p attachments/*
  cd
  cd public_html/doc
  put -p *.html
__Eof6
