% gitinfo2.sty
% Copyright 2014 Brent Longborough
%
% 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 Brent Longborough.
%
% This work consists of these files:
%     gitinfo2.sty, gitexinfo.sty, gitinfo2.tex, gitinfo2.pdf,
%     gitinfo2test.tex, post-git-sample.txt,
%     and gitPseudoHeadInfo.gin
% -----------------------------------------------------
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{gitinfo2}[2014/05/12 v2.00 git revision information]
\RequirePackage{etoolbox}
\RequirePackage{kvoptions}
\RequirePackage{eso-pic}
\SetupKeyvalOptions{family=gitinfo,prefix=gitInf@}
\DeclareBoolOption{draft}
\DeclareBoolOption{grumpy}
\DeclareBoolOption{pcount}
\DeclareBoolOption{footinfo}
\DeclareBoolOption{mark}
\DeclareBoolOption{markifdraft}
\DeclareBoolOption{markifdirty}
\DeclareBoolOption{marknotags}
\DeclareStringOption[(None)]{missing}
\DeclareStringOption[(None)]{notags}
\DeclareStringOption[(*)]{dirty}
\DeclareStringOption[4]{maxdepth}
\DeclareStringOption[1.5\baselineskip]{raisemark}
\ProcessKeyvalOptions*
\newcommand{\gitAbbrevHash}{\gitInf@missing}
\newcommand{\gitHash}{\gitInf@missing}
\newcommand{\gitAuthorName}{\gitInf@missing}
\newcommand{\gitAuthorEmail}{\gitInf@missing}
\newcommand{\gitAuthorDate}{\gitInf@missing}
\newcommand{\gitAuthorIsoDate}{\gitInf@missing}
\newcommand{\gitAuthorUnixDate}{\gitInf@missing}
\newcommand{\gitCommitterName}{\gitInf@missing}
\newcommand{\gitCommitterEmail}{\gitInf@missing}
\newcommand{\gitCommitterDate}{\gitInf@missing}
\newcommand{\gitCommitterIsoDate}{\gitInf@missing}
\newcommand{\gitCommitterUnixDate}{\gitInf@missing}
\newcommand{\gitTags}{}
\newcommand{\gitFirstTagDescribe}{\gitInf@missing}
\newcommand{\gitReferences}{\gitInf@missing}
\newcommand{\gitBranch}{\gitInf@missing}
\newcommand{\gitVtag}{}
\newcommand{\gitVtags}{}
\newcommand{\gitVtagn}{\space\gitInf@missing}
\newcommand{\gitRel}{}
\newcommand{\gitRels}{}
\newcommand{\gitReln}{\space\gitInf@missing}
\newcommand{\gitDirty}{}
\newcommand{\gitRoff}{}
\newcommand{\gitDescribe}{(None)}
\newcommand{\gitMarkPref}{[git]}
\newcommand{\gitMarkFormat}{\color{gray}\small\sffamily}
\newcommand{\gitMark}{}

% private values
\newif\ifGI@is@a@repo@

% private constants
\providecommand{\GI@repo@prefix}{}
\providecommand{\GI@githeadinfo@file}{\GI@repo@prefix.git/gitHeadInfo.gin}
\newcommand{\gitInf@dirtflag}{}
\newcommand{\gitInf@describe}{}
\newcommand{\gitInf@mcount}{}
\newcommand{\gitInf@rel}{}
\newcommand{\gitInf@off}{}

% \TeX{}nical tools

\newcommand\GI@export@macro[1]{%
  \edef\@tempa{\endgroup
    \def\noexpand#1{#1}}%
  \@tempa}

% finding the .git directory

\newcommand\GI@check@repo@prefix{%
  \GI@is@a@repo@true
  \IfFileExists{./\GI@githeadinfo@file}{%
    \edef\GI@repo@prefix{./}%
    }{%
    \GI@set@repo@prefix}}

\newcommand\GI@set@repo@prefix{%
  \begingroup \count@\gitInf@maxdepth\relax
  \GI@set@repo@prefix@}

\newcommand\GI@set@repo@prefix@{%
  \advance\count@\m@ne
  \ifnum\count@<\z@
    % \expandafter\GI@not@a@git@repo
    \endgroup\relax
  \else
    \edef\GI@repo@prefix{../\GI@repo@prefix}%
    \IfFileExists{\GI@githeadinfo@file}{%
  \PackageWarningNoLine{gitinfo2}{%
  gitinfo2 found  : \GI@githeadinfo@file \MessageBreak
  }%
      \GI@export@macro\GI@githeadinfo@file
      }{%
      \expandafter\GI@set@repo@prefix@}%
  \fi}

\GI@check@repo@prefix
\InputIfFileExists{\GI@githeadinfo@file}{}{%
    \ifbool{gitInf@grumpy}{%
        \PackageError{gitinfo2}{I can't find the file '\GI@githeadinfo@file'\MessageBreak
        Please check the manual (gitinfo2.pdf) \MessageBreak
        if you need more details}{}
    }{%
        \PackageWarningNoLine{gitinfo2}{I can't find the file '\GI@githeadinfo@file'.\MessageBreak
        All git metadata has been set to '\gitInf@missing'}
    }
}
% ---------------------------------------------------------------------
% Memoir support
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\@ifclassloaded{memoir}{%
    \ifbool{gitInf@pcount}{
        \newcommand{\@gitPage}{\thepage/\thelastpage}
        \addtodef{\frontmatter}{\renewcommand{\@gitPage}{\thepage}}{}
        \addtodef{\mainmatter}{}{\renewcommand{\@gitPage}{\thepage/\thelastpage}}
    }{%
        \newcommand{\@gitPage}{\thepage}
    }
    \newcommand{\@gitFootRev}{Release\gitRels: \gitAbbrevHash{} (\gitAuthorDate)}
    \newcommand{\@gitrFootRev}{%
        \tiny\textsf{\raggedleft \@gitFootRev}%
        }%
    \newcommand{\@gitlFootRev}{%
        \tiny\textsf{\@gitFootRev}%
        }%
    \copypagestyle{giplain}{plain}
    \copypagestyle{giruled}{ruled}
    \copypagestyle{giheadings}{headings}
    \makeevenfoot{giplain}{\@gitPage}{}{\@gitrFootRev}
    \makeoddfoot{giplain}{\@gitlFootRev}{}{\@gitPage}
    \makeevenfoot{giruled}{\@gitPage}{}{\@gitrFootRev}
    \makeoddfoot{giruled}{\@gitlFootRev}{}{\@gitPage}
    \makeevenfoot{giheadings}{\@gitPage}{}{\@gitrFootRev}
    \makeoddfoot{giheadings}{\@gitlFootRev}{}{\@gitPage}
    \makeevenhead{giheadings}{}{}{\slshape\leftmark}
    \makeoddhead{giheadings}{\slshape\rightmark}{}{}
}{}%
% ---------------------------------------------------------------------
% Watermarking
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\ifbool{gitInf@markifdirty}{%
    \IfEq{\gitDirty}{}{}{%
        \booltrue{gitInf@mark}
        \renewcommand{\gitMarkPref}{[Dirty]}
    }%
}{}%
\ifbool{gitInf@markifdraft}{%
    \ifbool{gitInf@draft}{%
        \booltrue{gitInf@mark}
        \renewcommand{\gitMarkPref}{[Draft]}
    }{}%
}{}%
\ifbool{gitInf@mark}{%
    \AddToShipoutPictureBG{%
        \AtPageLowerLeft{%
        \raisebox{\gitInf@raisemark}{%
            \makebox[\paperwidth]{%
                \begin{minipage}[b]{\paperwidth}%
                    \renewcommand{\baselinestretch}{1}%
                    \centering{\gitMarkFormat\gitMarkPref{} \gitMark}%
                \end{minipage}%
            }%
        }%
        }%
    }%
}{}%
% ---------------------------------------------------------------------
% Datetime integration
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\@ifpackageloaded{datetime}{%
    \IfEq{\gitAuthorDate}{\gitInf@missing}{%
        \newdate{gitdate}{\THEDAY}{\THEMONTH}{\THEYEAR}
    }{%
        \StrCut{\gitAuthorDate}{-}{\gitInf@year}{\gitInf@md}
        \StrCut{\gitInf@md}{-}{\gitInf@month}{\gitInf@day}
        \newdate{gitdate}{\gitInf@day}{\gitInf@month}{\gitInf@year}
    }    
}{}%
