#!/bin/sh

yourname="ENTER-REPO-OWNER-NAME"
repo="$PWD"
name="$(basename $repo .git)"
baseurl="ENTER-YOUR-WEBSITE-DOMAIN"
sitetitle="ENTER-YOUR-WEBSITE-NAME"
indextitle="ENTER-INDEX-PAGE-TITLE"
indextagline="ENTER-INDEX-PAGE-TAG-LINE"
basedir="ENTER-YOUR-WEBSITE-DIRECTORY"
pubrepodir="ENTER-YOUR-GIT-DIRECTORY"
htdir="$basedir/$name"
filesdir="ENTER-STAGIT-FILES-DIRECTORY"
bindir="ENTER-STAGIT-BIN-DIRECTORY"

setupinfo() {
	echo "$yourname" > owner
	echo "$baseurl/$name" > url
	git update-server-info
}

genstagit() {
	rm -rf "$htdir"
	mkdir -p "$basedir"
	cp -r "$repo" "$htdir"
	cd "$htdir"
	stagit -l 50 "$repo"
	git archive HEAD -o "$basedir/$name.zip"
	git show HEAD:README.md > file/README.md
}

cpindexfile() {
	cp "$htdir/file/README.md.html" "$htdir/index.html"
	printf '%s\n' \
		'g/="\.\.\//s//="/g' \
		w q \
	| ed -s "$htdir/index.html" > /dev/null
}
# this just redirects nav readme to repo index, probably a waste of time.
readmelink() {
	printf '%s\n' \
		"g/>README<\/a>/s|href=\"[^\"]*README\.md\.html\"|href=\"$baseurl/$name/\"|" \
		w q \
	| ed -s "$1" > /dev/null
}

cpstylefiles() {
	cp $filesdir/favicon.png "$1"
	cp $filesdir/logo.png    "$1"
	cp $filesdir/style.css   "$1"
	cp -r $filesdir/fonts    "$1"
}

formatreadme() {
	cd "$htdir"
	printf '%s\n' \
		g/class=\"line\"/d \
		"/<pre id=\"blob\">" d d i "$(lowdown file/README.md)" . w \
	| ed -s file/README.md.html > /dev/null
}

adddownloadbutton() {
	cd "$htdir"
	printf '%s\n' \
		"/log\.html\">Log<\/a>" i \
		"<a href=\"$baseurl/$name.zip\">Download</a> |" . w \
	| ed -s "$1" > /dev/null
}

addhomelink() {
	cd "$htdir"
	printf '%s\n' \
		"/<h1>" \
		"s|<h1>|<h1><a href=\"$baseurl\">$indextitle:</a><\/h1> <h1> |" . w \
	| ed -s "$1" > /dev/null
}

addbottombar() {
	printf '%s\n' \
		"/<\/body>" i "$(cat $filesdir/bottom.html)" . w \
	| ed -s "$1" > /dev/null
}

newsitetitle() {
	printf '%s\n' \
		",s|<title>Repositories</title>|<title>$sitetitle</title>|" w \
	| ed -s "$1" > /dev/null
}

changeindextitlestyle() {
	if [ -n "$indextagline" ]; then
			tagline="<span class=\"desc\">$indextagline</span>"
	fi
	printf '%s\n' \
		"/<td><span class=\"desc\">Repositories" c \
		"<td><h1>$indextitle</h1>$tagline</td></tr><tr><td></td><td>" . w \
	| ed -s "$basedir/index.html" > /dev/null
}

genstagitindex() {
	stagit-index "$pubrepodir"/*.git | \
	sed 's|/log\.html||g' > "$basedir/index.html"
}

mkpage() {
	echo "Updating stagit page for $name"
	setupinfo
	genstagit
	cpstylefiles "./"
	[ -e $htdir/file/README.md ] && formatreadme
	for f in $(find "$htdir" -name "*.html"); do
		adddownloadbutton $f
		addhomelink $f
		readmelink $f
		addbottombar $f
	done
	cpindexfile
	mkindex
}

mkindex() {
	echo "Updating stagit index page"
	genstagitindex
	newsitetitle "$basedir/index.html"
	cpstylefiles "$basedir/"
	addbottombar "$basedir/index.html"
	changeindextitlestyle
}

if [ "$1" = "all" ]; then
	for g in "$pubrepodir"/*.git; do
		cd $g
		cp $bindir/post-receive-stagit hooks/post-receive
		chmod +x hooks/post-receive
		$bindir/post-receive-stagit
		cd $pubrepodir
	done
elif [ "$1" = "index" ]; then
	mkindex
else
	mkpage
fi
