#!/bin/sh
#
# gmi2htmldir.sh -- POSIX shell script to convert a folder of Gemini files to HTML files
#
# Copyright (c) 2021 Jeremy Potter (jwinnie)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# usage:
# $ ./gmi2htmldir.sh <in> <out> title=<title> css=<css> original=<original>
#
# parameters:
# <in>: the folder of Gemini files to be converted
# <out>: the folder in which to put the generated HTML files
# <title>, <css>, <original>: parameters passed to gmi2html.awk; see gmi2html.awk for more details
#
in=$1
out=$2
title=$3
css=$4
original=$5
# Get the path of the gmi2html.awk script, which is in the
# same directory as this one.
#
# Credit to T.J. Crowder from Stack Overflow for the method
# of finding $SCRIPTPATH: https://stackoverflow.com/a/4774063
SCRIPTPATH=$(cd -- "$(dirname "$0")" >/dev/null 2>&1; pwd -P)
gmi2html=${SCRIPTPATH}/gmi2html.awk
# Loop through all the files in the directory (cd to the directory
# so that we get the relative path with relation to the root directory
# of each file)
cd $in
for f in *.gmi **/*.gmi; do
# Replace .gmi extension with .html
HTML_FILENAME=$out/${f%.gmi}.html
echo "processing: ${f} -> ${HTML_FILENAME}"
# Run the gmi2html.awk script, passing in the parameters
awk -f $gmi2html -v $title -v $css -v $original \
< $f \
> $HTML_FILENAME
done