#!/bin/sh
#
# /usr/local/bin/localwebresolve
# Copyright 2013 Jonas Smedegaard <dr@jones.dk>
#
# Bootstrap Ikiwiki-based blog site
#

set -e

BLOGGITDIR="$HOME/private_git/blog"
BLOGWORKDIR="$HOME/private_webdata/blog"
BLOGWORKDIR="$HOME/public_html"
BLOGPROTO="http"
BLOGHOST="user.homebase.dk"

# allow overriding above
[ ! -r /etc/local/ikiblog.conf ] || . /etc/local/ikiblog.conf

gitdir="$BLOGGITDIR"
workdir="$BLOGWORKDIR"
webdir="$BLOGWORKDIR"
baseurl="$BLOGPROTO://$BLOGHOST/~$USER"

# clone/create public repositories
mkdir -p "$gitdir"
git clone --bare git://source.jones.dk/ikiwiki/site "$gitdir.git"
git init --bare "$gitdir/content.git"

# clone/create private working repositories
mkdir -p "$(dirname "$workdir")"
git clone --recursive "$gitdir.git" "$workdir"
git clone "$gitdir/content.git" "$workdir/content"

# populate content
cd "$workdir/content"
rsync -av /usr/share/ikiwiki/examples/blog/ .
git add .
git commit -m "Initial commit, from sample blog."
git push

# redirect compiled blog to public folder
mkdir -p "$workdir/build"
ln -s "$webdir" "$workdir/build/html"
ln -s "$webdir" "$workdir/build/cgi"

# Customize settings and bootstrap
echo "# IkiWiki::Setup::Yaml" > "$workdir/config/ikiwiki.setup.in"
echo "url: $baseurl" >> "$workdir/config/ikiwiki.setup.in"
echo "cgiurl: $baseurl/ikiwiki.cgi" >> "$workdir/config/ikiwiki.setup.in"
echo "theme: goab" >> "$workdir/config/ikiwiki.setup.in"
make -C "$workdir" init

# initialize blog
make -C "$workdir"