#!/bin/bash
# Copyright (C) 2019 Marius Schellenberger

# this file generates Go source code for the gowiki
# it contains html and css and uses the contents
# of the templates directory

templates_go="pkg/server/templates.go"
echo "Generating ${templates_go}"

if [ ! -d templates ]; then
  echo "error: templates directory not found" 1>&2
  exit 2
fi

cat << EOF > ${templates_go}
// Copyright (C) 2019 Marius Schellenberger

// This file is auto-generated by templates.sh
// using the contents of the templates directory

package server

import (
	"encoding/base64"
	"errors"
	"git.giftfish.de/ston1th/gowiki/pkg/log"
	"html/template"
)

EOF

echo "const (" >> ${templates_go}

for f in $(ls templates/*.html); do
  fname=${f/templates\//}
  fname=${fname/.html/}
  echo "	${fname} = \`$(cat ${f})\`" >> ${templates_go}
done

for f in $(ls templates/*.css); do
  fname=${f/templates\//}
  fname=${fname/.css/CSS}
  echo "	${fname} = \`$(cat ${f})\`" >> ${templates_go}
done

echo "	favicon = \"$(base64 -w 0 templates/favicon.ico)\"" >> ${templates_go}

cat << EOF >> ${templates_go}
)

func (s *HTTPServer) loadTemplates() {
	var fatal bool
	s.templ = make(map[string]*template.Template)
	s.res = make(map[string][]byte)
	parse := func(html ...string) (temp *template.Template) {
		var err error
		temp = template.New("")
		for _, s := range html {
			temp, err = temp.Parse(s)
			if err != nil {
				log.Println("parse:", err)
				fatal = true
				return
			}
		}
		return
	}

	// resources
	s.res["bootstrap.css"] = []byte(bootstrapCSS)
	s.res["custom.css"] = []byte(customCSS)
	fav, err := base64.StdEncoding.DecodeString(favicon)
	if err != nil {
		log.Println("favicon:", err)
		fatal = true
	}
	s.res["favicon.ico"] = fav

	// pages
	s.templ["notFoundHandler"] = parse(index, menu, notFound)
	s.templ["forbiddenHandler"] = parse(index, menu, forbidden)
	s.templ["loginHandler"] = parse(index, menu, login)
	s.templ["loginTotpHandler"] = parse(index, menu, loginTotp)
	s.templ["searchHandler"] = parse(index, menu, search)
	s.templ["pageHandler"] = parse(index, menu, page)
	s.templ["pageNewHandler"] = parse(index, menu, pageNew)
	s.templ["pageEditHandler"] = parse(index, menu, pageEdit)
	s.templ["pageViewHandler"] = parse(index, menu, pageView)
	s.templ["pageMDHandler"] = parse(index, menu, pageMD)
	s.templ["pageShareHandler"] = parse(index, menu, pageShare)
	s.templ["pageBlacklistHandler"] = parse(index, menu, pageBlacklist)
	s.templ["pageDelHandler"] = parse(index, menu, pageDel)
	// sections
	s.templ["sectionHandler"] = parse(index, menu, section)
	s.templ["sectionsHandler"] = parse(index, menu, sections)
	s.templ["sectionNewHandler"] = parse(index, menu, sectionNew)
	s.templ["sectionEditHandler"] = parse(index, menu, sectionEdit)
	s.templ["sectionDelHandler"] = parse(index, menu, sectionDel)
	// users
	s.templ["usersHandler"] = parse(index, menu, users)
	s.templ["userNewHandler"] = parse(index, menu, userNew)
	s.templ["userEditHandler"] = parse(index, menu, userEdit)
	s.templ["userDelHandler"] = parse(index, menu, userDel)
	// totp
	s.templ["userTotpHandler"] = parse(index, menu, userTotp)

	if fatal {
		log.Fatal("parse: ", errors.New("parsing templates failed"))
	}
}
EOF
