#!/bin/bash -e

#-----------------------
VERSION=1.1
#-----------------------
set -x
DEST=../rougail-tutorials

if [ -d $DEST ]; then
    echo "déjà fait"
    exit 1
fi

export LC_ALL=C

BUILDER="$(pwd)"
mkdir -p $DEST/firefox
rm -f summary.md
touch summary.md
rm -f summary_total.md
touch summary_total.md
cd "$DEST"
DEST="$(pwd)"


cp "$BUILDER"/firefox.png .
cp "$BUILDER"/foxyproxy.png .
cp "$BUILDER"/summary.md .
touch README.md
"$BUILDER"/build.py "$VERSION"
rm -rf summary.md
rm -rf summary_total.md
rm -rf jinja_caches
git init -b "$VERSION"
git add .
git commit -am"$(cat $BUILDER/TITLE)"
#old_commit_id="$(git rev-parse HEAD)"
cd - > /dev/null

for i in $(ls -d "examples"/* | sort); do
        num=$(echo $(basename "$i"))
        echo "$num"
        commit_id="v$VERSION_$num"
        summary="[tutorial $(basename $i)] $(head -n 1 $i/README.md)"
        echo "- [$summary](https://forge.cloud.silique.fr/stove/rougail-tutorials/src/commit/$commit_id/README.md) ([diff](https://forge.cloud.silique.fr/stove/rougail-tutorials/compare/${commit_id}~1..$commit_id))" >> summary_total.md
done

for i in $(ls -d "examples"/* | sort); do
    if [ -f "$i/TITLE.md" ]; then
        echo "- $(head -n 1 $i/TITLE.md)" >> summary.md
    fi
    if [ -f "$i/SUBTITLE.md" ]; then
        echo "  - $(head -n 1 $i/SUBTITLE.md)" >> summary.md
    fi
    if [ -f "$i/README.md" ]; then
        rm -rf "$DEST/config"
        num=$(echo $(basename "$i"))
        echo "$num"
        commit_id="v$VERSION_$num"
        summary="[tutorial $(basename $i)] $(head -n 1 $i/README.md)"
        diff_url="https://forge.cloud.silique.fr/stove/rougail-tutorials/compare/${commit_id}~1..$commit_id"
        echo "    - [$summary](https://forge.cloud.silique.fr/stove/rougail-tutorials/src/commit/$commit_id/README.md) ([diff]($diff_url))" >> summary.md
        cp "summary_total.md" "$DEST"
        cd "$i/dictionaries"
        if [ -d firefox ]; then
            for j in $(ls firefox/*.yml 2>/dev/null); do
                cp "$j" "$DEST/$j"
            done
        fi
        if [ -d foxyproxy ]; then
            mkdir -p $DEST/foxyproxy
            for j in $(ls foxyproxy/*.yml 2>/dev/null); do
                cp "$j" "$DEST/$j"
            done
        fi
        cp -f ../README.md "$DEST"
        if [ -d ../config ]; then
            cp -aL ../config "$DEST"
        fi
        msg="[tutorial $(basename $i)] $(cat ../README.md)"
        [ -e ../firefox.png ] && cp ../firefox.png "$DEST"
        [ -e ../foxyproxy.png ] && cp ../foxyproxy.png "$DEST"
        cd - > /dev/null
        cd "$DEST"
        #
        before=$(grep "commit/$commit_id/README.md" summary_total.md -B1 | wc -l)
        echo "- [Summary](https://forge.cloud.silique.fr/stove/rougail-tutorials/src/branch/$VERSION/README.md)" > summary_before.md
        if [ "$before" = "2" ]; then
            echo $(grep "commit/$commit_id/README.md" summary_total.md -B1 | head -n 1) >> summary_before.md
        fi
        echo "" >> summary_before.md
        echo "# $summary" >> summary_before.md
        echo "" >> summary_before.md
        echo "[View the diff]($diff_url)" >> summary_before.md
        after=$(grep "commit/$commit_id/README.md" summary_total.md -A1 | wc -l)
        if [ "$after" = "2" ]; then
            echo $(grep "commit/$commit_id/README.md" summary_total.md -A1 | tail -n 1) > summary_after.md
        fi
        #
        if [ -f config/mode ]; then
            mv config/mode mode
        fi
        "$BUILDER"/build.py "$VERSION"
        rm -rf config/*/README.md config/read_write config/namespace
        rm -rf jinja_caches summary.md summary_before.md summary_after.md summary_total.md
        if [ -f mode ]; then
            MODE=1
            rm -f mode
        else
            MODE=0
        fi
        git add *.png README.md
        if [ -d config ]; then
            git add config
        else
            git status |grep -q config && git add config
        fi
        git commit -m"$summary (config and doc)"
        git add .
        git commit -am"$msg"
        git tag -a "$commit_id" -m"format version $VERSION: $num"
        if [ "$MODE" = 1 ]; then
            touch mode
        fi
        cd - > /dev/null
#        old_commit_id="$commit_id"
    fi
done
cd "$DEST"
echo "# Description

This tutorial shows to you an example of Rougail use on how to set a proxy in the Mozilla Firefox browser.

More precisely, this tutorial aims at reproducing this [Mozilla Firefox](https://www.mozilla.org/firefox/new/) settings page:

![Firefox Proxy setting](firefox.png)

And the integration of part of the [Firefox FoxyProxy plugin](https://addons.mozilla.org/firefox/addon/foxyproxy-standard/).

The idea is to have a namespace specific to FoxyProxy and to find in it part of the settings that we will have made in the main namespace.

This is what the page looks like:

![Foxyproxy Proxy setting](foxyproxy.png)
" > README.md
echo '# Installation

```bash
# python -m venv rougail
# . rougail/bin/activate
# pip install rougail-cli rougail-output-exporter rougail-output-doc rougail-user-data-file
```
' >> README.md
echo "# Summary

Each step is a separerate commit. You can navigate to history to discover different aspect to the Rougail format.
" >> README.md
cp "$BUILDER"/firefox.png .
cp "$BUILDER"/foxyproxy.png .
cat "$BUILDER"/summary.md >> README.md
echo >> README.md
rm -rf firefox foxyproxy mode
git add .
git commit -am"Summary"
#
git remote add origin ssh://git@forge.cloud.silique.fr:2222/stove/rougail-tutorials.git
git push --set-upstream origin $VERSION --force
git push --tag --force

rm -f summary.md summary_total.md
echo "FAIT"
exit 0
