% \iffalse meta-comment
%
% Copyright (C) 2011 by Michael Schmeing <michael.schmeing@googlemail.com>
% -------------------------------------------------------
% 
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.2
% 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.2 or later is part of all distributions of LaTeX 
% version 1999/12/01 or later.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{mVersion.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{mVersion}
%<*package>	
    [2013/03/11 v1.0.1 easy versioning for LaTeX]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{mVersion}[2011/10/18]
\EnableCrossrefs         
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{mVersion.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v1.0}{2011/10/18}{Initial version}
% \changes{v1.0.1}{2013/03/11}{Typos fixed}
%
% \GetFileInfo{mVersion.dtx}
%
% 
% \title{The \textsf{mVersion} package\thanks{This document
%   corresponds to \textsf{mVersion}~\fileversion, dated \filedate.}}
% \author{Michael Schmeing \\ \texttt{michael.schmeing@googlemail.com}}
%
% \maketitle
%
% \section{Introduction}
%
% The \textsf{mVersion} package is an easy way to keep track of different versions of your document. It provides a counter that can be incremented each time you compile the document. By displaying the counter on each page, (e.g. with the \textsf{hyperref}-package) you can see which of two versions is the newer one.
% 
% The version number is considered to consist of two parts which are separated by a dot. The first part is a fixed string that can be set by the user with |setVersion|. The second part is the build number which can be incremented by calling |increaseBuild|. The command |version| prints the complete version number, e.g. 0.1.334 with  0.1 being the fixed version number and 334 the build number.
%
% Fixed version number and build number are automatically stored in the file \emph{version.dat} which is created by mVersion. By calling |increaseBuild| in the preamble of your document, you can increase the build number of your document each time you compile.
%
% \section{Usage}
%
% The \textsf{mVersion} package provides three commands.
%
% \DescribeMacro{version}
% This command prints out the version number consisting of the fixed part,
% followed by a dot and the build number.
%
% \DescribeMacro{setBuild}
% Sets the fixed part of the version number
%
% \DescribeMacro{increaseBuild}
% Each time |increaseBuild| is called, the build number that can be 
% displayed with |version| is increased by one.
%
% \StopEventually{}
% \PrintIndex
% \section{Implementation}
%
% We begin with a small macro |parseline| that reads a line of the form X;Y followed by one space. It then assigns X to |versionnumber| and Y to |buildnumber|. This macro helps reading the fixed version number and the build number from the file \emph{version.dat}. Since \LaTeX\ adds an additional space to each line it reads from file, we need this macro. 
%    \begin{macrocode}
\def\parseline#1;#2 %this space is important
{
  \def\versionnumber{#1}
  \def\buildnumber{#2} 
}
%    \end{macrocode}
%
% \subsection{Things to do on startup}
% First, check if version file \emph{version.dat} exsists. 
%    \begin{macrocode}
\IfFileExists{version.dat}
{ }
%    \end{macrocode}
% If not, initialize with version number 0.0 and build number 0 and write the version file to disk. Note that the version file stores the version information in the format X;Y with X being the fixed version number and Y the build number.
%    \begin{macrocode}
{
	\newwrite\outfile
	\immediate\openout\outfile=version.dat
	\immediate\write\outfile{0.0;0}
	\immediate\closeout\outfile
}
%    \end{macrocode}
% After making sure that the version file exists, read version and build number from version file
%    \begin{macrocode}
\newread\versionfile
\openin\versionfile=version.dat
\read\versionfile to \versionline
\closein\versionfile
%    \end{macrocode}
% |versionline| now contains the version information in the X;Y--format but with an additional space following. Therefore, we let |versionline| be parsed by |pareseline|.
%    \begin{macrocode}
\expandafter\parseline\versionline
%    \end{macrocode}
% Finally, we initialize the build counter
%    \begin{macrocode}
\newcounter{buildcounter}
\setcounter{buildcounter}{\buildnumber}
%    \end{macrocode}
%
% \subsection{Command definitions}
%
% \begin{macro}{\version}
% Get the current version number.
%    \begin{macrocode}
	\newcommand{\version}{\versionnumber.\thebuildcounter}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setVersion}
% |setVersion| lets you set the fixed version number. First, write the new version number and old build number to version file
%    \begin{macrocode}
\newcommand{\setVersion}[1]
{
	\newwrite\outfile
	\immediate\openout\outfile=version.dat
	\immediate\write\outfile{#1;\thebuildcounter}
	\immediate\closeout\outfile
%    \end{macrocode}
% Now re-read version file to re-read version number (not build number)
%    \begin{macrocode}
	\newread\versionfile
	\openin\versionfile=version.dat
	\read\versionfile to \versionline
	\expandafter\parseline\versionline
	\closein\versionfile
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\increaseBuild}
%Increase build number by one and save new version and build number to version file. By calling |increaseBuild| in the preamble, the build number is increased each time the document is compiled.
%    \begin{macrocode}
\newcommand{\increaseBuild}
{
%    \end{macrocode}
	%Increase build number
%    \begin{macrocode}
	\stepcounter{buildcounter}
%    \end{macrocode}
	%Save version and build number to version file
%    \begin{macrocode}	
	\newwrite\outfile
	\immediate\openout\outfile=version.dat
	\immediate\write\outfile{\versionnumber;\thebuildcounter}
	\immediate\closeout\outfile
}
%    \end{macrocode}
% \end{macro}
% \Finale
\endinput
