% Copyright 2004, 2005 by Arne Jorgensen

% This file is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.

% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
% General Public License for more details.

% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston,
% MA 02111-1307, USA.

\NeedsTeXFormat{LaTeX2e}[1995/12/01]

% subversion date and revision
\def\dkbib@svn $#1 #2 #3 #4-#5-#6 #7 #8 ${%
  \def\dkbib@sty@revision{#3}
  \def\dkbib@sty@date{#4/#5/#6}
}
\dkbib@svn $Id: dk-bib.sty 47 2005-01-20 18:29:13Z arne $
\def\dkbib@version{0.3}

\ProvidesPackage{dk-bib}[\dkbib@sty@date\space v\dkbib@version\space(r\dkbib@sty@revision) Danish variants of the standard BibTeX styles]

\RequirePackage{xkeyval}

\newif\ifdkbib@url
\dkbib@urlfalse
\DeclareOptionX{url}[true]{%
  \csname dkbib@url#1\endcsname}

\newif\ifdkbib@isbn
\dkbib@isbnfalse
\DeclareOptionX{isbn}[true]{%
  \csname dkbib@isbn#1\endcsname}

\newif\ifdkbib@issn
\dkbib@issnfalse
\DeclareOptionX{issn}[true]{%
  \csname dkbib@issn#1\endcsname}

\newif\ifdkbib@printing
\dkbib@printingfalse
\DeclareOptionX{printing}[true]{%
  \csname dkbib@printing#1\endcsname}

\newif\ifdkbib@ordinalstoword
\dkbib@ordinalstowordfalse
\DeclareOptionX{ordinals2word}[true]{%
  \csname dkbib@ordinalstoword#1\endcsname}

\newcount\dkbib@ordinaldepth
\dkbib@ordinaldepth20
\DeclareOptionX{ordinaldepth}{%
  \dkbib@ordinaldepth#1}

\newif\ifdkbib@apalike
\dkbib@apalikefalse
\DeclareOptionX{apalike}[true]{%
  \csname dkbib@apalike#1\endcsname}

\ProcessOptionsX

% Work around a wrong implementation  of \MakeUppercase (according to
% Morten H\o gholm who also provided this implementation)
\DeclareRobustCommand\dkbib@MakeUppercase[1]{%
  \begingroup
  \def\i{I}\def\j{J}%
  \def\dkbib@reserved@a##1##2{\let##1##2\dkbib@reserved@a}%
  \expandafter\dkbib@reserved@a\@uclclist\dkbib@reserved@b{\dkbib@reserved@b\@gobble}%
  \protected@edef\dkbib@reserved@a{\endgroup\uppercase{#1}}%
  \dkbib@reserved@a
}
\protected@edef\dkbib@MakeUppercase#1{\dkbib@MakeUppercase{#1}}

\newcommand*\dkbib@DeclareOrdinalWord[2]{
  \@namedef{dkbib@word#1}{#2}%
}
\dkbib@DeclareOrdinalWord{1}{f\o rste}
\dkbib@DeclareOrdinalWord{2}{anden}
\dkbib@DeclareOrdinalWord{3}{tredje}
\dkbib@DeclareOrdinalWord{4}{fjerde}
\dkbib@DeclareOrdinalWord{5}{femte}
\dkbib@DeclareOrdinalWord{6}{sjette}
\dkbib@DeclareOrdinalWord{7}{syvende}
\dkbib@DeclareOrdinalWord{8}{ottende}
\dkbib@DeclareOrdinalWord{9}{niende}
\dkbib@DeclareOrdinalWord{10}{tiende}
\dkbib@DeclareOrdinalWord{11}{elvte}
\dkbib@DeclareOrdinalWord{12}{tolvte}
\dkbib@DeclareOrdinalWord{13}{trettende}
\dkbib@DeclareOrdinalWord{14}{fjortende}
\dkbib@DeclareOrdinalWord{15}{femtende}
\dkbib@DeclareOrdinalWord{16}{sekstende}
\dkbib@DeclareOrdinalWord{17}{syttende}
\dkbib@DeclareOrdinalWord{18}{attende}
\dkbib@DeclareOrdinalWord{19}{nittende}
\dkbib@DeclareOrdinalWord{20}{tyvende}

\AtBeginDocument{%
  %%% apalike
  \ifdkbib@apalike%
    \RequirePackage{apalike}%
  \fi%
  %%% url
  \ifdkbib@url%
    \RequirePackage{url}%
    \expandafter\ifx\csname dkbiburl\endcsname\relax\def\dkbiburl#1{URL \url{#1}}\fi%
  \else%
    \def\dkbiburl#1.{\relax}%
  \fi%
  %%% isbn
  \ifdkbib@isbn%
    \expandafter\ifx\csname dkbibisbn\endcsname\relax\def\dkbibisbn#1{ISBN #1}\fi%
  \else%
    \def\dkbibisbn#1.{\relax}%
  \fi%
  %%% issn
  \ifdkbib@issn%
    \expandafter\ifx\csname dkbibissn\endcsname\relax\def\dkbibissn#1{ISSN #1}\fi%
  \else%
    \def\dkbibissn#1.{\relax}%
  \fi%
  %%% printing
  \ifdkbib@printing%
    \ifdkbib@ordinalstoword%
      \def\dkbibPrinting#1{%
        \@ifundefined{dkbib@word#1}%
          {\dkbib@MakeUppercase #1\dkbib@addperiod~oplag}%
          {%
            \ifnum#1>\dkbib@ordinaldepth
              #1\dkbib@addperiod~oplag%
            \else
              \expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\dkbib@MakeUppercase\@nameuse{dkbib@word#1}~oplag%
            \fi
          }%
        }
      \def\dkbibprinting#1{%
        \unskip ,
        \@ifundefined{dkbib@word#1}%
          {#1~oplag}%
          {%
            \ifnum#1>\dkbib@ordinaldepth
            #1~oplag%
            \else
            \@nameuse{dkbib@word#1}~oplag%
            \fi
          }%
        }
    \else
      \expandafter\ifx\csname dkbibprinting\endcsname\relax%
        \def\dkbibprinting#1{#1\dkbib@addperiod~oplag}\fi%
      \expandafter\ifx\csname dkbibPrinting\endcsname\relax%
        \def\dkbibPrinting#1{\dkbib@MakeUppercase#1\dkbib@addperiod~oplag}\fi%
    \fi
  \else%
    \def\dkbibprinting#1{\unskip}%
    \def\dkbibPrinting#1#2{\unskip}%
  \fi%
  %%% edition
  \ifdkbib@ordinalstoword%
    \def\dkbibEdition#1#2{%
      \@ifundefined{dkbib@word#1}%
        {\dkbib@MakeUppercase#1\dkbib@addperiod~udgave}%
        {%
          \ifnum#1>\dkbib@ordinaldepth
            #1\dkbib@addperiod~udgave%
          \else
            \expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\dkbib@MakeUppercase\@nameuse{dkbib@word#1}~udgave%
          \fi
        }%
      }
    \def\dkbibedition#1#2{%
      \@ifundefined{dkbib@word#1}%
        {#1\dkbib@addperiod~udgave}%
        {%
          \ifnum#1>\dkbib@ordinaldepth
          #1\dkbib@period~udgave%
          \else
          \@nameuse{dkbib@word#1}~udgave%
          \fi
        }%
      }
  \else
    \def\dkbibEdition#1{\dkbib@MakeUppercase#1\dkbib@addperiod~udgave}%
    \def\dkbibedition#1{#1\dkbib@addperiod~udgave}%
  \fi%
}
