#!/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
#         or: refdbnd (interactive mode)

# markus@mhoenicka.de 2002-11-26
# $Id: refdbnd.in,v 1.8.2.5 2005/07/29 21:21:50 mhoenicka Exp $

# 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

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

# Makefile-generated defaults
maketemplate="/usr/share/refdb/examples/Makefile.template"

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/'`
				    fi
				fi
			    fi
			fi
		    fi
		fi
	    fi
	fi
	basename=${xfile%%.*}
    fi
}

# if we have at least six command line arguments (cssfile is 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, as well as"
	echo "'teix' and 'tei5x' for TEI XML P4 and TEI XML P5, respectively"
	echo "[$defdoctype]"
	read doctype

	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
    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

    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 ""

    # 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

    echo "You've selected the following values:"
    echo "Basename:           $basename"
    echo "Document type:      $doctype"
    echo "Publication type:   $pubtype"
    echo "Database:           $database"
    echo "Bibliography style: $style"
    echo "Encoding:           $encoding"
    echo "CSS file:           $cssfile"

    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

    # 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

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";;
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%" > 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 ) 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;;
	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;;
	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;;
	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;;
	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;;
	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;;
	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;;
	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;;
	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;;
    esac

    # create the full-style document
    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 "Your existing document is called $xfile."
    fi
fi

if [ "$silent" = "f" ]; then
    echo ""
    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

exit 0
