#!/bin/sh

set -x

base="$(basename "${0}" ".sh" )"
tmp="/tmp/${base}_$$.tmp"
out=""

savePage=0
if [ "${1}" = "--file" ]
then
	savePage=1
fi

####################################################################################################
####################################################################################################
###
###	Site List ("heredoc" storage format)
###

cat >"${tmp}" <<'EnDoFfIlE'
Open Worldwide Application Security Project User group (OWASP) - Ottawa chapter|https://github.com/OWASP/www-chapter-ottawa|
Open Source Nerds and Nachos|https://www.meetup.com/opensourcenerdsandnachos/|
Ottawa Capital Linux User Group|https://wiki.linux-ottawa.org/doku.php?id=start|
Ottawa BSD User Group|ocbug@ocbug.ca|
Hamilton BSD User Group|https://hambug.ca/|
Ontario BSD User Group|https://www.onbsd.ca/|
Greater Toronto Area BSD User Group|https://www.gtabug.ca/|
Linux User Group at Ryerson University (now, Toronto Metropolitan University)|https://www.facebook.com/RULUG/|
Vancouver Linux Users Group|https://vanlug.ca/|
Nova Scotia Linux Users Group|https://www.nslug.ns.ca/|
Edmonton Linux Users Group|https://elug.ca/|
Manitoba Unix Users Group|https://muug.ca/|
Rutgers Student Linux User Group|https://www.cs.rutgers.edu/component/content/article/rutgers-student-linux-user-group-ruslug?Itemid=101&amp;catid=129|
Low Level Linux User Group (L3GO)|sigs-l3go@linux-ottawa.org|
Toronto Postgres User Group|https://www.meetup.com/toronto-postgres-user-group/|
Rencontres Linux|https://www.rencontres-linux.quebec/|
Linux Gatineau|https://www.linux-gatineau.org|
Calgary Unix Users Group|https://cuug.ab.ca/|
Savoir Faire Linux|https://www.savoirfairelinux.ca|
Club Framboise (dedicated to the Raspberry Pi)|https://www.clubframboise.ca|
Microsoft Reactor Toronto|https://www.meetup.com/microsoft-reactor-toronto/|
EnDoFfIlE


####################################################################################################
####################################################################################################
###
###	HTML Wrapper for displayed list
###

buildPage()
{
	tableStyles=""
	if [ ${inclCSS} -eq 1 ]
	then
		tableStyles=$( cat <<EnDoFfIlE
<style>

.column {
	width:			100% ;
}

.siteDesc, .siteUrl {
	width:			100% ;
	padding-bottom:		0px ;
}

.siteDesc {
	font-family:		sans-serif ;
	font-size:		15px ;
	font-style:		normal ;
	font-weight:		bold ;
	padding-top:		4px ;
	padding-left:		3px ;
}

.siteUrl {
	font-family:		monospace ;
	font-size:		13px ;
	font-style:		italic ;
	font-weight:		normal ;
	padding-top:		0px ;
	padding-left:		30px ;
}

a {
	text-decoration:	none ;
	font-size:		20px ;
}

</style>
EnDoFfIlE
)
	fi

	cat <<EnDoFfIlE
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
${tableStyles}

</head>
<body>
Here are some user groups based in Canada: <br>
<br>

${formattedList}

</body>
</html>
EnDoFfIlE

} #buildPage()


####################################################################################################
####################################################################################################
###
###	Generate page using original basic presentation format
###

doBasic(){
	#awk -v mode=${sel} 'BEGIN{
	formattedList=$( awk 'BEGIN{
		sIndx=0 ;
		pIndx=0 ;
	}{
		if( NF > 0 ){
			n=split( $0, sites, "|" ) ;
			printf("- %s:<br/>\n\n &nbsp; &nbsp; &nbsp; %s<br/>\n\n", sites[1], sites[2] ) ;
		} ;
	}' <${tmp} )

	if [ ${savePage} -eq 1 ]
	then
		out="${base}.html"
		buildPage >"${out}"
	else
		buildPage
	fi

	
} #doBasic()


####################################################################################################
####################################################################################################
###
###	Generate page using original basic presentation format
###

doTable(){
	#awk -v mode=${sel} 'BEGIN{
	formattedList=$( awk 'BEGIN{
		sIndx=0 ;
		pIndx=0 ;

		###	Initialize tags for start of HTML table
		printf("<table class=\"column\">\n")
	}{
		if( NF > 0 ){
			n=split( $0, sites, "|" ) ;
			printf("\t<tr><td class=\"siteDesc\"> ● %s: <a href=\"%s\"> ▶</a></td></tr>\n\t<tr><td class=\"siteUrl\">%s</td></tr>\n", sites[1], sites[2], sites[2] ) ;
		} ;
	}END{
		###	Close off tags for end of HTML table
		printf("</table>\n") ;
	}' <${tmp} )

	if [ ${savePage} -eq 1 ]
	then
		out="${base}.html"
		buildPage >"${out}"
	else
		buildPage
	fi

	
} #doBasic()




####################################################################################################
####################################################################################################
###
###	Script controlling logic
###

inclCSS=0

echo "
Choose HTML page format:

[1] Basic (original)
[2] Table

Enter selection [ (1) | 2 ] => \c" ; read sel
if [ -z "${sel}" ] ; then  sel=1 ; fi

case ${sel} in
	1 ) doBasic ;;
	2 ) inclCSS=1 ; doTable ;; 
	* ) echo "\t Selection entered is not available.  Try again!\n" ; exit 1 ;;
esac
