#!/bin/sh
#####################################################################
# bib2blink - Convert BiBTeX online citations to blink files
# Created 2010-09-09 by David Meyer <papa@freeshell.org>  +JMJ
#####################################################################

# Identification ####################################################

PROGRAM='bib2blink'
VERSION=0.0
COPYRIGHT='Copyright (C) 2010 David Meyer'
DESCRIPTION='Convert BiBTeX online citations to blink files'
USAGE="Usage: $PROGRAM [OPTIONS] [ARGUMENTS]"
CONTACT='David Meyer <papa@freeshell.org>'

# Initialize environment ############################################

unset IFS
PATH=/bin:/usr/bin

# Arguments #########################################################

while getopts Vh option
do	case $option in
	V )	echo "$PROGRAM $VERSION"
		echo $COPYRIGHT
		exit 0
		;;
	h )	cat << ENDHELP
$USAGE

$DESCRIPTION

Options are as follows:
...
-V	Display version number
-h	Display this help message

Report bugs to $CONTACT.
ENDHELP
		exit 0
		;;
	* )	echo $USAGE >&2
		exit 1
		;;
	esac
done

shift $(( $OPTIND-1 ))


# Functions #########################################################


# Main driver #######################################################

cat $1 | while read bibline; do
    case $bibline in
	 @ONLINE* ) online=1 ;;
	 *=* ) 
	    val=$(echo ${bibline##*=}|sed -e 's;[{}"];;g' -e 's;^ *;;' -e 's;, *$;;')
	    case $bibline in
		author* ) author=$val ;;
		booktitle* ) booktitle=$val ;;
		chapter* ) chapter=$val ;;
		institution* ) institution=$val ;;
		month* ) month=$val ;;
		note* ) note=$val ;;
		publisher* ) publisher=$val ;;
		title* ) title=$val ;;
		url* ) url=$val ;;
		year* ) year=$val ;;
	    esac
	    ;;
    esac

done
echo "online=$online"
echo "author=$author"
echo "booktitle=$booktitle"
echo "chapter=$chapter"
echo "institution=$institution"
echo "month=$month"
echo "note=$note"
echo "publisher=$publisher"
echo "title=$title"
echo "url=$url"
echo "year=$year"



exit 0