shithub: 9intro

ref: 2b99422480d596ebc26921c87c6bb81a07949f3e
dir: /bib/

View raw version
#!/bin/rc
rfork e

bibfile=bibfile

oifs=$"ifs
nifs=' 	'

rifs=' 	
'
nlifs='
'
got=''

track=()
idents=()
auths=()
titles=()
arts=()
others=()
ind=()

fn checkgot {
	got=0
	for (chk in `{seq 1 $#idents}) {
		if (~ $1 $idents($chk) ) 
			got=$chk
	}
}

fn getbib {
	authors=()
	title=''
	str=()
	aut=''
	other=''
	ifs=$nlifs
	for (i in `{echo $*}) {
		if (~ $i [%]A*) {
			author=`{echo $i | sed s/%A.//}
			if (~ $#authors 0)
				authors=($author)
			if not authors=($authors $author)
		}
		if not { 
			if (~ $i [%]T*)
				title=`{echo $i | sed s/%T.//}	
			if not if (~ $i [%]O*)
				other=`{echo $i | sed s/%O.//}
			if not if (! ~ $i [%]K* ) {
				strpart=`{echo $i | sed s/%[A-Z].//}
				strpart=$strpart^'.'
				if (~ $#str 0)
					str=($strpart)
				if not str=($str $strpart)
			}	
		}
	}

	aut=$authors(1)
	if (! ~ $#authors 1 ) {
		for (i in `{seq 2 $#authors}) {
			if (~ $i $#authors)		
				sep=' and '
			if not sep=', '

			aut=$aut^$sep^$authors($i)
		}
	}
	if (~ $#track 1) {
		auths=($aut)
		titles=($title)
		arts=($"str)
		others=($other)
	}
	if not {
		auths=($auths $aut)
		titles=($titles $title)
		arts=($arts $"str)
		others=($others $other)
	}
}

fn lookup {
	ifs=$nifs
	search=`{grep '^%K' $bibfile}
	for (part in $*) 		
		search=`{echo $search | grep -i '*'$part}		
	if (~ $#search 0) 
		echo No match for $* in $bibfile >[1=2]
	if not {
		lines=`{echo $search | wc -l | awk '{gsub(" ", "", $0); printf("%s", $0)}'}
		if (~ $lines 2) {
			ifs=$rifs
			search=`{echo $search}
			
			ident=`{echo $search | sum | awk '{printf("%s", $1)}'}
			checkgot $ident
			if (~ $got 0) {
				if(~ $#track 0) track=('1')
				if not track=($track '1')

				if (~ $#idents 0) 
					idents=($ident)
				if not 
					idents=($idents $ident)
				ifs=' '	
				bib=`{awk '/'^$"search'/,/^$/' bibfile}
				getbib $"bib
				ref=$#idents
			}
			if not {
				ref=$got
			}
		}
		if not echo Multiple matches for "$*" in $bibfile >[1=2]
	}
}


fn getlookup {
	lb=`{echo $* | awk '/\[\./ { split($0, a, ".");printf("%s", a[2])}'}
	ifs=' '
	slb=`{echo $lb}
	lookup $slb
	}

fn dodump {
	echo .SH 
	echo References
	for (i in `{seq 1 $#track}) {
		echo .IP $i
		echo $auths($i)^': '
		echo '.I'
		echo $titles($i)^'.'
		echo '.R'
		echo $arts($i)
		echo '.br'
		echo $others($i)
		}
}

while (line=`{read}) {
	ifs=$nlifs
	if (~ $line *[\[][\.]*[\.]]*)  {
		getlookup $line
		echo $line | sed s/\[\..*\.\]/\[$ref\]/
	}
	if not { 
		if (~ $line *[\.][\[]]) { 
			dodump
		}
		if not {
			echo $line
		}
	}
}