#!/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.3 2004/09/17 19:59:27 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="db41"
defpubtype="book"
defdatabase="refdbtest"
defstyle="J.Biol.Chem."
defencoding="utf-8"
defcssfile=""
xfile=""
encoding=""
cssfile=""

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

# if we have seven command line arguments, we'll use them. Otherwise
# ask interactively
if [ -z "$*" ] || [ "$#" -ne 7 ]; 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' and 'db42x' for DocBook XML versions 4.1.2 and"
	echo "4.2, respectively, and 'teix' for TEI XML P4"
	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"
	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 "[$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

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

if [ "$ok" = "y" ] || [ "$ok" = "Y" ]; then
    echo "Fine, so then ..."
    echo ""
else 
    echo "Please try again"
    exit 1
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     ) 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";;
    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";;
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

echo "Makefile created."
echo ""

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

# create an empty 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]>" > $basename.short.$extension;;
	db40 ) echo -e "<!DOCTYPE $pubtype PUBLIC \"-//OASIS//DTD DocBook V4.0//EN\" [\n<!ENTITY bibliography SYSTEM \"$basename.bib.$extension\">\n]>" > $basename.short.$extension;;
	db41 ) echo -e "<!DOCTYPE $pubtype PUBLIC \"-//OASIS//DTD DocBook V4.1//EN\" [\n<!ENTITY bibliography SYSTEM \"$basename.bib.$extension\">\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]>" > $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]>" > $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]>" > $basename.short.$extension;;
    esac
    echo "Document $basename.short.$extension created."
else
    echo "Your existing document is called $xfile."
fi


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" ]; 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
echo "make rtf to create a Rich Text Format (RTF) file ($basename.rtf)"
if [ "$extension" = "sgml" ]; then
    echo "make ps to create a Postscript file ($basename.ps)"
fi
echo "make all to create all available output formats"

exit 0
