###
### apa reference formatting style
###

BEGIN	{
	PASS="";		# only one pass thru document
	sortflag = 1;		# reference_placement bib is sortedone
	STYLE = "apa";
	SKEY = "adt";
}



#######################################################
#
# Functions required by general awk script
#
#######################################################


##
## Set author and editor fields
##	for reference list
##	for citation
##	for possible sorting
##
func setauthor(b,a) {
	Afull[b,a] = LastF_M();
	Alast[b,a] = Last;
	Asort[b] = Asort[b] Afull[b,a] " ";
}

func seteditor(b,a) {
	E[b,a] = F_MLast();
	Efull[b,a] = LastF_M();
	Elast[b,a] = Last;
	Esort[b] = Esort[b] Efull[b,a] " ";
}


##
## citations
##	print a citation list
##	(Last, year; Last, year...)
##
func citations(lastpunc) {
	sep = "\n(";
	if(lastpunc == "\"" || lastpunc == "'") {
		sep = lastpunc " (";
		lastpunc = "\\ ";
	}
	ptext = 0;
	ctext = 0;
	for(i=1; i<=cites; i++) {
		cn = bibptr[i];
		b = uniqno[cn];
		if (pre_text[cn] || post_text[cn]) ctext = 1;
		punc = ", ";
		if ((ctext == 1) || (ptext == 1) || new_author(i)) {
			printf sep;
			ppunc = "";
			if (pre_text[cn]) {
				printf "%s", pre_text[cn];
				if((no_author[cn] == 0) || (no_date[cn] == 0))
					printf " ";
			}	
			if (no_author[cn] == 0) {
				if(authors[b])
					printlast(b);
				else if (editors[b])
					printlasted(b);
				else if (issuer[b])
					printf issuer[b];
				else
					printf title[b];
			}
			else punc = "";
		}
		if(no_date[cn] == 0) {
			if(year[b] ~ /^[0-9][0-9][0-9][0-9]$/)
				printf "%s%s", punc, year[b];
			else {
				if (date[b] != 0) printf "%s%s", punc, date[b];
			}
		}
		if (post_text[cn]) {
			if (is_punc(substr(post_text[cn],1,1)) == 0) print "";
			printf "%s", post_text[cn];
		}
		sep = ";\n";
		ptext = ctext;
		ctext = 0;
	}
	printf ")";
	printf lastpunc;
}

func is_punc(p)
{
	if(p == ",") return 1;
	if(p == ".") return 1;
	if(p == ";") return 1;
	return 0;
}

##
## new_author
##	returns 1 if current author is difference from
##	previous author in the citation list
##
func new_author(c) {
	if (c == 1) return 1;
	b1 = uniqno[bibptr[c-1]];
	b = uniqno[bibptr[c]];
	if(authors[b] == 0) return 1;
	if(authors[b1] != authors[b]) return 1;
	for(k = 1; k <= authors[b]; k++) {
		if (Alast[b1,k] != Alast[b,k]) return 1;
	}
	return 0;
}


##
## print author last name for citation
##	First
##	First and Second
##	First, Second,..., & Last
func printlast(c) {			
        a = authors[c]			
	if ( a == 1 ) printf "%s", Alast[c,1];
	else if ( a == 2 ) printf "%s & %s", Alast[c,1], Alast[c,2];
	else if (a > 5) printf "%s et al.", Alast[c,1];
	else {
		for ( j = 1; j < a; j++ ) printf Alast[c,j] ", ";
		printf "& %s", Alast[c,a];
	}
}

##

## print editor last name for citation
##	First
##	First and Second
##	First, Second,..., & Last
func printlasted(c) {			
        a = editors[c]			
	if ( a == 1 ) printf "%s", Elast[c,1];
	else if ( a == 2 ) printf "%s & %s", Elast[c,1], Elast[c,2];
	else if (a > 5) printf "%s et al.", Elast[c,1] ;
	else {
		for ( j = 1; j < a; j++ ) printf Elast[c,j] ", ";
		printf "& %s", Elast[c,a];
	}
}


##
## bibindex
##	set up indentation for bibliography;
##	return an index for accessing rest of citation
##
func bibindex(i) {
	printf ".sp .5\n.ti -.5i\n";
	if (sortflag)
		return sortorder[i];
	else return i;
}



#############################################
#
# Reference types
#
############################################


func book(b) {
	# authorlist
			if(authors[b]) {
				printfull(b);
				printf "\n";
			}
	# (date).
			printf "(%s)", date[b] ;
			punc = ".\n";
	# italic(title)
			if(title[b]) {
				printf "%s\\f2\\&%s\\f1\\&", punc, title[b];
	# (Vol #).
				if(volstr[b])
					printf " (%s)", volstr[b];
				else if(volume[b])
					printf " (Vol. %s)", volume[b];
			}
	# location: publisher.
			if (city[b]) {
				printf "%s%s", punc, city[b] ;
				punc = ": ";
			}
			printf "%s%s", punc, issuer[b] ;
}


func editedbook(b) {
	# authorlist
			if(authors[b]) {
					printfull(b);
				if(authors[b] <2)
					printf " (Ed.).\n";
				else
					printf " (Eds.).\n";
			}			
	# (date).
			printf "(%s)", date[b] ;
			punc = ".\n";
	# italic(title)
			if(title[b]) {
				printf "%s\\f2\\&%s\\f1\\&", punc, title[b];
	# (Vol #).
				if(volstr[b])
					printf " (%s)", volstr[b];
				else if(volume[b])
					printf " (Vol. %s)", volume[b];
			}
	# location: publisher.
			if (city[b]) {
				printf "%s%s", punc, city[b] ;
				punc = ": ";
			}
			printf "%s%s", punc, issuer[b] ;
}


func thesis(b) {

	# authorlist
			if(authors[b]) {
				printfull(b);
				printf "\n";
			}
	# (date).
			printf "(%s).\n", date[b] ;
	# italic(title).
			if(title[b]) {
				printf "\\f2\\&%s\\f1\\&.\n", title[b];
			}
	# thesis, school.
			if(reftype[b] == "phdthesis")
				printf "Unpublished doctoral dissertation, %s", issuer[b];
			else if(reftype[b] == "mastersthesis")
				printf "Unpublished master's thesis, %s", issuer[b];
}

func review(b) {
	article(b);
}


func article(b) {

	# authorlist
			if(authors[b]) {
				printfull(b);
				printf "\n";
			}
	# (date).
			printf "(%s).\n", date[b] ;
	# title.
			if(title[b]) {
				printf title[b] ".\n";
			}
	# [reviewed title.]
			if(booktitle[b]) {
				printf "[Review of \\f2\\&%s\\f1\\&", booktitle[b];
				if(editors[b]) {
					printf " by ";
					editorlist(b);
				}
				if (issuer[b]) {
					printf " %s", issuer[b];
				}
				printf "].\n";
			}

	# italics journal,
			printf "\\f2\\&%s\\f1\\&", journal[b];
	# italics(volume)
			if (volstr[b])
				printf ", \\f2\\&%s\\f1\\&", volstr[b];
			else if (volume[b])
				printf ", \\f2\\&%s\\f1\\&", volume[b];
	# (number Pt. part)
			if (numstr[b]) {
				printf "(%s", numstr[b];
				if(partstr[b])
					printf " %s", partstr[b];
				else if(part[b])
					printf " Pt. %s", part[b];
				printf ")";
			}
			else if (number[b]) {
				printf "(%s", number[b];
				if(partstr[b])
					printf " %s", partstr[b];
				else if(part[b])
					printf " Pt. %s", part[b];
				printf ")";
			}
	# , pages
			if ( pages[b] ) printf ", %s", pages[b];
}

func inproceedings(b) {

	# authorlist
			if(authors[b]) {
				printfull(b);
				printf "\n";
			}
	# (date).
			printf "(%s).\n", date[b] ;
	# title.
			if(title[b]) {
				printf title[b] ".\n";
			}

	# italics journal,
			printf "\\f2\\&%s\\f1\\&", journal[b];
	# italics(volume)
			if (volstr[b])
				printf ", \\f2\\&%s\\f1\\&", volstr[b];
			else if (volume[b])
				printf ", \\f2\\&%s\\f1\\&", volume[b];
	# (number Pt. part)
			if (numstr[b]) {
				printf "(%s", numstr[b];
				if(partstr[b])
					printf " %s", partstr[b];
				else if(part[b])
					printf " Pt. %s", part[b];
				printf ")";
			}
			else if (number[b]) {
				printf "(%s", number[b];
				if(partstr[b])
					printf " %s", partstr[b];
				else if(part[b])
					printf " Pt. %s", part[b];
				printf ")";
			}
	# editors	
			if(editors[b]) {
				printf ", ";
				editorlist(b);
				if (editors[b] == 1) printf " (Ed.)"
				else if (editors[b] > 1) printf " (Eds.)"
			}
	# , pages
			if ( pages[b] ) printf ", %s", pages[b];
}

func proceedings(b) {
			punc = "";
	# authorlist
			if(authors[b] >0) {
				printfull(b);
				punc = "\n";
			}
	# editor
			else if(editors[b]) {
				printedfull(b);
				if (editors[b] == 1) printf " (Ed.)"
				else if (editors[b] > 1) printf " (Eds.)"
				punc = ".\n";
			}

	# publisher
			else if(issuer[b]) {
				printf "%s%s", punc, issuer[b];
				punc = ".\n";
			}
	# (date).
			printf "%s(%s).\n", punc, date[b] ;
	# italic(title)
			if (title[b]) {
				printf "\\f2\\&%s\\f1\\&", title[b];
				punc = ".\n";
				if(substr(title[b],length(title[b]),1) == ".")
					punc = "\n";
	# italics(volume)
				if (volstr[b])
					printf ", \\f2\\&%s\\f1\\&", volstr[b];
				else if (volume[b])
					printf ", \\f2\\&%s\\f1\\&", volume[b];
	# (number Pt. part)
				if (numstr[b]) {
					printf "(%s", numstr[b];
					if(partstr[b])
						printf " %s", partstr[b];
					else if(part[b])
						printf " Pt. %s", part[b];
					printf ")";
				}
				else if (number[b]) {
					printf "(%s", number[b];
					if(partstr[b])
						printf " %s", partstr[b];
					else if(part[b])
						printf " Pt. %s", part[b];
					printf ")";
				}
			}
	# location: publisher,
			if (city[b]) {
				printf "%s%s", punc, city[b] ;
				punc = ": ";
			}
			printf "%s%s", punc, issuer[b] ;
}

func incollection(b) {

	# authorlist
			if(authors[b]) {
				printfull(b);
				printf "\n";
			}	
	# (date).
			printf "(%s).\n", date[b] ;
	# title
			if(title[b]) {
				printf "%s.\n", title[b];
			}
	# In editorlist (Ed.),
			if(editors[b]) {
				printf "In "
				editorlist(b);
				if (editors[b] == 1) printf " (Ed.),\n"
				else if (editors[b] > 1) printf " (Eds.),\n"
			}
	# italics(booktitle)
			printf "\\f2\\&%s\\f1\\&", booktitle[b];
	# (volume, pages).
			if (volume[b] || pages[b]) {
				comma = "";
				printf "\n(" ;
				if(volstr[b]) {
					printf "%s", volstr[b];
					comma = ", " ;
				}
				else if(volume[b]) {
					printf "Vol. %s", volume[b];
					comma = ", " ;
				}
				if(pages[b]) {
					if ( pages[b] ~ /[,-]/ )
						printf "%spp. %s", comma, pages[b];
					else printf "%sp. %s)", comma, pages[b];
				}
				printf ")"
			}
			printf ".\n";
	# location: publisher,
			if (city[b]) printf "%s: ", city[b] ;
			printf "%s", issuer[b] ;
}



func techreport(b) {

	# authorlist
			if(authors[b]) {
				printfull(b);
				printf "\n";
			}
	# (date).
			printf "(%s).\n", date[b] ;
	# italic(title)
			if(title[b])
				printf "\\f2\\&%s\\f1\\&\n", title[b];
	# (report number).
			printf "(%s).\n", report[b];

	# location: publisher.
			if (city[b]) printf "%s: ", city[b] ;
			printf issuer[b];
}

func tm(b) {

	# authorlist
			if(authors[b]) {
				printfull(b);
				printf "\n";
			}
	# (date).
			printf "(%s).\n", date[b] ;
	# italic(title)
			if(title[b])
				printf "\\f2\\&%s\\f1\\&\n", title[b];
	# (tm number).
			if(rp)
				printf "(AT&T Bell Laboratories internal memorandum)";
			else
				printf "(%s)", tmno[b] 
}

func manual(b) {
			punc = "";
	# authorlist
			if(authors[b] >0) {
				printfull(b);
				punc = "\n";
			}
	# editor
			else if(editors[b]) {
				printedfull(b);
				if (editors[b] == 1) printf " (Ed.)"
				else if (editors[b] > 1) printf " (Eds.)"
				punc = ".\n";
			}

	# publisher
			else if(issuer[b]) {
				printf "%s%s", punc, issuer[b];
				punc = ".\n";
			}
	# (date).
			printf "%s(%s).\n", punc, date[b] ;
	# italic(title)
			if(title[b]) {
				printf "\\f2\\&%s\\f1\\&", title[b];
	# (Vol #).
				if(volstr[b]) {
					printf " (%s)", volstr[b];
					punc = ".\n"
				}
				else if(volume[b]) {
					printf " (Vol. %s)", volume[b];
					punc = ".\n"
				}
				punc = ".\n"
			}

	# location: publisher.
			if (city[b]) {
				printf "%s%s", punc, city[b] ;
				punc = ": ";
			}
			if(issuer[b]) {
				if(editors[b] || authors[b]);
					printf "%s%s", punc, issuer[b];
			}
}

func pamphlet(b)
{

			punc = "";
	# authorlist
			if(authors[b] >0) {
				printfull(b);
				punc = "\n";
			}
	# publisher
			else if(issuer[b]) {
				printf "%s%s", punc, issuer[b];
				punc = ".\n";
			}
	# italic(title)
			else if(title[b]) {
				printf "\\f2\\&%s\\f1\\&", title[b];
				punc = ".\n"
			}
	# (date).
			if(date[b]) {
				printf "%s(%s)", punc, date[b] ;
				punc = ".\n";
			}
	# italic(title)
			if(editors[b] || authors[b] || issuer[b]) {
				if(title[b]) {
					printf "%s\\f2\\&%s\\f1\\&", punc, title[b];
					punc = ".\n"
				}
			}

	# location: publisher.
			if (city[b]) {
				printf "%s%s", punc, city[b] ;
				punc = ": ";
			}
			if(issuer[b]) {
				if(editors[b] || authors[b]);
					printf "%s%s", punc, issuer[b];
			}

}

func default(b) {
			punc = "";
	# authorlist
			if(authors[b]) {
				printfull(b);
				punc = "\n";
			}
	# publisher
			else if(issuer[b]) {
				printf "%s%s", punc, issuer[b];
				punc = ".\n";
			}
	# italic(title).
			else if(title[b]) {
				printf "\\f2\\&%s\\f1\\&", title[b];
				punc = ".\n"
			}
	# (date).
			if(date[b]) {
				printf "%s(%s)", punc, date[b] ;
				punc = ".\n";
			}
	# italic(title)
			if(editors[b] || authors[b] || issuer[b] ) {
				if(title[b]) {
					printf "%s%s", punc, title[b];
					punc = ".\n"
				}
			}
	# (Vol #, pp.).
			if(pages[b] || volume[b]) {
				printf punc;
				printf "("
				if(volstr[b]) {
					printf volstr[b];
					punc = ", ";
				}
				else if(volume[b]) {
					printf "Vol. %s", volume[b];
					punc = ", ";
				}
				if (pages[b]) {
					if ( pages[b] ~ /[,-]/ )
						printf "%spp. %s", punc, pages[b];
					else
						printf "%sp. %s", punc, pages[b];
				}
				printf ")";
				punc = ".\n"
			}

	# location: publisher.
			if (city[b]) {
				printf "%s%s", punc, city[b] ;
				punc = ": ";
			}
			if(issuer[b]) {
				if(editors[b] || authors[b]);
					printf "%s%s", punc, issuer[b];
			}
}


# Author, First
# Author, First, and Second Author
# Author, First, Second Author, and Remaining Authors

func printfull(b) {			
 	if ( authors[b] == 0 ) ;
	else if ( authors[b] == 1 ) {
		printf Afull[b,1] ;
		if(aetal[b]) printf ", et al.";
	}
	else if ( authors[b] == 2 ) printf Afull[b,1] ", & " Afull[b,2];
	else {
		for ( j = 1 ; j < authors[b]; j++ )
			printf  Afull[b,j] ", ";
		printf "& " Afull[b,authors[b]];
	}
}
func printedfull(b) {			
 	if ( editors[b] == 0 ) ;
	else if ( editors[b] == 1 ) {
		printf Efull[b,1] ;
		if(eetal[b]) printf ", et al.";
	}
	else if ( editors[b] == 2 ) printf Efull[b,1] ", & " Efull[b,2];
	else {
		for ( j = 1 ; j < editors[b]; j++ )
			printf  Efull[b,j] ", ";
		printf "& " Efull[b,editors[b]];
	}
}

func editorlist(b) {			
 	if ( editors[b] == 0 ) ;
	else if ( editors[b] == 1 ) printf "%s", E[b,1] ;
	else if ( editors[b] == 2 ) printf "%s & %s", E[b,1], E[b,2];
	else {
		for ( j = 1; j < editors[b]; j++ ) printf E[b,j] ", ";
		printf "& %s", E[b,editors[b]];
	}
}

