\ProvidesPackage{libertine}[%
   2012/11/13 (Bob Tennent)  Supports Libertine and Biolinum fonts for all LaTeX engines. ] 

%  with (partial) compatibilty with libertine-type1, biolinum-type1,    
%  libertine, libertine-legacy and libertineotf packages                

\RequirePackage{ifxetex,ifluatex,xkeyval,textcomp}

\newif\iflibertine@otf
\ifxetex
  \libertine@otftrue
\else\ifluatex
  \libertine@otftrue
\else  % [pdf]LaTeX
  \libertine@otffalse
\fi\fi

\newif\iflibertine@sf \libertine@sffalse
\newif\iflibertine@rm \libertine@rmfalse
\newif\iflibertine@sfdefault \libertine@sfdefaultfalse
\newif\iflibertine@lining \libertine@liningfalse
\newif\iflibertine@tabular \libertine@tabularfalse
\newif\iflibertine@semibold \libertine@semiboldfalse

\DeclareOptionX{sf}{\libertine@sftrue}
\DeclareOptionX{rm}{\libertine@rmtrue}
\DeclareOptionX{sfdefault}{\libertine@sftrue\libertine@sfdefaulttrue}
\DeclareOptionX{type1}{\libertine@otffalse}
\DeclareOptionX{lining}{\libertine@liningtrue}
\DeclareOptionX{nf}{\libertine@liningtrue}
\DeclareOptionX{lf}{\libertine@liningtrue}
\DeclareOptionX{oldstyle}{\libertine@liningfalse}
\DeclareOptionX{osf}{\libertine@liningfalse}
\DeclareOptionX{tabular}{\libertine@tabulartrue}
\DeclareOptionX{proportional}{\libertine@tabularfalse}
\DeclareOptionX{semibold}{\libertine@semiboldtrue}
\DeclareOptionX{bold}{\libertine@semiboldfalse}

\newcommand*{\LinuxLibertineO@scale}{1}  % not adjustable
\newcommand*{\LinuxBiolinumO@scale}{1}
\DeclareOptionX{scaled}{\renewcommand*{\LinuxBiolinumO@scale}{#1}}
\DeclareOptionX{scale}{\renewcommand*{\LinuxBiolinumO@scale}{#1}}

\ExecuteOptionsX{oldstyle,proportional,bold}
\ProcessOptionsX\relax

\iflibertine@otf
  \iflibertine@lining
    \def\libertine@figurestyle{Lining}
  \else
    \def\libertine@figurestyle{OldStyle}
  \fi
  \iflibertine@tabular
    \def\libertine@figurealign{Monospaced} 
  \else
    \def\libertine@figurealign{Proportional} 
  \fi
  \iflibertine@semibold
     \def\LibertineO@boldstyle{RZ} 
  \else
    \def\LibertineO@boldstyle{RB} 
  \fi

\else % type1

  \iflibertine@lining
    \def\libertine@figurestyle{LF} 
  \else
    \def\libertine@figurestyle{OsF} 
  \fi
  \iflibertine@tabular
    \def\libertine@figurealign{T} 
  \else
    \def\libertine@figurealign{} 
  \fi
  \iflibertine@semibold
    \renewcommand*{\bfdefault}{sb} 
  \else
    \renewcommand*{\bfdefault}{b} 
  \fi
\fi


\iflibertine@otf
  \RequirePackage{fontspec}
\else
  \RequirePackage{fontenc,fontaxes}
\fi

\iflibertine@rm\else\iflibertine@sf\else
  \libertine@rmtrue\libertine@sftrue  % both false -> both true
\fi\fi

% Set main and/or sans fonts:

\iflibertine@otf

  \ifxetex\XeTeXtracingfonts=1\fi
  \defaultfontfeatures{
     Ligatures = TeX ,
     Extension = .otf ,
     SmallCapsFeatures={Letters=SmallCaps} 
  }
  \iflibertine@rm
    \setmainfont
          [ Numbers = {\libertine@figurealign,\libertine@figurestyle},
            UprightFont    = *_R,
            ItalicFont     = *_RI,
            BoldFont       = *_\LibertineO@boldstyle, 
            BoldItalicFont = *_\LibertineO@boldstyle I
          ] {LinLibertine530}
    \newfontfamily\libertine
          [ Numbers = {\libertine@figurealign,\libertine@figurestyle},
            UprightFont    = *_R,
            ItalicFont     = *_RI,
            BoldFont       = *_\LibertineO@boldstyle, 
            BoldItalicFont = *_\LibertineO@boldstyle I
          ] {LinLibertine530}
  \fi
  \iflibertine@sf
    \setsansfont
          [ Numbers   = {\libertine@figurealign, \libertine@figurestyle} ,
            Scale     = \LinuxBiolinumO@scale ,
            UprightFont    = *_R ,
            ItalicFont     = *_RI ,
            BoldFont       = *_RB , 
            BoldItalicFont = *_RBO   % oblique to fake italic
          ] {LinBiolinum530}
    \newfontfamily\biolinum
          [ Numbers   = {\libertine@figurealign, \libertine@figurestyle} ,
            Scale     = \LinuxBiolinumO@scale ,
            UprightFont    = *_R ,
            ItalicFont     = *_RI ,
            BoldFont       = *_RB , 
            BoldItalicFont = *_RBO   % oblique to fake italic
          ] {LinBiolinum530}
    % grab the \sfdefault in case it changes subsequently:
    \let\biolinumfamily\sfdefault
    \iflibertine@sfdefault\renewcommand*\familydefault{\biolinumfamily}\fi
  \fi

\else % type1

  \def\libertinefamily{LinuxLibertineO-\libertine@figurealign\libertine@figurestyle}
  \def\biolinumfamily{LinuxBiolinumO-\libertine@figurealign\libertine@figurestyle}

  \iflibertine@rm\renewcommand*\rmdefault{\libertinefamily}\fi
  \iflibertine@sf\renewcommand*\sfdefault{\biolinumfamily}\fi
  \iflibertine@sfdefault\renewcommand*\familydefault{\biolinumfamily}\fi

\fi

% Define \oldstylenums[f] and \liningnums[f]

\iflibertine@otf
  \newfontfamily\libertineosf
    [ Numbers = {\libertine@figurealign,OldStyle},
      UprightFont = *_R,
      ItalicFont = *_RI,
      BoldFont = *_\LibertineO@boldstyle,
      BoldItalicFont = *_\LibertineO@boldstyle I 
    ] {LinLibertine530}
  \newfontfamily\libertinelf
    [ Numbers = {\libertine@figurealign,Lining},
      UprightFont    = *_R,
      ItalicFont     = *_RI,
      BoldFont       = *_\LibertineO@boldstyle, 
      BoldItalicFont = *_\LibertineO@boldstyle I
    ] {LinLibertine530}
  \newfontfamily\biolinumosf
    [ Numbers = {\libertine@figurealign,OldStyle},
      Scale     = \LinuxBiolinumO@scale ,
      UprightFont    = *_R,
      ItalicFont     = *_RI,
      BoldFont       = *_RB,
      BoldItalicFont = *_RBO   % oblique to fake italic
    ] {LinBiolinum530}
  \newfontfamily\biolinumlf
    [ Numbers = {\libertine@figurealign,Lining},
      Scale     = \LinuxBiolinumO@scale ,
      UprightFont    = *_R,
      ItalicFont     = *_RI,
      BoldFont       = *_RB,
      BoldItalicFont = *_RBO   % oblique to fake italic
    ] {LinBiolinum530}
  \iflibertine@rm
    \renewcommand*\oldstylenums[1]{\libertineosf #1}
    \renewcommand*\liningnums[1]{\libertinelf #1}
  \fi
  \iflibertine@sf
    \newcommand*\oldstylenumsf[1]{\biolinumosf #1}
    \newcommand*\liningnumsf[1]{\biolinumlf #1}
  \fi
  \iflibertine@rm
    \iflibertine@sf\else % Libertine only
      \newcommand*\oldstylenumsf [1]{\textsf{\oldstylenums{#1}}}
      \newcommand*\liningnumsf [1]{\textsf{\liningnums{#1}}}
    \fi    
  \else
    \iflibertine@sf % Biolinum only
      %  \oldstynums and \liningnums defined by \fontspec ?
    \fi
  \fi
\else % type1
  \iflibertine@rm
    \iflibertine@sf % Both Libertine and Biolinum
      \renewcommand*\oldstylenums [1]
        {{\fontfamily{LinuxLibertineO-\libertine@figurealign OsF}\selectfont#1}}
      \newcommand*\liningnums [1]
        {{\fontfamily{LinuxLibertineO-\libertine@figurealign LF}\selectfont#1}}
      \newcommand*\oldstylenumsf [1]
        {{\fontfamily{LinuxBiolinumO-\libertine@figurealign OsF}\selectfont#1}}
      \newcommand*\liningnumsf [1]
        {{\fontfamily{LinuxBiolinumO-\libertine@figurealign LF}\selectfont#1}}
   \else % Libertine only 
      \renewcommand*\oldstylenums [1]
        {{\fontfamily{LinuxLibertineO-\libertine@figurealign OsF}\selectfont#1}}
      \newcommand*\liningnums [1]
        {{\fontfamily{LinuxLibertineO-\libertine@figurealign LF}\selectfont#1}}
      \newcommand*\oldstylenumsf [1]{\textsf{\oldstylenums{#1}}}
      \newcommand*\liningnumsf [1]{\textsf{#1}}
   \fi
  \else % Biolinum only
    % \oldstylenums defined by \textcomp?
    \newcommand*\liningnums [1]{#1}
    \newcommand*\oldstylenumsf [1]
      {{\fontfamily{LinuxBiolinumO-\libertine@figurealign OsF}\selectfont#1}}
    \newcommand*\liningnumsf [1]
      {{\fontfamily{LinuxBiolinumO-\libertine@figurealign LF}\selectfont#1}}
  \fi
\fi

% turn off defaults in case other fonts are selected:
\iflibertine@otf
  \defaultfontfeatures{}
\fi

\endinput
