#!/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/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_get_extension () {
    echo "$1" | awk -F . '{print $NF}'
}

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 [ $(yelp_get_extension ${1}) = "tmpl" -a -f "${1}" ]; then
    infile="${1}"
    outext="."$(yelp_get_extension $(basename "${1}" ".tmpl"))
elif [ -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 variables for substitution
if type git >/dev/null 2>&1; then
    username=`git config user.name`
    useremail=`git config user.email`
fi
if [ "x$username" = "x" -a "x$useremail" = "x" ]; then
    if type bzr >/dev/null 2>&1; then
        username=`bzr whoami | sed -e 's/ <.*//'`
        useremail=`bzr whoami --email`
    fi
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

outid=$(basename "${2}")

if [ "x$spec" != "x" ]; then
    if [ "."$(yelp_get_extension "${2}") = "$spec" ]; then
        outfile="${2}"
    elif [ "."$(yelp_get_extension "${2}") = "$outext" ]; then
        outfile="${2}${spec}"
    else
        outfile="${2}${outext}${spec}"
    fi
elif [ "."$(yelp_get_extension ${2}) = "$outext" ]; then
    outfile="${2}"
else
    outfile="${2}${outext}"
fi

if [ "x$spec" = "x.tmpl" ]; then
    cp "$infile" "$outfile"
else
    cat "$infile" | grep -v '<\?yelp-tmpl-desc' | sed \
        -e s/@ID@/"$outid"/ \
        -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

