------------------------------------------------------------
#!/bin/bash
# 2020-11-15 ew

unset LANG
set -euo pipefail

dir_source="site.public.onion"
dir_target="site.public.onion.html"
declare sha=sha512
declare shafile="ew.flightlog.${sha}sums"

[ -d "./${dir_target}" ] || mkdir -p ./${dir_target}

(
    # list all files in dir_source
    cd ./${dir_source} && find . -type f |
            LANG='' sort |
            grep -vE '~$|\.${sha}$|template.gmi|wrap_head.gmi|wrap_tail.gmi'
) | (
    cd ./${dir_target} &&
        while read _file
        do
            html_file="${_file/\.gmi/.html}"
            # create dir
            dir=${_file%/*}
            [ -d ${dir} ] || mkdir -p "${dir}"
            case "${_file}" in
                (*.gmi)
                    # convert .gmi files
                    ../bin/gem2html.sh ../${dir_source}/${_file} > ./${html_file}.new
                    echo -n ". $html_file "
                    if cmp --silent "$html_file" "${html_file}.new"
                    then
                        rm -f ./${html_file}.new
                        echo "found."
                    else
                        mv ./${html_file}.new ./${html_file}
                        echo "updated."
                    fi
                    ;;
                (*)
                    # copy all others
                    cp -a ../${dir_source}/${_file} ./${_file}.new
                    echo -n ". $_file "
                    if cmp --silent "$_file" "${_file}.new"
                    then
                        rm -f ./${_file}.new
                        echo "found."
                    else
                        mv ./${_file}.new ./${_file}
                        echo "updated."
                    fi
                    ;;
            esac
        done

)
------------------------------------------------------------