#!/bin/sh -e
#
# https://www.dioptre.fr/bin/jig
# Copyright 2020 Damien Riou <contact@dioptre.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

main() {
        test -n "$1" || usage
 	test -d "$1" || no_dir "$1"
        test -n "$2" || usage

        cd "${1%/}"
        create_header "$2"
        create_footer

        echo '[jig] done' >&2
}

usage() {
        echo "usage: ${0##*/} src dst" >&2
	exit 1
}

no_dir() {
	echo "${0##*/}: $1: No such directory" >&2
	exit 2
}

create_header() {
        cp _header.html joli-index/header.jihtml
        ji_css_js="<link rel=\"stylesheet\" type=\"text/css\" href=\"/joli-index/style-index.css\" />\n<script type=\"text/javascript\" src=\"/joli-index/script-index.js\" defer title=\"$1\"></script>"
        sed -i "/<\/head>/i $ji_css_js" joli-index/header.jihtml
}

create_footer() {
        cp _footer.html joli-index/footer.jihtml
        sed -i "s#<a href=\"https://www.romanzolotarev.com/ssg.html\">ssg5</a>#jig#" joli-index/footer.jihtml
}

main "$@"
