Mini Shell
#!/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=$(grep '<\?yelp-tmpl-desc' "$f" | 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"
wroteheader=0
for f in *.page.tmpl; do
if [ -e "$f" ]; then
if [ $wroteheader = 0 ]; then
echo ""
echo "Local Mallard Templates:"
wroteheader=1
fi
yelp_describe_tmpl "$f" "page.tmpl"
fi
done
wroteheader=0
for f in ${tmpldir}*.page; do
if [ -e "$f" ]; then
if [ $wroteheader = 0 ]; then
echo ""
echo "Mallard Templates:"
wroteheader=1
fi
yelp_describe_tmpl "$f" "page"
fi
done
wroteheader=0
for f in *.docbook.tmpl; do
if [ -e "$f" ]; then
if [ $wroteheader = 0 ]; then
echo ""
echo "Local DocBook Templates:"
wroteheader=1
fi
yelp_describe_tmpl "$f" "xml.tmpl"
fi
done
wroteheader=0
for f in ${tmpldir}*.docbook; do
if [ -e "$f" ]; then
if [ $wroteheader = 0 ]; then
echo ""
echo "DocBook Templates:"
wroteheader=1
fi
yelp_describe_tmpl "$f" "xml"
fi
done
}
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" ] && [ -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 command -v git >/dev/null 2>&1; then
username=$(git config user.name)
useremail=$(git config user.email)
fi
if [ "x$username" = "x" ] && [ "x$useremail" = "x" ]; then
if command -v bzr >/dev/null 2>&1; then
username=$(bzr whoami | sed -e 's/ <.*//')
useremail=$(bzr whoami --email)
fi
fi
if [ "x$username" = "x" ] && [ "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
grep -v '<\?yelp-tmpl-desc' "$infile" | 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