#!/bin/bash


# clear out the old files and copy back the files
[ -x output ] && rm -rf output/ && mkdir output
[ ! -x output ] && mkdir output # just in case ö_ö
cp -r ressources-orig output/ressources

insHeader(){
#takes $1 for the title

	cat <<< '<!DOCTYPE html><html lang="de">'
	
	cat <<< '<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<meta name="description" content="">
	<meta name="author" content="">
	<link rel="icon" href="https://getbootstrap.com/favicon.ico">
	<!-- please dont judge me, im not getting paid for this. -->'

	cat <<< "<title>$1</title>"

	cat <<< '<!-- Bootstrap core CSS -->
	<link href="ressources/bootstrap.css" rel="stylesheet">
	<!-- Custom styles for this template -->
	<link href="ressources/sticky-footer-navbar.css" rel="stylesheet">
	</head>'

}

insNavbar(){

	cat <<< '<header><nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">'
	cat <<< '<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>'

	cat <<< '<div class="collapse navbar-collapse" id="navbarCollapse">'
	cat <<< '<ul class="navbar-nav mr-auto">'

	cat <<< '<a class="navbar-brand" href="#">Navigation</a>'

	for navitem in content/*.html
	do
		pagename=$(basename $navitem .html)
		if [[ "$navitem" == "$content" ]]
		then
		        cat <<< '<li class="nav-item active">'
			cat <<< "<a class=\"nav-link\" href=\"#\">$pagename</a>"
		else
			cat <<< '<li class="nav-item">'
			cat <<< "<a class=\"nav-link\" href=\"$(basename $navitem)\">$pagename</a>"
		fi
		cat <<< '</li>'
	done
	cat <<< '</ul>'
	cat <<< '</div>'
	cat <<< '</nav></header>'
	# end navbar content
}

insFooter(){

	cat <<< '
	<!--<script src="ressources/lib/web/viewer.js"></script>-->
	<script src="ressources/jquery-3.js"></script>
	<script src="ressources/popper.js"></script>
	<script src="ressources/bootstrap.js"></script>
	'

	cat <<< '<footer class="footer">
	  <div class="container">
	    <span class="text-muted">© 2019, BFI St. Stefan</span>
	  </div>
	</footer>
	'

	cat <<< '</body></html>'
}

genHTML(){
	content=$1

	# name of the file without ending as title for the webpage
	insHeader $(basename $content .html)

	# insert the navigation bar
	insNavbar

	# insert the body 
	cat <<< '<body>'

	# main content
	cat $content 

	# insert the script links and footer
	insFooter
}

for content in content/*.html
do
	genHTML $content > output/$(basename $content)
done
