#!/bin/sh
# yelp-new
# Copyright (C) 2010 Shaun McCance <shaunm@gnome.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

tmpldir="/usr/local/share/yelp-tools/templates/"

yelp_describe_tmpl () {
    line="  "`basename "$1" | sed -e 's/\.'$2'$//'`
    desc=`cat "$f" | grep '<\?yelp-tmpl-desc' | sed -e 's/<?yelp-tmpl-desc //' -e 's/?>$//'`
    if [ "x$desc" != "x" ]; then
        line="$line - $desc"
    fi
    echo "$line"
}

yelp_usage() {
    echo "Usage: yelp-new [OPTIONS] <TEMPLATE> <ID> [TITLE]"
    echo ""
    echo "Options:"
    echo "  --stub  Create a .page.stub file instead of a .page file"
    echo "  --tmpl  Copy an installed template to a local template"
    if [ -f *.page.tmpl ]; then
        echo ""
        echo "Local Mallard Templates:"
        for f in *.page.tmpl; do
            yelp_describe_tmpl "$f" "page.tmpl"
        done
    fi
    if [ -f ${tmpldir}*.page ]; then
        echo ""
        echo "Mallard Templates:"
        for f in ${tmpldir}*.page; do
            yelp_describe_tmpl "$f" "page"
        done
    fi
    if [ -f *.docbook.tmpl ]; then
        echo ""
        echo "Local DocBook Templates:"
        for f in *.docbook.tmpl; do
            yelp_describe_tmpl "$f" "xml.tmpl"
        done
    fi
    if [ -f ${tmpldir}*.docbook ]; then
        echo ""
        echo "DocBook Templates:"
        for f in ${tmpldir}*.docbook; do
            yelp_describe_tmpl "$f" "xml"
        done
    fi
}

if [ $# -lt 2 ]; then
    yelp_usage
    exit 1
fi

# Process options
spec=""
while [ $# -gt 0 ]; do
    case "$1" in
        --stub)
            spec=".stub"
            shift;;
        --tmpl)
            spec=".tmpl"
            shift;;
        -h | --help)
            yelp_usage
            exit 0;;
        *)
            break
    esac
done

# Locate the template file
if [ -f "${1}.page.tmpl" ]; then
    infile="${1}.page.tmpl"
    outext=".page"
elif [ -f "${tmpldir}${1}.page" ]; then
    infile="${tmpldir}${1}.page"
    outext=".page"
elif [ -f "${1}.docbook.tmpl" ]; then
    infile="${1}.docbook.tmpl"
    outext=".docbook"
elif [ -f "${tmpldir}${1}.docbook" ]; then
    infile="${tmpldir}${1}.docbook"
    outext=".docbook"
else
    echo "Error: No template named ${1} found"
    exit 1
fi

# Set up some variable for substitution
if type git >/dev/null 2>&1; then
    username=`git config user.name`
    useremail=`git config user.email`
elif type bzr >/dev/null 2>&1; then
    username=`bzr whoami | sed -e 's/ <.*//'`
    useremail=`bzr whoami --email`
fi
if [ "x$username" = "x" -a "x$useremail" = "x" ]; then
    username='YOUR NAME'
    useremail='YOUR EMAIL ADDRESS'
fi
pagetitle="$3"
if [ "x$pagetitle" = "x" ]; then
    pagetitle="TITLE"
fi

outfile="${2}${outext}${spec}"
if [ "x$spec" = "x.tmpl" ]; then
    cp "$infile" "$outfile"
else
    cat "$infile" | grep -v '<\?yelp-tmpl-desc' | sed \
        -e s/@ID@/"$2"/ \
        -e s/@DATE@/`date +%Y-%m-%d`/ \
        -e s/@YEAR@/`date +%Y`/ \
        -e s/@NAME@/"$username"/ \
        -e s/@EMAIL@/"$useremail"/ \
        -e s/@TITLE@/"$pagetitle"/ \
        > "$outfile"
fi

