#!/bin/sh
# db2ris - converts DocBook BiblioEntry elements into RIS datasets
# Markus Hoenicka <markus@mhoenicka.de> 010927
# $Id: db2ris.in,v 1.4 2003/02/26 23:27:09 mhoenicka Exp $
# OPTIONS: -a (long firstnames) -h (invoke help) -o outfile -O outfile
#          -r (default reference type)
# relies on these external programs: (open)jade sed
# tries to use these environment variables: $REFDBLIB $HOME

### start user-customizable section
# path to the db2ris stylesheet
db2risdsssl="/usr/share/refdb/dsssl/db2ris.dsl"

# the Jade/OpenJade command. You should use OpenJade whenever possible. Jade
# cannot read arbitrary variable values from the command line so you cannot
# use configuration files or the -a and -r command line switches. To configure
# this script for use with Jade, edit the initialization for authorlong
# and defaultreftype accordingly
myjade="/usr/bin/openjade"


# the SGML declaration
sgmldecl="/usr/share/refdb/declarations/docbook.dcl"

# the default destination for log output. 0 means stderr, 2 means
# a custom log file defined in $logfile
logdest=0

# the path of the custom log file. To use this, set $logdest to 2
logfile="/var/log/db2ris"

# the path of the global configuration file
globalconfig="/etc/refdb"

### end user-customizable section

# initialize variables
outfile=""
truncate='f'
authorlong=""
defaultreftype=""
allconfigs=""
globalconfig=""
varstring=""

# determine configuration files
if [ ! -r "$globalconfig" ] && [ -n "$REFDBLIB" ]; then
    globalconfig=$REFDBLIB/db2risrc
fi
userconfig=$HOME/.db2risrc

# first try the hidden file, then the plain file
if [ ! -r "$userconfig" ]; then
    userconfig=$HOME/db2risrc
fi

if [ -n "$globalconfig" ] && [ -r "$globalconfig" ]; then
    allconfigs=$globalconfig
fi

if [ -r "$userconfig" ]; then
    allconfigs=$allconfigs" "$userconfig
fi

# read the settings in the configure file(s)
for config in $allconfigs; do
    while read refdbvar refdbval; do
	if [ -n "$refdbvar" ]; then
	    if [ $refdbvar = authorlong ]; then
		authorlong=$refdbval
	    fi
	    if [ $refdbvar = defaultreftype ]; then
		defaultreftype=$refdbval
	    fi
	    if [ $refdbvar = logdest ]; then
		logdest=$refdbval
	    fi
	    if [ $refdbvar = logfile ]; then
		logfile=$refdbval
	    fi
	fi
    done < $config
done

# read the command line options
while getopts ":ae:hL:o:O:r:" opt; do
  case $opt in
    a  ) authorlong="t";;
    e  ) logdest=$OPTARG;;
    h  ) echo "converts DocBook BiblioEntry elements into RIS datasets"
	 echo 'usage: db2ris [-a] [-e dest] [-h] [-L logfile] [-o outfile] [-O outfile] [-r type] file1 [file2...]'
	 echo "Options: -a use long author firstnames"
         echo "         -e log destination. 0 means stderr, 2 means custom file"
         echo "         -h print this help and exit"
         echo "         -L path of a custom log file"
	 echo "         -o send output to file"
	 echo "         -O append output to file"
	 echo "         -r set default reference type"
	 exit 0 ;;
    L  ) logfile=$OPTARG;;
    o  ) outfile=$OPTARG
	 truncate='t';;
    O  ) outfile=$OPTARG;;
    r  ) defaultreftype=$OPTARG;;
    \? ) echo 'usage: db2ris [-a] [-e dest] [-h] [-L logfile] [-o outfile] [-O outfile] [-r type] file1 [file2...]'
	 echo 'type db2ris -h to invoke help'
	 exit 1;;
  esac
done

# correct the index so the filename argument is always $1
shift $(($OPTIND - 1))

# assemble the OpenJade variable string
if [ -n "$authorlong" ]; then
    varstring=" -V AUTHORLONG="$authorlong
fi
if [ -n "$defaultreftype" ]; then
    varstring=$varstring" -V DEFAULTREFTYPE="$defaultreftype
fi

# truncate the output file if requested. We cannot use the redirect and append
# operators in the command below because the former would not make much sense
# if several files are specified on the command line. The difference between
# overwriting and appending in this script is that the output of all files is
# appended to an empty file in the first case and to a possibly existing file
# in the second case.
if [ -n "$outfile" ] && [ $truncate = "t" ]; then
    # poor man's truncate(1)
    echo "" > $outfile
fi

for filename in $*; do
    if [ -n "$outfile" ]; then
	if [ $logdest = 2 ] && [ -n "$logfile" ]; then
	    $myjade -t sgml -d $db2risdsssl $varstring $sgmldecl $filename 2>>$logfile | sed 'N;s%^>*<db2ris\n>\(.*\)</db2ris$%\1%g' | sed 's%^>$%%g' >> $outfile
	else
	    $myjade -t sgml -d $db2risdsssl $varstring $sgmldecl $filename | sed 'N;s%^>*<db2ris\n>\(.*\)</db2ris$%\1%g' | sed 's%^>$%%g' >> $outfile
	fi
    else
	if [ $logdest = 2 ] && [ -n "$logfile" ]; then
	    $myjade -t sgml -d $db2risdsssl $varstring $sgmldecl $filename 2>>$logfile | sed 'N;s%^>*<db2ris\n>\(.*\)</db2ris$%\1%g' | sed 's%^>$%%g'
	else
	    $myjade -t sgml -d $db2risdsssl $varstring $sgmldecl $filename | sed 'N;s%^>*<db2ris\n>\(.*\)</db2ris$%\1%g' | sed 's%^>$%%g'
	fi
    fi
done

exit 0
