% --------------------------------------------------------------------------
% the CHEMMACROS package
%
%   comprehensive support for typesetting chemistry documents
%
% --------------------------------------------------------------------------
% Clemens Niederberger
% --------------------------------------------------------------------------
% https://github.org/cgnieder/chemmacros/
% contact@mychemistry.eu
% --------------------------------------------------------------------------
% If you have any ideas, questions, suggestions or bugs to report, please
% feel free to contact me.
% --------------------------------------------------------------------------
% Copyright 2011-2017 Clemens Niederberger
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Clemens Niederberger.
% --------------------------------------------------------------------------
\RequirePackage{expl3,xparse,l3keys2e}
\ExplSyntaxOn

% --------------------------------------------------------------------------
% package information:
\tl_const:Nn \c_chemmacros_date_tl {2017/04/24}
\tl_const:Nn \c_chemmacros_version_major_number_tl {5}
\tl_const:Nn \c_chemmacros_version_minor_number_tl {8}
\tl_const:Nn \c_chemmacros_version_subrelease_tl   {}
\tl_const:Nx \c_chemmacros_version_number_tl
  {
    \c_chemmacros_version_major_number_tl .
    \c_chemmacros_version_minor_number_tl
  }
\tl_const:Nx \c_chemmacros_version_tl
  {
    \c_chemmacros_version_number_tl
    \c_chemmacros_version_subrelease_tl
  }
\tl_const:Nn \c_chemmacros_info_tl
  {comprehensive~ support~ for~ typesetting~ chemistry~ documents}

\ProvidesExplPackage
  {chemmacros}
  {\c_chemmacros_date_tl}
  {\c_chemmacros_version_tl}
  {\c_chemmacros_info_tl \c_space_tl (CN)}

% --------------------------------------------------------------------------
% set compatibility
\fp_new:N \l__chemmacros_compatibility_version_fp
\fp_set:Nn \l__chemmacros_compatibility_version_fp {\c_chemmacros_version_number_tl}

\keys_define:nn {chemmacros/compatibility}
  {
    compatibility .code:n =
      \str_case:nnTF {#1}
        {
          {newest} {}
          {latest} {}
        }
        {
          \fp_set:Nn \l__chemmacros_compatibility_version_fp
            { \c_chemmacros_version_number_tl }
        }
        { \fp_set:Nn \l__chemmacros_compatibility_version_fp {#1} } ,
    unknown       .code:n =
      \PassOptionsToPackage{\CurrentOption}{chemmacros4}
      \PassOptionsToPackage{\CurrentOption}{chemmacros5}
  }

\ProcessKeysPackageOptions {chemmacros/compatibility}

% --------------------------------------------------------------------------
% compare version numbers
% #1: number
% #2: relation
% #3: number
% #4: true
% #5: false
\prg_new_conditional:Npnn \chemmacros_if_version:nnn #1#2#3 {p,T,F,TF}
  {
    \fp_compare:nTF { floor(#1) = floor(#3) }
      {
        \fp_compare:nTF
          {
            ( \chemmacros_get_minor_version:f {#1} )
            #2
            ( \chemmacros_get_minor_version:f {#3} )
          }
          { \prg_return_true: }
          { \prg_return_false: }
      }
      {
        \fp_compare:nTF { (#1) #2 (#3) }
          { \prg_return_true: }
          { \prg_return_false: }
      }
  }

\cs_new:Npn \__chemmacros_get_minor_version:w #1.#2.#3 \q_stop
  { \tl_if_blank:nTF {#2} {0} {#2} }
\cs_new:Npn \chemmacros_get_minor_version:n #1
  { \__chemmacros_get_minor_version:w #1.. \q_stop }
\cs_generate_variant:Nn \chemmacros_get_minor_version:n {f}

% check compatibility
% #1: relation
% #2: number
% #3: true
% #4: false
\prg_new_conditional:Npnn \chemmacros_if_compatibility:nn #1#2 {p,T,F,TF}
  {
    \chemmacros_if_version:nnnTF
      { \fp_to_decimal:N \l__chemmacros_compatibility_version_fp }
      {#1}
      {#2}
      { \prg_return_true: }
      { \prg_return_false: }
  }
\cs_generate_variant:Nn \chemmacros_if_compatibility:nnT {nV}

% user checks:
\NewDocumentCommand \IfChemCompatibilityTF {mm+m+m}
  { \chemmacros_if_compatibility:nnTF {#1} {#2} {#3} {#4} }

\NewDocumentCommand \IfChemCompatibilityT {mm+m}
  { \chemmacros_if_compatibility:nnT {#1} {#2} {#3} }

\NewDocumentCommand \IfChemCompatibilityF {mm+m}
  { \chemmacros_if_compatibility:nnF {#1} {#2} {#3} }

% --------------------------------------------------------------------------
% compatibility coding:
\cs_new_protected:Npn \ChemCompatibility #1#2 \EndChemCompatibility
  { \chemmacros_if_compatibility:nnT {=} {#1} {#2} }
  
\cs_new_protected:Npn \ChemCompatibilityFrom #1#2 \EndChemCompatibility
  { \chemmacros_if_compatibility:nnT {>=} {#1} {#2} }

\cs_new_protected:Npn \ChemCompatibilityTo #1#2 \EndChemCompatibility
  { \chemmacros_if_compatibility:nnT {<=} {#1} {#2} }

\cs_new_protected:Npn \ChemCompatibilityBetween #1#2#3 \EndChemCompatibility
  {
    \bool_if:nT
      {
        \chemmacros_if_compatibility_p:nn {>=} {#1} &&
        \chemmacros_if_compatibility_p:nn {<=} {#2}
      }
      {#3}
  }

% --------------------------------------------------------------------------
% messages:
\msg_new:nnn {chemmacros} {too-low-compatibility}
  {
    Compatibility~ for~ versions~ below~ v4.7~ is~ not~ supported!~ You~
    requested~ version~ v #1.~ Loading~ v4.7~ instead.
  }

\msg_new:nnn {chemmacros} {too-high-compatibility}
  {
    You~ requested~ compatibility~ mode~ v #1~ while~ the~ current~ version~
    of~ chemmacros~ is~ v \c_chemmacros_version_number_tl .~ Falling~ back~
    to~ v \c_chemmacros_version_number_tl !
  }

\msg_new:nnn {chemmacros} {low-compatibility}
  {
    You~ requested~ compatibility~ mode~ v #1~ while~ the~ current~ version~
    of~ chemmacros~ is~ v \c_chemmacros_version_number_tl .~ Not~ all~ features~
    will~ be~ available!
  }

% --------------------------------------------------------------------------
% let's see that the max number is the newest (=current) version
\chemmacros_if_compatibility:nnT {>} { \c_chemmacros_version_number_tl }
  {
    \fp_set:Nn \l__chemmacros_compatibility_version_fp
      { \c_chemmacros_version_number_tl }
  }

% --------------------------------------------------------------------------
% compatibility too low:
\chemmacros_if_compatibility:nnT {<} {4.7}
  {
    \msg_warning:nnx {chemmacros} {too-low-compatibility}
      { \fp_to_tl:N \l__chemmacros_compatibility_version_fp }
  }

% not the newest version:
\chemmacros_if_compatibility:nnT {<} { \c_chemmacros_version_number_tl }
  {
    \msg_warning:nnx {chemmacros} {low-compatibility}
      { \fp_to_tl:N \l__chemmacros_compatibility_version_fp }
  }
  
% --------------------------------------------------------------------------
\chemmacros_if_compatibility:nnTF {<} {5.0}
  { \RequirePackage{chemmacros4} }
  { \RequirePackage{chemmacros5} }

% --------------------------------------------------------------------------
\tex_endinput:D

