%% This is KEYLIST.TEX for generating a list of contents of a
%% BibTeX database file, complete with the keywords as labels
%% This must be used with KEYLIST.BST to get the correct bibliography style
%% 
%% USE:  1. LaTeX this file and enter name of .bib file when prompted
%%       2. BibTeX this file (more precisely, the .aux file of this name)
%%       3. LaTeX this file again (no prompts this time)
%%       4. Delete the .bbl file before going to 1. again
%%  If a .bbl file exists at step 1, the program halts prematurely
%%  If step 2 is omitted, the program is more forgiving: it halts so that
%%     BibTeX may be run, followed by step 3.
%%  (These two precautions ensure that the contents listed have the correct
%%     title. If they were not there, then if one forgot step 2 and an
%%     older .bbl file were still present, then it would be output with
%%     the new heading.)
%% Date: 1993 January 19
%% Author:       Patrick W. Daly
%%               Max-Planck-Institut f\"ur Aeronomie
%%               W-3411 Katlenburg-Lindau
%%               Germany
%%               E-Mail:     Span-  nsp::linmpi::daly  (nsp=former ecd1)
%%                       Internet-  daly@linmpi.dnet.gwdg.de
%%
\documentstyle{article}
\oddsidemargin=0pt \topmargin=0pt
\textwidth=450pt \textheight=630pt
\pagestyle{myheadings}

\makeatletter 
\def\writeaux#1{\write\@auxout{#1}}
\def\@biblabel#1{\bf#1\hfill} 
\makeatother
\def\thebibliography#1{\list
 {}{\settowidth\labelwidth{\bf#1}\leftmargin\labelwidth
 \advance\leftmargin\labelsep\usecounter{enumi}}
 \def\newblock{\hskip .11em plus .33em minus -.07em}
 \sloppy \sfcode`\.=1000\relax}

\typeout{LISTING CONTENTS OF BIBLIOGRAPHIC DATA BASE}

\newif\iffirst \newif\ifbbl \newread\infile
\openin\infile=\jobname.aux
\ifeof\infile \firsttrue \else \firstfalse \fi
\closein\infile
\openin\infile=\jobname.bbl 
\ifeof\infile \bblfalse \else \bbltrue \fi
\closein\infile

\begin{document}
\expandafter\ifx\csname bibfile\endcsname\relax \firsttrue\fi
\def\Bibfile{??????}  \def\quit{\relax}
\iffirst
{\obeyspaces
  \typeout{**************************}
  \typeout{*   This is FIRST pass   *}
  \typeout{**************************}  }
     \ifbbl
     {\obeyspaces
        \typeout{WARNING: file \jobname.bbl already exists}
        \typeout{         Delete it and re-run this job} }
        \gdef\quit{\enddocument}
     \else
        \typein[\bibfile]{Name of .bib file to list}
        \writeaux{\string\gdef\string\bibfile{\bibfile}}  
     \fi
\else
{\obeyspaces
  \typeout{**************************}
  \typeout{*  This is SECOND pass   *}
  \typeout{**************************}  }
  \edef\Bibfile{\uppercase{\bibfile}}
  \typeout{Listing contents of BibTeX database:}
  \typeout{======= \bibfile\space =======}
\fi 
\quit

\markright{Bibliography Key List for \Bibfile, \today}
\thispagestyle{plain}
\begin{center}
\Large Key List for Bibliography File: {\Bibfile} \\
\today
\end{center}
\vspace{1em}
\nocite{*}
\bibliographystyle{keylist}
\bibliography{\bibfile}

\ifbbl
{\obeyspaces
  \typeout{********************************************}
  \typeout{  Listing for database \jobname finished}
  \typeout{  Ignore the warning about changed label(s)}
  \typeout{         DO NOT PROCESS AGAIN!!}
  \typeout{********************************************}  }
\else  
{\obeyspaces
  \typeout{********************************************}
  \typeout{  The \jobname.bbl does not yet exist}
  \typeout{  Run BibTeX \jobname to make up .bbl file}
  \typeout{  and then run LaTeX \jobname once more}
  \typeout{********************************************} }
  \writeaux{\string\gdef\string\bibfile{\bibfile}}  
\fi
\end{document}
