#!/bin/sh
#
# stuff we should do in the makefile
# but can't for portability reasons.
# public domain

copy()
{
	dotfiles="$HOME/$1 $dotfiles"

	dname=$(dirname "$HOME/$1")
	cat <<EOF >> Makefile.local
$HOME/$1: $2
	[ ! -d "$dname" ] && mkdir -p "$dname" || true
	cp \$? \$@
EOF
}

pair()
{
	files="$files $2"
	dotfiles="$HOME/$1 $dotfiles"
	xxxfiles="${2%.lp} $xxxfiles"

	dname=$(dirname "$HOME/$1")
	cat <<EOF >> Makefile.local
$HOME/$1: $2
	[ ! -d "$dname" ] && mkdir -p "$dname" || true
	./lpp \$? > \$@
EOF

	if [ -n "$3" ]; then
		printf '\tchmod %s $@\n' "$3" >> Makefile.local
	fi
	echo >> Makefile.local
}

conv()
{
	gemfiles="gem/$1.gmi $gemfiles"
	wwwfiles="www/$1.html $wwwfiles"
	dname=$(dirname "$1")
	cat <<EOF >> Makefile.local
gem/$1.gmi: $2
	[ ! -d "gem/$dname" ] && mkdir -p "gem/$dname" || true
	sed 's/EXT/gmi'/ $2 | ./unpar | ./gc > \$@

www/$1.html: gem/$1.gmi header.html footer.html
	[ ! -d "www/$dname" ] && mkdir -p "www/$dname" || true
	sed 's!TITLE!$1!' header.html > \$@
	sed 's/EXT/html/' $2 | ./unpar | ./gc | ./gem2html >> \$@
	cat footer.html >> \$@

EOF
}

rm -f Makefile.local

pair .profile		profile.lp
pair .kshrc		kshrc.lp
pair lib/profile	rc.lp
pair .sqliterc		sqliterc.lp
pair .psqlrc		psqlrc.lp
pair .cwmrc		cwmrc.lp
pair .mg		mg.lp

pair bin/acmerc		bin/acmerc.lp	+x
pair bin/amused-monitor	bin/amused-monitor.lp +x
pair bin/browser	bin/browser.lp	+x
pair bin/clip		bin/clip.lp	+x
pair bin/diffstat	bin/diffstat.lp	+x
pair bin/gamemode	bin/gamemode.lp	+x
pair bin/gmi2txt	bin/gmi2txt.lp	+x
pair bin/mquote		bin/mquote.lp	+x
pair bin/myterm		bin/myterm.lp	+x
pair bin/nof		bin/nof.lp	+x
pair bin/sshot		bin/sshot.lp	+x
pair bin/template	bin/template.lp	+x

copy bin/news		bin/news

pair .config/herbstluftwm/autostart	config/herbstluftwm/autostart.lp
pair .config/herbstluftwm/panel		config/herbstluftwm/panel.lp	+x
pair .config/herbstluftwm/process.awk	config/herbstluftwm/process.awk.lp
pair .config/herbstluftwm/lib		config/herbstluftwm/lib.lp
pair .config/herbstluftwm/event-gen	config/herbstluftwm/event-gen.lp

pair .emacs.d/early-init.el	emacs/early-init.el.lp
copy .emacs.d/init.el		emacs/init.el
copy .emacs.d/minimal-theme.el	emacs/minimal-theme.el
copy .emacs.d/simple-pass.el	emacs/simple-pass.el

conv index		index.lp
for file in $files; do
	conv "${file%.lp}" "$file"
done

cat <<EOF >> Makefile.local
DOTFILES =	$dotfiles
GEMFILES =	$gemfiles
WWWFILES =	$wwwfiles
XXXFILES =	$(printf "%s\n" $xxxfiles | sort | xargs echo)
EOF
