#!/bin/sh

# refdbnd: creates a new document and sets up a Makefile for use
# with RefDB bibliographies
# Invocation: refdbnd basename doctype|filename pubtype database style encoding fo-driver html-driver xhtml-driver
#         or: refdbnd (interactive mode)

# markus@mhoenicka.de 2002-11-26

# 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, see <http://www.gnu.org/licenses/>

# user variable defaults
defbasename="refdbtest"
defdoctype="db43x"
defpubtype="book"
defdatabase="refdbtest"
defstyle="J.Biol.Chem."
defencoding="utf-8"
defcssfile=""
xfile=""
encoding=""
cssfile=""
silent="f"
myfo=""
myhtml=""
myxhtml=""

fodriveropt=""
htmldriveropt=""
xhtmldriveropt=""


check_existing_doc() {
    if [ -e $basename ]; then
        # guess values from existing file
	xfile=$basename

	if grep "^<!DOCTYPE.*DTD DocBook V3.1//EN" $basename; then
	    doctype="db31"
	    pubtype=`grep "DTD DocBook V3.1//EN" $basename | sed 's/^<!DOCTYPE \(.*\) PUBLIC.*/\1/'`
	else 
	    if grep "^<!DOCTYPE.*DTD DocBook V4.0//EN" $basename; then
		doctype="db40"
		pubtype=`grep "DTD DocBook V4.0//EN" $basename | sed 's/^<!DOCTYPE \(.*\) PUBLIC.*/\1/'`
	    else
		if grep "^<!DOCTYPE.*DTD DocBook V4.1//EN" $basename; then
		    doctype="db41"
		    pubtype=`grep "DTD DocBook V4.1//EN" $basename | sed 's/^<!DOCTYPE \(.*\) PUBLIC.*/\1/'`
		else
		    if grep "^<!DOCTYPE.*DTD DocBook XML V4.1.2//EN" $basename; then
			doctype="db41x"
			pubtype=`grep "DTD DocBook XML V4.1.2//EN" $basename | sed 's/^<!DOCTYPE \(.*\) PUBLIC.*/\1/'`
			encoding=`grep "^<?xml version=\"1.0\"" $basename | sed 's/^.*encoding=\"\(.*\)\".*/\1/'`
		    else
			if grep "^<!DOCTYPE.*DTD DocBook XML V4.2//EN" $basename; then
			    doctype="db42x"
			    pubtype=`grep "DTD DocBook XML V4.2//EN" $basename | sed 's/^<!DOCTYPE \(.*\) PUBLIC.*/\1/'`
			    encoding=`grep "^<?xml version=\"1.0\"" $basename | sed 's/^.*encoding=\"\(.*\)\".*/\1/'`
			else
			    if grep "^<!DOCTYPE.*DTD DocBook XML V4.2//EN" $basename; then
				doctype="db43x"
				pubtype=`grep "DTD DocBook XML V4.3//EN" $basename | sed 's/^<!DOCTYPE \(.*\) PUBLIC.*/\1/'`
				encoding=`grep "^<?xml version=\"1.0\"" $basename | sed 's/^.*encoding=\"\(.*\)\".*/\1/'`
			    else
				if grep "^<!DOCTYPE.*TEI P4//DTD Main DTD Driver File//EN" $basename; then
				    doctype="teix"
				    pubtype=`grep "TEI P4//DTD Main DTD Driver File//EN" $basename | sed 's/^<!DOCTYPE \(.*\) PUBLIC.*/\1/'`
				    encoding=`grep "^<?xml version=\"1.0\"" $basename | sed 's/^.*encoding=\"\(.*\)\".*/\1/'`
				else
				    if grep "xmlns.*\"http://www.tei-c.org/ns/1.0\"" $basename; then
					doctype="tei5x"
					pubtype="TEI"
					encoding=`grep "^<?xml version=\"1.0\"" $basename | sed 's/^.*encoding=\"\(.*\)\".*/\1/'`
				    else
					if grep "^\{\\rtf1" $basename; then
					    doctype="rtf"
					    pubtype="rtf"
					    encoding=`grep "^\{\\rtf1" $basename | sed 's/^\{\\rtf1\\\([^\\]+\)\\/\1/'`
					fi
				    fi
				fi
			    fi
			fi
		    fi
		fi
	    fi
	fi
	basename=${xfile%%.*}
    fi
}

# if we have at least six command line arguments (cssfile and driver
# files are optional), we'll use them. Otherwise ask interactively
if [ -z "$*" ] || [ "$#" -lt 6 ]; then
    # get arguments interactively
    clear
    echo "I'll be happy to assist you in setting up a new document along with"
    echo "a Makefile. First we'll collect a few answers, and only if you"
    echo "accept your settings any files will be created. Press Ctrl-C anytime"
    echo "to exit."
    echo ""
    echo "Each question will present a default value which you can accept by"
    echo "pressing ENTER"
    echo ""
    echo "Please enter the basename of your document. This is the name without"
    echo "any suffix. For example, if your printed output file is supposed to"
    echo "be called refdbtest.pdf, the basename will be 'refdbtest'"
    echo "Alternatively, enter the full name of an existing document in this"
    echo "directory. I'll try and guess the proper values for this document."
    echo "[$defbasename]"
    read basename
    if [ -z "$basename" ]; then
	basename=$defbasename
    fi

    check_existing_doc

    if [ -z $doctype ]; then
	echo ""
	echo "Please enter the type of the document. Available types are 'db31',"
	echo "'db40', and 'db41' for DocBook SGML versions 3.1, 4.0, and 4.1,"
	echo "respectively, 'db41x', 'db42x', 'db43x', and 'db50x' for DocBook XML"
	echo "versions 4.1.2, 4.2, 4.3, and 5.0, respectively, "
	echo "'teix' and 'tei5x' for TEI XML P4 and TEI XML P5, respectively,"
	echo "and 'rtf' for Rich Text Format (RTF)."
	echo "[$defdoctype]"
	read doctype

	if [ ! "$doctype" = "rtf" ]; then
	    echo ""
	    echo "Please enter the element root which determines the type of the"
	    echo "publication. Common are 'set', 'book', and 'article' for DocBook"
	    echo "documents and 'TEI.2' for TEI documents"

        # use TEI.2 as default for TEI documents. $defpubtype defaults to 'book'
	    if [ "$doctype" = "teix" ]; then
		defpubtype="TEI.2"
	    else
		if [ "$doctype" = "tei5x" ]; then
		    defpubtype="TEI"
		fi
	    fi
	    echo "[$defpubtype]"
	    read pubtype
	else
	    pubtype="rtf"
	fi
    fi

    echo ""
    echo "Please enter the name of the RefDB database where you take your"
    echo "references from"
    echo "[$defdatabase]"
    read database
    echo ""
    echo "Please enter the bibliography style that your document should use"
    echo "Enter 'raw' to use the default formatting of the stock stylesheets"
    echo "[$defstyle]"
    read style
    echo ""
    if [ -z $encoding ]; then
	echo "Please enter the character encoding that your document should use"
	echo "[$defencoding]"
	read encoding
	echo ""
    fi

    if [ -z "$style" ]; then
	style=$defstyle
    fi

    # mangle stylename to generate the name of the CSS file
    defcssfile=${style%.*}.css

    if [ "$doctype" != "rtf" ]; then
	echo "Please enter the path or the URL of a custom CSS file for the"
	echo "(x)html output. Hit ENTER if you do not use a custom CSS file"
	echo "[$defcssfile]"
	read cssfile
	echo ""

	echo "Please enter the path of a custom XSL driver file for the"
	echo "printable (fo) output. Hit ENTER to use the default driver file"
	read myfo
	echo ""

	echo "Please enter the path of a custom XSL driver file for the"
	echo "html output. Hit ENTER to use the default driver file"
	read myhtml
	echo ""

	echo "Please enter the path of a custom XSL driver file for the"
	echo "xhtml output. Hit ENTER to use the default driver file"
	read myxhtml
	echo ""
    fi

    # apply defaults to empty strings
    if [ -z "$basename" ]; then
	basename=$defbasename
    fi
    if [ -z "$doctype" ]; then
	doctype=$defdoctype
    fi
    if [ -z "$pubtype" ]; then
	pubtype=$defpubtype
    fi
    if [ -z "$database" ]; then
	database=$defdatabase
    fi
    if [ -z "$encoding" ]; then
	encoding=$defencoding
    fi
    if [ -z "$cssfile" ]; then
	cssfile=$defcssfile
    fi

    if [ -n "$myfo" ]; then
	fodriveropt=" -a $myfo "
    fi
    if [ -n "$myhtml" ]; then
	htmldriveropt=" -b $myhtml "
    fi
    if [ -n "$myxhtml" ]; then
	xhtmldriveropt=" -c $myxhtml "
    fi

    echo "You've selected the following values:"
    echo "Basename:           $basename"
    echo "Document type:      $doctype"
    if [ "$doctype" != "rtf" ]; then
	echo "Publication type:   $pubtype"
    fi
    echo "Database:           $database"
    echo "Bibliography style: $style"
    echo "Encoding:           $encoding"
    if [ "$doctype" != "rtf" ]; then
	echo "CSS file:           $cssfile"
	if [ -n "$myfo" ]; then
	    echo "Fo driver file:     $myfo"
	fi
	if [ -n "$myhtml" ]; then
	    echo "HTML driver file:   $myhtml"
	fi
	if [ -n "$myxhtml" ]; then
	    echo "XHTML driver file:  $myxhtml"
	fi
    fi

    echo ""
    echo "Is this ok?"
    echo "[y]"
    read ok

    if [ -z "$ok" ]; then
	ok="y"
    fi

else
    # all arguments were specified on the command line
    basename=$1
    doctype=$2
    pubtype=$3
    check_existing_doc
    database=$4
    style=$5
    encoding=$6
    cssfile=$7
    myfo=$8
    myhtml=$9
    shift
    myxhtml=$9

    # mangle stylename to generate the name of the CSS file
    defcssfile=${style%.*}.css
    if [ -z "$cssfile" ]; then
	cssfile=$defcssfile
    fi

    ok="y"
    silent="t"
fi

if [ "$silent" = "f" ]; then
    if [ "$ok" = "y" ] || [ "$ok" = "Y" ]; then
	echo "Fine, so then ..."
	echo ""
    else 
	echo "Please try again"
	exit 1
    fi
fi

# customize a Makefile from the template
if [ "$doctype" = "rtf" ]; then
    maketemplate="<refdblib>/examples/Makefile.rtf.template"
else
    maketemplate="<refdblib>/examples/Makefile.template"
fi

case "$doctype" in
    db31 | db40 | db41) doctypeswitch="db31"
	                extension="sgml"
			htmlstylesheet=".dsl"
			xhtmlstylesheet=".dsl"
			printstylesheet=".dsl"
			makeall="all: pdf ps rtf html"
			dist="dist: pdfdist psdist rtfdist htmldist"
			transformscript="refdbjade";;
    db41x | db42x | db43x) doctypeswitch="db31x"
	                extension="xml"
			htmlstylesheet=".html.xsl"
			xhtmlstylesheet=".xhtml.xsl"
			printstylesheet=".fo.xsl"
			makeall="all: pdf rtf html"
			dist="dist: pdfdist rtfdist htmldist"
			transformscript="refdbxml";;
    db50x             ) doctypeswitch="db50x"
	                extension="xml"
			htmlstylesheet=".html.xsl"
			xhtmlstylesheet=".xhtml.xsl"
			printstylesheet=".fo.xsl"
			makeall="all: pdf rtf html"
			dist="dist: pdfdist rtfdist htmldist"
			transformscript="refdbxml";;
    teix              ) doctypeswitch="teix"
	                extension="xml"
			htmlstylesheet=".html.xsl"
			xhtmlstylesheet=".html.xsl"
			printstylesheet=".fo.xsl"
			makeall="all: pdf rtf html"
			dist="dist: pdfdist rtfdist htmldist"
			transformscript="refdbxml";;
    tei5x             ) doctypeswitch="tei5x"
	                extension="xml"
			htmlstylesheet=".html.xsl"
			xhtmlstylesheet=".html.xsl"
			printstylesheet=".fo.xsl"
			makeall="all: pdf rtf html"
			dist="dist: pdfdist rtfdist htmldist"
			transformscript="refdbxml";;
    rtf               ) doctypeswitch="rtf"
	                extension="rtf"
			makeall="all: rtf"
			dist="dist: rtfdist"
			transformscript="refdbrtf";;
esac

# substitute values
sed "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%" | sed "s%<fodriveropt>%$fodriveropt%" | sed "s%<htmldriveropt>%$htmldriveropt%" | sed "s%<xhtmldriveropt>%$xhtmldriveropt%" > Makefile

if [ "$silent" = "f" ]; then
    echo "Makefile created."
    echo ""
fi

encodingstring=" encoding=\"$encoding\""

# create an empty short-style document
if [ -z $xfile ]; then
    case "$doctype" in
	db31 ) printf "<!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;;
	db40 ) printf "<!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;;
	db41 ) printf "<!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;;
	db41x) printf "<?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;;
	db42x) printf "<?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;;
	db43x) printf "<?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;;
	db50x) printf "<?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;;
	teix ) printf "<?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;;
	tei5x) printf "<?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;;
	# must use quadruple backslashes. One is eaten by the shell, the
	# other one by the echo command due to the -e switch (which we
	# need for inserting newlines)
	# also, use ansi as default encoding for the time being instead
	# of $encoding
	rtf) printf "{\\\\rtf1\\\\ansi\n\\\\deff1\n{\\\\fonttbl\n{\\\\f0\\\\froman\\\\fcharset0\\\\fprq2 Times New Roman;}\n{\\\\f1\\\\froman\\\\fcharset0\\\\fprq2 Times New Roman;}\n{\\\\f2\\\\fswiss\\\\fcharset0\\\\fprq2 Lucida Sans Unicode;}}\n}\n" > $basename.$extension;;
    esac

    # create the full-style document
    if [ "$doctype" != "rtf" ]; then
	cp $basename.short.$extension $basename.$extension
	if [ "$silent" = "f" ]; then
	    echo "Documents $basename.short.$extension and $basename.$extension created."
	fi
    else
	if [ "$silent" = "f" ]; then
	    echo "Document $basename.$extension created."
	fi
    fi

else
    if [ "$silent" = "f" ]; then
	echo "Your existing document is called $xfile."
    fi
fi

if [ "$silent" = "f" ]; then
    echo ""
    if [ "$doctype" = "rtf" ]; then
	echo "After editing this file you can type \"make\" to generate $basename.refdb.$extension"
    else
	echo "After editing this file you can use the following commands to create"
	echo "formatted output:"
	echo "make pdf to create a Portable Document Format (PDF) file ($basename.pdf)"
	if [ "$doctype" = "db41x" ] || [ "$doctype" = "db42x" ] || [ "$doctype" = "db43x" ] || [ "$doctype" = "db50x" ]; then
	    echo "make html to create HTML output ($basename.html)"
	    echo "make xhtml to create XHTML output ($basename.xhtml)"
	else
	    echo "make html to create HTML output (${pubtype}1.html)"
	fi
	if [ "$extension" = "sgml" ]; then
	    echo "make rtf to create a Rich Text Format (RTF) file ($basename.rtf)"
	    echo "make ps to create a Postscript file ($basename.ps)"
	fi
	echo "make all to create all available output formats"
    fi
fi

exit 0
