%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part of the ptx2pdf macro package for formatting USFM text
% copyright (c) 2007-2020 by SIL International
% Automatic page number replacements and chapter-verse referencing
%
% Permission is hereby granted, free of charge, to any person obtaining  
% a copy of this software and associated documentation files (the  
% "Software"), to deal in the Software without restriction, including  
% without limitation the rights to use, copy, modify, merge, publish,  
% distribute, sublicense, and/or sell copies of the Software, and to  
% permit persons to whom the Software is furnished to do so, subject to  
% the following conditions:
%
% The above copyright notice and this permission notice shall be  
% included in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,  
% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF  
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  
% NONINFRINGEMENT. IN NO EVENT SHALL SIL INTERNATIONAL BE LIABLE FOR  
% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  
% CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION  
% WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
% Except as contained in this notice, the name of SIL International  
% shall not be used in advertising or otherwise to promote the sale,  
% use or other dealings in this Software without prior written  
% authorization from SIL International.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\labelp@ge#1{%
    \write\picp@ges{\string\labelonpage{\folio}#1}%
}
\x@\def\csname MS:zlabel\endcsname{%
  \get@ttribute{id}%
  \trace{m}{zlabel: \attr@b}%
  \makelabelfromattr
}
\def\makelabelfromattr{%
  \ifx\attr@b\relax
    \let\attr@b\c@rref
    \ifx\attr@b\undefined
      \let\attr@b\relax
    \fi
  \fi
  \ifx\attr@b\relax\else
    \x@\def\x@\@@l@bel\x@{\x@\detokenize\x@{\attr@b}}%
    \edef\l@gstring{{\@@l@bel}{\book:\ch@pter:\print@bleverse}}%
    \trace{m}{zlabel: \l@gstring}%
    \x@\labelp@ge\x@{\l@gstring}%
  \fi
}

\x@\def\csname MS:zpage\endcsname{%
  \get@ttribute{label}%
  \ifx\attr@b\relax\else
    \x@\def\x@\@@l@bel\x@{\x@\detokenize\x@{\attr@b}}%
    \ifcsname zpage-\@@l@bel\endcsname
      \csname zpage-\@@l@bel\endcsname
    \else
      0000\message{Unable to find label for '\attr@b'. Re-run or correct typo at/near '\c@rref'.}%
    \fi
  \fi
}

\x@\def\csname MS:zref\endcsname{%
  \get@ttribute{label}%
  \ifx\attr@b\relax\else
    \x@\def\x@\@@l@bel\x@{\x@\detokenize\x@{\attr@b}}%
    \ifcsname zref-\@@l@bel\endcsname
      \x@\let\x@\temp\csname zref-\@@l@bel\endcsname
      \get@ttribute{show}%
      \ifx\attr@b\relax\def\attr@b{b_c:v}\fi
      \x@\extr@ctfirst\temp\relax
      \tracingassigns=1
      \x@\p@rsezref\x@|\attr@b|%
      \tracingassigns=0
    \else
      ??:??\message{Unable to find reference for '\attr@b'. Re-run or correct typo at/near '\c@rref'.}%
    \fi
  \fi
}

\def\p@rsezref|#1#2|{\trace{m}{p@rsezref {'#1''#2'}}\edef\tmp{\zap@space #2 \empty}%
  \if b#1\relax\trace{m}{book}\@book
  \else\if c#1\relax\trace{m}{chapter}\@chapter
    \else\if v#1\relax\trace{m}{verse}\@verse
      \else\if _#1\relax\trace{m}{space}\space
        \else #1\trace{m}{literal '#1'}\fi
      \fi
    \fi
  \fi\ifx\tmp\empty\else
    \x@\p@rsezref\x@|#2|%
  \fi}

\def\labelonpage#1#2#3{%
  \x@\def\csname zpage-\x@\detokenize{#2}\endcsname{#1}%Page number for label.
  \x@\def\csname zref-\x@\detokenize{#2}\endcsname{#3}%Reference for label, in case label might be triggered from trigger.
}
  
\def\checklabelpage#1#2#3{%
  \def\ch@ck{#1}%
  \let\ch@nged\empty
  \x@\ifx\csname zpage-\x@\detokenize{#2}\endcsname\ch@ck\else
    \temptrue\edef\ch@nged{Page number}%
  \fi
  \def\ch@ck{#3}%
  \x@\ifx\csname zref-\x@\detokenize{#2}\endcsname\ch@ck\else
    \temptrue\edef\ch@nged{\ifx\ch@nged\empty Reference\else\ch@nged\space and reference\fi}%
  \fi
  \ifx\ch@nged\empty\else
    \message{\ch@nged\space for label #2 changed. Re-run file.}%
  \fi
}
