% Version management for Unified Process Methodology
%
% Copyright (c) 2006-2007 Stephane GALLAND <galland@arakhne.org>
% Copyright (c) 2013-2025 Stephane GALLAND <galland@arakhne.org>
% 
% This program is free library; you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as
% published by the Free Software Foundation; either version 3 of the
% License, or any later version.
%
% This library is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
% Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public
% License along with this library; see the file COPYING.  If not,
% write to the Free Software Foundation, Inc., 59 Temple Place - Suite
% 330, Boston, MA 02111-1307, USA.
%

\global\edef\upm@package@version@ver{2025/03/15}

\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{upmethodology-version}[\upm@package@version@ver]

\RequirePackage{upmethodology-p-common}

%locales
\def\upm@version@lang@english{
  \gdef\upm@lang@@{\message{**** upmethodology-version is using English language ****}}%
  \gdef\upm@lang@date{Date}
  \gdef\upm@lang@updates{Updates}
  \gdef\upm@lang@version{Version}
  \gdef\upm@lang@version@history{Version History}
  \gdef\upm@lang@restricted{Confidential}
  \gdef\upm@lang@validable{Validable}
  \gdef\upm@lang@validated{Validated}
  \gdef\upm@lang@public{Public}
}
\def\upm@version@lang@french{
  \gdef\upm@lang@@{\message{**** upmethodology-version is using French language ****}}%
  \gdef\upm@lang@date{Date}
  \gdef\upm@lang@updates{Modifications}
  \gdef\upm@lang@version{Version}
  \gdef\upm@lang@version@history{Historique}
  \gdef\upm@lang@restricted{Confidentiel}
  \gdef\upm@lang@validable{Validable}
  \gdef\upm@lang@validated{Valid\'e}
  \gdef\upm@lang@public{Publique}
}

%----------------------------------------
% OPTIONS
%----------------------------------------
\DeclareOption{french}{%
  \upm@version@lang@french
}
\DeclareOption{francais}{%
  \upm@version@lang@french
}
\DeclareOption{english}{%
  \upm@version@lang@english
}
\ExecuteOptions{english}
\ProcessOptions*

\upm@lang@@

\RequirePackage{upmethodology-fmt}

%tmp counter
\newcount{\upm@tmp@a}

% Internal information: first and last registered dates
\let\upm@thefirstdate\@undefined
\let\upm@thelastdate\@undefined

%increment major part of version number
\def\upm@incmajorversion#1.#2{%
	\upm@tmp@a=#1\advance\upm@tmp@a + 1%
	\global\edef\theupmversion{\the\upm@tmp@a.0}%
}

%increment minor part of version number
\def\upm@incminorversion#1.#2{%
	\upm@tmp@a=#2\advance\upm@tmp@a + 1%
	\global\edef\theupmversion{#1.\the\upm@tmp@a}%
}

%list of updates
\def\upm@update@list{}

%add an update to the list
\newcommand{\upm@addupdatetolist}[4]{%
	\global\protected@edef\upm@update@list{%
		\upm@update@list%
		#1 & #2 & #3 \protect\\%
	}
}

%-----
% Available status' constants
\def\upmrestricted{\upm@lang@restricted}
\def\upmvalidable{\upm@lang@validable}
\def\upmvalidated{\upm@lang@validated}
\def\upmpublic{\upm@lang@public}

%-----
% Replies the modification date of the given version
\newcommand{\upmdate}[1]{\@nameuse{upm@version@#1@updatedate}}

%-----
% Replies the modification description of the given version
\newcommand{\upmdescription}[1]{\@nameuse{upm@version@#1@description}}

%-----
% Replies the status of the given version
\newcommand{\upmstatus}[1]{\@nameuse{upm@version@#1@level}}

%-----
\newcommand{\upm@updateversion}[4]{%
	\gdef\theupmdate{#2}%
	\gdef\theupmlastmodif{#3}%
	\gdef\theupmstatus{#4}%
	\ifx\upm@thefirstdate\@undefined
		\global\edef\upm@thefirstdate{#2}
	\fi
	\global\edef\upm@thelastdate{#2}
	\@namedef{upm@version@#1@updatedate}{#2}%
	\@namedef{upm@version@#1@description}{#3}%
	\@namedef{upm@version@#1@level}{#4}%
	\upm@addupdatetolist{#1}{#2}{#3}{#4}%
}

%-----
%\updateversion{new_version}{update_date}{description}{status}
\newcommand{\updateversion}[4]{%
	\gdef\theupmversion{#1}%
	\upm@updateversion{#1}{#2}{#3}{#4}%
}

%-----
%\initialversion[version]{date}{description}{status}
\newcommand{\initialversion}[4][0.1]{%
	\updateversion{#1}{#2}{#3}{#4}%
}

%-----
%\incversion{update_date}{description}{status}
\newcommand{\incversion}[3]{%
	\expandafter\upm@incmajorversion\theupmversion%
	\upm@updateversion{\theupmversion}{#1}{#2}{#3}%
}

%-----
%\incsubversion{update_date}{description}{status}
\newcommand{\incsubversion}[3]{%
	\expandafter\upm@incminorversion\theupmversion%
	\upm@updateversion{\theupmversion}{#1}{#2}{#3}%
}

%-----
% Version variables
\def\theupmversion{0.1}
\edef\theupmdate{\today}
\def\theupmlastmodif{}
\def\theupmstatus{\upmrestricted}

%-----
% Display the version history
%\upmhistory[width]
\newcommand{\upmhistory}[1][\linewidth]{%
	\noindent\expandafter\begin{mtabular}[#1]{3}{|c|c|X|}{\upm@lang@version@history}%
	\tabularheader{\upm@lang@version}{\upm@lang@date}{\upm@lang@updates}%
	\upm@update@list
	\hline
	\expandafter\end{mtabular}\par\vspace{.5cm}%
}

%-----
% Copyright date
%\upmcopyrightdate
\newcommand{\upmcopyrightdate}{%
	\ifx\upm@thefirstdate\@undefined%
		\edef\upm@tmp@copyright@a{\the\year}%
	\else%
		\edef\upm@tmp@copyright@a{\extractyear{\upm@thefirstdate}}%
	\fi%
	\ifx\upm@thelastdate\@undefined%
		\def\upm@tmp@copyright@b{\the\year}%
	\else%
		\edef\upm@tmp@copyright@b{\extractyear{\upm@thelastdate}}%
	\fi%
	\ifthenelse{\equal{\upm@tmp@copyright@a}{\upm@tmp@copyright@b}}{%
		\upm@tmp@copyright@a%
	}{%
		\upm@tmp@copyright@a-\upm@tmp@copyright@b%
	}%
}

\endinput