1#!/bin/sh
2
3# refdbnd: creates a new document and sets up a Makefile for use
4# with RefDB bibliographies
5# Invocation: refdbnd basename doctype|filename pubtype database style encoding
6#         or: refdbnd (interactive mode)
7
8# markus@mhoenicka.de 2002-11-26
9# $Id: refdbnd.in,v 1.8.2.5 2005/07/29 21:21:50 mhoenicka Exp $
10
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19# GNU General Public License for more details.
20
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25# user variable defaults
26defbasename="refdbtest"
27defdoctype="db43x"
28defpubtype="book"
29defdatabase="refdbtest"
30defstyle="J.Biol.Chem."
31defencoding="utf-8"
32defcssfile=""
33xfile=""
34encoding=""
35cssfile=""
36silent="f"
37
38# Makefile-generated defaults
39maketemplate="<refdblib>/examples/Makefile.template"
40
41check_existing_doc() {
42    if [ -e $basename ]; then
43        # guess values from existing file
44	xfile=$basename
45
46	if grep "^<!DOCTYPE.*DTD DocBook V3.1//EN" $basename; then
47	    doctype="db31"
48	    pubtype=`grep "DTD DocBook V3.1//EN" $basename | sed 's/^<!DOCTYPE \(.*\) PUBLIC.*/\1/'`
49	else
50	    if grep "^<!DOCTYPE.*DTD DocBook V4.0//EN" $basename; then
51		doctype="db40"
52		pubtype=`grep "DTD DocBook V4.0//EN" $basename | sed 's/^<!DOCTYPE \(.*\) PUBLIC.*/\1/'`
53	    else
54		if grep "^<!DOCTYPE.*DTD DocBook V4.1//EN" $basename; then
55		    doctype="db41"
56		    pubtype=`grep "DTD DocBook V4.1//EN" $basename | sed 's/^<!DOCTYPE \(.*\) PUBLIC.*/\1/'`
57		else
58		    if grep "^<!DOCTYPE.*DTD DocBook XML V4.1.2//EN" $basename; then
59			doctype="db41x"
60			pubtype=`grep "DTD DocBook XML V4.1.2//EN" $basename | sed 's/^<!DOCTYPE \(.*\) PUBLIC.*/\1/'`
61			encoding=`grep "^<?xml version=\"1.0\"" $basename | sed 's/^.*encoding=\"\(.*\)\".*/\1/'`
62		    else
63			if grep "^<!DOCTYPE.*DTD DocBook XML V4.2//EN" $basename; then
64			    doctype="db42x"
65			    pubtype=`grep "DTD DocBook XML V4.2//EN" $basename | sed 's/^<!DOCTYPE \(.*\) PUBLIC.*/\1/'`
66			    encoding=`grep "^<?xml version=\"1.0\"" $basename | sed 's/^.*encoding=\"\(.*\)\".*/\1/'`
67			else
68			    if grep "^<!DOCTYPE.*DTD DocBook XML V4.2//EN" $basename; then
69				doctype="db43x"
70				pubtype=`grep "DTD DocBook XML V4.3//EN" $basename | sed 's/^<!DOCTYPE \(.*\) PUBLIC.*/\1/'`
71				encoding=`grep "^<?xml version=\"1.0\"" $basename | sed 's/^.*encoding=\"\(.*\)\".*/\1/'`
72			    else
73				if grep "^<!DOCTYPE.*TEI P4//DTD Main DTD Driver File//EN" $basename; then
74				    doctype="teix"
75				    pubtype=`grep "TEI P4//DTD Main DTD Driver File//EN" $basename | sed 's/^<!DOCTYPE \(.*\) PUBLIC.*/\1/'`
76				    encoding=`grep "^<?xml version=\"1.0\"" $basename | sed 's/^.*encoding=\"\(.*\)\".*/\1/'`
77				else
78				    if grep "xmlns.*\"http://www.tei-c.org/ns/1.0\"" $basename; then
79					doctype="tei5x"
80					pubtype="TEI"
81					encoding=`grep "^<?xml version=\"1.0\"" $basename | sed 's/^.*encoding=\"\(.*\)\".*/\1/'`
82				    fi
83				fi
84			    fi
85			fi
86		    fi
87		fi
88	    fi
89	fi
90	basename=${xfile%%.*}
91    fi
92}
93
94# if we have at least six command line arguments (cssfile is optional), we'll
95# use them. Otherwise ask interactively
96if [ -z "$*" ] || [ "$#" -lt 6 ]; then
97    # get arguments interactively
98    clear
99    echo "I'll be happy to assist you in setting up a new document along with"
100    echo "a Makefile. First we'll collect a few answers, and only if you"
101    echo "accept your settings any files will be created. Press Ctrl-C anytime"
102    echo "to exit."
103    echo ""
104    echo "Each question will present a default value which you can accept by"
105    echo "pressing ENTER"
106    echo ""
107    echo "Please enter the basename of your document. This is the name without"
108    echo "any suffix. For example, if your printed output file is supposed to"
109    echo "be called refdbtest.pdf, the basename will be 'refdbtest'"
110    echo "Alternatively, enter the full name of an existing document in this"
111    echo "directory. I'll try and guess the proper values for this document."
112    echo "[$defbasename]"
113    read basename
114    if [ -z "$basename" ]; then
115	basename=$defbasename
116    fi
117
118    check_existing_doc
119
120    if [ -z $doctype ]; then
121	echo ""
122	echo "Please enter the type of the document. Available types are 'db31',"
123	echo "'db40', and 'db41' for DocBook SGML versions 3.1, 4.0, and 4.1,"
124	echo "respectively, 'db41x', 'db42x', 'db43x', and 'db50x' for DocBook XML"
125	echo "versions 4.1.2, 4.2, 4.3, and 5.0, respectively, as well as"
126	echo "'teix' and 'tei5x' for TEI XML P4 and TEI XML P5, respectively"
127	echo "[$defdoctype]"
128	read doctype
129
130	echo ""
131	echo "Please enter the element root which determines the type of the"
132	echo "publication. Common are 'set', 'book', and 'article' for DocBook"
133	echo "documents and 'TEI.2' for TEI documents"
134
135        # use TEI.2 as default for TEI documents. $defpubtype defaults to 'book'
136	if [ "$doctype" = "teix" ]; then
137	    defpubtype="TEI.2"
138	else
139	    if [ "$doctype" = "tei5x" ]; then
140		defpubtype="TEI"
141	    fi
142	fi
143	echo "[$defpubtype]"
144	read pubtype
145    fi
146
147    echo ""
148    echo "Please enter the name of the RefDB database where you take your"
149    echo "references from"
150    echo "[$defdatabase]"
151    read database
152    echo ""
153    echo "Please enter the bibliography style that your document should use"
154    echo "Enter 'raw' to use the default formatting of the stock stylesheets"
155    echo "[$defstyle]"
156    read style
157    echo ""
158    if [ -z $encoding ]; then
159	echo "Please enter the character encoding that your document should use"
160	echo "[$defencoding]"
161	read encoding
162	echo ""
163    fi
164
165    if [ -z "$style" ]; then
166	style=$defstyle
167    fi
168
169    # mangle stylename to generate the name of the CSS file
170    defcssfile=${style%.*}.css
171
172    echo "Please enter the path or the URL of a custom CSS file for the"
173    echo "(x)html output. Hit ENTER if you do not use a custom CSS file"
174    echo "[$defcssfile]"
175    read cssfile
176    echo ""
177
178    # apply defaults to empty strings
179    if [ -z "$basename" ]; then
180	basename=$defbasename
181    fi
182    if [ -z "$doctype" ]; then
183	doctype=$defdoctype
184    fi
185    if [ -z "$pubtype" ]; then
186	pubtype=$defpubtype
187    fi
188    if [ -z "$database" ]; then
189	database=$defdatabase
190    fi
191    if [ -z "$encoding" ]; then
192	encoding=$defencoding
193    fi
194    if [ -z "$cssfile" ]; then
195	cssfile=$defcssfile
196    fi
197
198    echo "You've selected the following values:"
199    echo "Basename:           $basename"
200    echo "Document type:      $doctype"
201    echo "Publication type:   $pubtype"
202    echo "Database:           $database"
203    echo "Bibliography style: $style"
204    echo "Encoding:           $encoding"
205    echo "CSS file:           $cssfile"
206
207    echo ""
208    echo "Is this ok?"
209    echo "[y]"
210    read ok
211
212    if [ -z "$ok" ]; then
213	ok="y"
214    fi
215
216else
217    # all arguments were specified on the command line
218    basename=$1
219    doctype=$2
220    pubtype=$3
221    check_existing_doc
222    database=$4
223    style=$5
224    encoding=$6
225    cssfile=$7
226
227    # mangle stylename to generate the name of the CSS file
228    defcssfile=${style%.*}.css
229    if [ -z "$cssfile" ]; then
230	cssfile=$defcssfile
231    fi
232
233    ok="y"
234    silent="t"
235fi
236
237if [ "$silent" = "f" ]; then
238    if [ "$ok" = "y" ] || [ "$ok" = "Y" ]; then
239	echo "Fine, so then ..."
240	echo ""
241    else
242	echo "Please try again"
243	exit 1
244    fi
245fi
246
247# customize a Makefile from the template
248
249case "$doctype" in
250    db31 | db40 | db41) doctypeswitch="db31"
251	                extension="sgml"
252			htmlstylesheet=".dsl"
253			xhtmlstylesheet=".dsl"
254			printstylesheet=".dsl"
255			makeall="all: pdf ps rtf html"
256			dist="dist: pdfdist psdist rtfdist htmldist"
257			transformscript="refdbjade";;
258    db41x | db42x | db43x) doctypeswitch="db31x"
259	                extension="xml"
260			htmlstylesheet=".html.xsl"
261			xhtmlstylesheet=".xhtml.xsl"
262			printstylesheet=".fo.xsl"
263			makeall="all: pdf rtf html"
264			dist="dist: pdfdist rtfdist htmldist"
265			transformscript="refdbxml";;
266    db50x             ) doctypeswitch="db50x"
267	                extension="xml"
268			htmlstylesheet=".html.xsl"
269			xhtmlstylesheet=".xhtml.xsl"
270			printstylesheet=".fo.xsl"
271			makeall="all: pdf rtf html"
272			dist="dist: pdfdist rtfdist htmldist"
273			transformscript="refdbxml";;
274    teix              ) doctypeswitch="teix"
275	                extension="xml"
276			htmlstylesheet=".html.xsl"
277			xhtmlstylesheet=".html.xsl"
278			printstylesheet=".fo.xsl"
279			makeall="all: pdf rtf html"
280			dist="dist: pdfdist rtfdist htmldist"
281			transformscript="refdbxml";;
282    tei5x             ) doctypeswitch="tei5x"
283	                extension="xml"
284			htmlstylesheet=".html.xsl"
285			xhtmlstylesheet=".html.xsl"
286			printstylesheet=".fo.xsl"
287			makeall="all: pdf rtf html"
288			dist="dist: pdfdist rtfdist htmldist"
289			transformscript="refdbxml";;
290esac
291
292# substitute values
293sed "s%<doctypeswitch>%$doctypeswitch%" < $maketemplate | sed "s%<pubtype>%$pubtype%" | sed "s%<basename>%$basename%" | sed "s%<extension>%$extension%" | sed "s%<style>%$style%" | sed "s%<htmlstylesheet>%$htmlstylesheet%" | sed "s%<xhtmlstylesheet>%$xhtmlstylesheet%" | sed "s%<printstylesheet>%$printstylesheet%" | sed "s%<database>%$database%" | sed "s%all: pdf rtf html%$makeall%" | sed "s%dist: pdfdist rtfdist htmldist%$dist%" | sed "s%<transformscript>%$transformscript%"  | sed "s%<encoding>%$encoding%" | sed "s%<cssfile>%$cssfile%" > Makefile
294
295if [ "$silent" = "f" ]; then
296    echo "Makefile created."
297    echo ""
298fi
299
300encodingstring=" encoding=\"$encoding\""
301
302# create an empty short-style document
303if [ -z $xfile ]; then
304    case "$doctype" in
305	db31 ) echo -e "<!DOCTYPE $pubtype PUBLIC \"-//OASIS//DTD DocBook V3.1//EN\" [\n<!ENTITY bibliography SYSTEM \"$basename.bib.$extension\">\n]>\n<$pubtype>\n<!-- include the bibliography at the chapter level by inserting the entity &bibliography; -->\n</$pubtype>\n" > $basename.short.$extension;;
306	db40 ) echo -e "<!DOCTYPE $pubtype PUBLIC \"-//OASIS//DTD DocBook V4.0//EN\" [\n<!ENTITY bibliography SYSTEM \"$basename.bib.$extension\">\n]>\n<$pubtype>\n<!-- include the bibliography at the chapter level by inserting the entity &bibliography; -->\n</$pubtype>\n" > $basename.short.$extension;;
307	db41 ) echo -e "<!DOCTYPE $pubtype PUBLIC \"-//OASIS//DTD DocBook V4.1//EN\" [\n<!ENTITY bibliography SYSTEM \"$basename.bib.$extension\">\n]>\n<$pubtype>\n<!-- include the bibliography at the chapter level by inserting the entity &bibliography; -->\n</$pubtype>\n" > $basename.short.$extension;;
308	db41x) echo -e "<?xml version=\"1.0\"$encodingstring?>\n<!DOCTYPE $pubtype PUBLIC \"-//OASIS//DTD DocBook XML V4.1.2//EN\" \"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd\" [\n<!ENTITY bibliography SYSTEM \"$basename.bib.$extension\">\n]>\n<$pubtype>\n<!-- include the bibliography at the chapter level by inserting the entity &bibliography; -->\n</$pubtype>\n" > $basename.short.$extension;;
309	db42x) echo -e "<?xml version=\"1.0\"$encodingstring?>\n<!DOCTYPE $pubtype PUBLIC \"-//OASIS//DTD DocBook XML V4.2//EN\" \"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\" [\n<!ENTITY bibliography SYSTEM \"$basename.bib.$extension\">\n]>\n<$pubtype>\n<!-- include the bibliography at the chapter level by inserting the entity &bibliography; -->\n</$pubtype>\n" > $basename.short.$extension;;
310	db43x) echo -e "<?xml version=\"1.0\"$encodingstring?>\n<!DOCTYPE $pubtype PUBLIC \"-//OASIS//DTD DocBook XML V4.3//EN\" \"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd\" [\n<!ENTITY bibliography SYSTEM \"$basename.bib.$extension\">\n]>\n<$pubtype>\n<!-- include the bibliography at the chapter level by inserting the entity &bibliography; -->\n</$pubtype>\n" > $basename.short.$extension;;
311	db50x) echo -e "<?xml version=\"1.0\"$encodingstring?>\n<$pubtype xmlns=\"http://docbook.org/ns/docbook\">\n<!-- xinclude the bibliography file $basename.bib.xml at the chapter level -->\n<!-- <xi:include href=\"$basename.bib.xml\" xmlns:xi=\"http://www.w3.org/2001/XInclude\">\n<xi:fallback>$basename.bib.xml appears to be missing</xi:fallback>\n</xi:include> -->\n</$pubtype>\n" > $basename.short.$extension;;
312	teix ) echo -e "<?xml version=\"1.0\"$encodingstring?>\n<!DOCTYPE TEI.2 PUBLIC \"-//TEI P4//DTD Main DTD Driver File//EN\" \"http://www.tei-c.org/P4X/DTD/tei2.dtd\" [\n<!ENTITY % TEI.general 'INCLUDE'>\n<!ENTITY % TEI.names.dates 'INCLUDE'>\n<!ENTITY % TEI.linking 'INCLUDE'>\n<!ENTITY % TEI.XML 'INCLUDE'>\n<!ENTITY bibliography SYSTEM \"$basename.bib.$extension\">\n]>\n<$pubtype>\n<!-- include the bibliography at the chapter level by inserting the entity &bibliography; -->\n</$pubtype>\n" > $basename.short.$extension;;
313	tei5x) echo -e "<?xml version=\"1.0\"$encodingstring?>\n<$pubtype xmlns=\"http://www.tei-c.org/ns/1.0\">\n<!-- xinclude the bibliography file $basename.bib.xml at the chapter level -->\n<!-- <xi:include href=\"$basename.bib.xml\" xmlns:xi=\"http://www.w3.org/2001/XInclude\">\n<xi:fallback>$basename.bib.xml appears to be missing</xi:fallback>\n</xi:include> -->\n</$pubtype>\n" > $basename.short.$extension;;
314    esac
315
316    # create the full-style document
317    cp $basename.short.$extension $basename.$extension
318
319    if [ "$silent" = "f" ]; then
320	echo "Documents $basename.short.$extension and $basename.$extension created."
321    fi
322else
323    if [ "$silent" = "f" ]; then
324	echo "Your existing document is called $xfile."
325    fi
326fi
327
328if [ "$silent" = "f" ]; then
329    echo ""
330    echo "After editing this file you can use the following commands to create"
331    echo "formatted output:"
332    echo "make pdf to create a Portable Document Format (PDF) file ($basename.pdf)"
333    if [ "$doctype" = "db41x" ] || [ "$doctype" = "db42x" ] || [ "$doctype" = "db43x" ] || [ "$doctype" = "db50x" ]; then
334	echo "make html to create HTML output ($basename.html)"
335	echo "make xhtml to create XHTML output ($basename.xhtml)"
336    else
337	echo "make html to create HTML output (${pubtype}1.html)"
338    fi
339    if [ "$extension" = "sgml" ]; then
340	echo "make rtf to create a Rich Text Format (RTF) file ($basename.rtf)"
341	echo "make ps to create a Postscript file ($basename.ps)"
342    fi
343    echo "make all to create all available output formats"
344fi
345
346exit 0
347