%%%%% Identification Part %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\imta@CLASSNAME{imta}
\def\imta@VERSION{1 }
\def\imta@DATE{2017/05/11}
\def\imta@DATE@fr{11/05/2017}
\def\imta@AUTHOR{Fabien Dagnat <Fabien.Dagnat@imt-atlantique.fr>}
\newcommand*\tbClasseVersionDate{version \imta@VERSION du \imta@DATE@fr}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{\imta@CLASSNAME}[\imta@DATE]
\def\imta@ten@spaces{\@spaces\@spaces\space\space}
\def\imta@nineteen@spaces{\imta@ten@spaces\@spaces\@spaces\space}
\def\imta@fortynine@spaces{\imta@ten@spaces\imta@ten@spaces\imta@ten@spaces\imta@ten@spaces\@spaces\@spaces\space}
\def\imta@STAR@LINE{******************************************************************************^^J}
\def\imta@LINE#1{* #1 *^^J}
\def\imta@LINE@PRESENTATION{\imta@LINE{Classe pour les documents pédagogiques du dépt Info de l'IMTA\@spaces}}
\def\imta@LINE@AUTHOR{\imta@LINE{Auteur : \imta@AUTHOR\imta@nineteen@spaces}}
\def\imta@LINE@VERSION{\imta@LINE{Version \imta@VERSION du \imta@DATE@fr\imta@fortynine@spaces}}
\typeout{^^J\imta@STAR@LINE\imta@LINE@PRESENTATION\imta@LINE@AUTHOR\imta@LINE@VERSION\imta@STAR@LINE}

% %%%% TODO %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - revoir espacement fluo
% - revoir la condition sur le fond page de titre et non pas numero 1 !
% %%%% Package loading %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage[T1]{fontenc}
\RequirePackage[utf8]{inputenc} %welcome to the XXIIst century!
\RequirePackage{textcomp}
\RequirePackage{xcolor}
\RequirePackage[pdftex]{graphicx}
\RequirePackage{ifthen}
\RequirePackage{verbatim}
\RequirePackage{fancyhdr}
\RequirePackage{IMTAcolors}
\RequirePackage{inconsolata}

% %%%% Meta Macros %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% To create list (an empty list is nothing)
% \imta@APPEND takes the element (1) and the list (2), it append (1) at the end of (2)
\newcommand{\imta@APPEND}[2]{%
  \edef\@tempa{#2}%
  \ifx\@tempa\@empty%
    \edef#2{#1}%
  \else%
    \edef#2{#2,#1}%
  \fi%
}
% A macro that determine if a macros is already defined
\newcommand{\imta@ISDEFINED}[2]{%
   \expandafter\ifx\csname#1\endcsname\relax%
   \else%
     \ClassWarning{\imta@CLASSNAME}{#2\space #1 is already defined}%
   \fi%
}
% Manipulating Modes, a mode is an option that enable to exclude or includeonly
% parts of the document
% The list of modes
\def\imta@MODE@LIST{}
% Defining a new mode (argument: NAME):
% - Appends it to the mode list
% - Creates a boolean imta@mode@NAME
% - Sets it to false
\newcommand{\imta@DEFINE@MODE}[1]{%
  \imta@APPEND{#1}{\imta@MODE@LIST}%
  \newboolean{imta@mode@#1}%
  \setboolean{imta@mode@#1}{false}%
}
% Define a list of mode (argument is a comma separated list)
\newcommand{\imta@DEFINE@MODE@LIST}[1]{
  \@for\@unevar:=#1\do{%
    \imta@DEFINE@MODE{\@unevar}%
  }
}
% Manipulating Variables, a variable is a value useful for the document
% The list of modes
\def\imta@VAR@LIST{}
% Defining a new variable (argument: NAME):
% - Warns if the macro \NAME already exists
% - Appends it to the var list
% - Creates a macro \imta@var@NAME initialy containing << NAME to define >>
% - Creates a macro \showNAME printing the value of \imta@var@NAME
% - Creates a macro \NAME taking one argument that is the new content of the variable (macro \imta@var@NAME)
\RequirePackage[logonly]{trace}
\newcommand{\imta@DEFINE}[1]{
  \imta@ISDEFINED{#1}{variable}%
  \imta@APPEND{#1}{\imta@VAR@LIST}%
  \expandafter\edef\csname imta@var@#1\endcsname{#1 to define}%
  \expandafter\edef\csname show#1\endcsname{\noexpand\csname imta@var@#1\noexpand\endcsname}%
  \expandafter\edef\csname#1\endcsname##1{\noexpand\expandafter\noexpand\def\noexpand\csname imta@var@#1\noexpand\endcsname{##1}}%
}
% Define a list of variables (argument is a comma separated list)
\newcommand{\imta@DEFINE@LIST}[1]{
  \@for\@unevar:=#1\do{%
    \imta@DEFINE{\@unevar}%
  }
}
% A macro printing all defined variables and their content
\newcommand{\debug}{%
  \@for\@unevar:=\imta@VAR@LIST\do{%
    \@unevar = [\csname show\@unevar\endcsname] ; %
  }
}

% Plus besoin a priori
%\newcommand\capitalize[1]{\fup#1!}
%\def\fup#1#2!{\uppercase{#1}#2}

% %%%% Initialisations %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\imta@DEFINE@MODE@LIST{%
  correction,notesenseignants,%
  cours,devoir,ficheprog,booklet,notes,% différentes formes
  concur,concurtaf,langlog,glpo,harmojava,majeure,fip2a,fig3a,fip3a,idl,mpo,idlimta,fipsit211,ilsdihmgit,dclcodecamp,dclshell,harmobjet%
}
\imta@DEFINE@LIST{type,matiere,code,numero,soustitre,titre,titrecourt,soustitrecourt}% ,version => Je retire version pour la compatibilité avec changestb (un seul fichier trouvé qui l'utilisait dans glop

% Intern usage only to detect when we are in a correction part
\newboolean{imta@boolean@inCorrection}
\setboolean{imta@boolean@inCorrection}{false}
% Intern usage only to detect when we are in a notesenseignants part
\newboolean{imta@boolean@inNotesenseignants}
\setboolean{imta@boolean@inNotesenseignants}{false}

% Intern usage only to know if there is a subtitle
\newboolean{imta@boolean@subtitle}
\setboolean{imta@boolean@subtitle}{false}
% To control the printing mode
\newboolean{imta@boolean@handout}
\setboolean{imta@boolean@handout}{false}

%%% Switchers %%%
% Controls the use of authors on the title page
\newboolean{imta@boolean@withAuthors}
\setboolean{imta@boolean@withAuthors}{false}
\newcommand*{\withAuthors}{\setboolean{imta@boolean@withAuthors}{true}}
\newcommand*{\withoutAuthors}{\setboolean{imta@boolean@withAuthors}{false}}

% Controls the use of date on the title page
\newboolean{imta@boolean@withDate}
\setboolean{imta@boolean@withDate}{false}
\newcommand*{\withDate}{\setboolean{imta@boolean@withDate}{true}}
\newcommand*{\withoutDate}{\setboolean{imta@boolean@withDate}{false}}

% Controls the inclusion of subject and correction envs
\newboolean{imta@boolean@withCorrection}
\setboolean{imta@boolean@withCorrection}{false}
\newcommand*{\withCorrection}{\setboolean{imta@boolean@withCorrection}{true}}
\newcommand*{\withoutCorrection}{\setboolean{imta@boolean@withCorrection}{false}}

% Controls the inclusion of subject and notesenseignants envs
\newboolean{imta@boolean@withNotesenseignants}
\setboolean{imta@boolean@withNotesenseignants}{false}
\newcommand*{\withNotesenseignants}{\setboolean{imta@boolean@withNotesenseignants}{true}}
\newcommand*{\withoutNotesenseignants}{\setboolean{imta@boolean@withNotesenseignants}{false}}


% In a cours control the adding of a slide at the beginning of each section
% it shows the section list with current section highlighted
\newboolean{imta@boolean@withSection}
\setboolean{imta@boolean@withSection}{false}
\newcommand*{\withSection}{\setboolean{imta@boolean@withSection}{true}}
\newcommand*{\withoutSection}{\setboolean{imta@boolean@withSection}{false}}

\newboolean{imta@boolean@french}
\setboolean{imta@boolean@french}{true}

\newboolean{imta@boolean@smaller}
\setboolean{imta@boolean@smaller}{false}

% %%%% Macros utilitaires %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newbox\maboiteI%
\newbox\maboiteII%
\newcommand{\insertsep}[3]{%
  \setbox\maboiteI=\hbox{#1}%
  \setbox\maboiteII=\hbox{#3}%
  \ifdim\wd\maboiteI>0pt%
    \ifdim\wd\maboiteII>0pt%
      #1 #2 #3%
    \else%
      #1%
    \fi%
  \else%
    #3%
  \fi%
}
% %%%% Options, Class loading %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Declaration of options corresponding to modes
\@for\@unevar:=\imta@MODE@LIST\do{%
  \edef\imta@tempa{{\@unevar}{\noexpand\setboolean{imta@mode@\@unevar}{true}}}%
  \expandafter\DeclareOption\imta@tempa%
}
% Processing of modes necessary to know if it is a course or not
\ProcessOptions\relax%
\def\imta@languages{}

\DeclareOption{english}{
 \ifx\imta@languages\@empty\relax
 \def\imta@languages{english}
 \else
 \expandafter\def\expandafter\imta@languages\expandafter{\imta@languages,english}
 \fi
}%

\DeclareOption{nofrench}{%TODO, if only english do not add french, possible?
  \setboolean{imta@boolean@french}{false}
}%
\DeclareOption{smaller}{%
  \setboolean{imta@boolean@smaller}{true}
}%


\DeclareOption{draft}{\setlength{\overfullrule}{5pt}}

%\ExecuteOptions{french}

\ifthenelse{\boolean{imta@mode@cours}}{%
  \ifthenelse{\boolean{imta@mode@booklet}}{%
    \PassOptionsToClass{\CurrentOption,hyperref={draft=true},trans}{beamer}%
  }{}
  \DeclareOption{handout}{%
    \setboolean{imta@boolean@handout}{true}%
    \PassOptionsToClass{\CurrentOption,hyperref={draft=true}}{beamer}%
  }%
  % Forwarding all other options to beamer
  \DeclareOption*{\PassOptionsToClass{\CurrentOption}{beamer}}%
  \ProcessOptions\relax%
  \ifthenelse{\boolean{imta@boolean@smaller}}{%
    \PassOptionsToClass{ignorenonframetext,10pt}{beamer}%
  }{%
    \PassOptionsToClass{ignorenonframetext,12pt}{beamer}%
  }%
  \LoadClass{beamer}%
}{%
  \DeclareOption{handout}{\setboolean{imta@boolean@handout}{true}}%
  % Forwarding all options to article,
  \DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}%
  \ProcessOptions\relax%
  % Some default setting: A4 paper and 12pt font
  \PassOptionsToClass{a4paper,12pt}{article}%
  \LoadClass{article}%
}

\ifthenelse{\boolean{imta@boolean@french}}{%
  \ifx\imta@languages\@empty\relax
  \def\imta@languages{french}
  \else
  \expandafter\def\expandafter\imta@languages\expandafter{\imta@languages,french}
  \fi
}{}


% load all needed langages
\RequirePackage[\imta@languages]{babel}
% load package for exercises
\RequirePackage{exercice}%
% %%%% Title page %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A macro to define the title
\renewcommand{\title}[2][]{%
  \renewcommand{\imta@var@titre}{{#2}}%
  \def\@tempa{#1}%
  \ifx\@tempa\empty%
  \renewcommand{\imta@var@titrecourt}{{#2}}%
  \else%
  \renewcommand{\imta@var@titrecourt}{{#1}}%
  \fi
}
\providecommand{\subtitle}{}%ensure \subtitle exists
\renewcommand{\subtitle}[2][]{%
  \renewcommand{\imta@var@soustitre}{{#2}}%
  \def\@tempa{#1}%
  \ifx\@tempa\empty%
    \renewcommand{\imta@var@soustitrecourt}{{#2}}%
  \else%
    \renewcommand{\imta@var@soustitrecourt}{{#1}}%
  \fi
}
\subtitle{}
\newcommand\imta@empty@title{{}}
% finally load specific package for each type of doc
\ifthenelse{\boolean{imta@mode@cours}}{%
  \RequirePackage{coursimta}
}{%
  \RequirePackage{texteimta}
}
% %%%% Variable initialisation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifthenelse{\boolean{imta@mode@ficheprog}}{
  \numero{Fiche Programme}
  \title{\imta@var@matiere{} \imta@var@type}
  %\subtitle{\imta@var@matiere}
  %\setboolean{imta@boolean@withAuthors}{true}
}{}

\ifthenelse{\boolean{imta@mode@notes}}{
  % \numero{Fiche Programme}
  % \title{\imta@var@matiere{} \imta@var@type}
  %\subtitle{\imta@var@matiere}
  %\setboolean{imta@boolean@withAuthors}{true}
}{}


\ifthenelse{\boolean{imta@mode@concur}}{
  \matiere{Modélisation et Programmation Concurrente}
  \code{ELU 512}
  \type{}
}{}

\ifthenelse{\boolean{imta@mode@langlog}}{
  \matiere{Langages et logiques}
  \code{LaLog}
  \type{}
}{}

\ifthenelse{\boolean{imta@mode@harmojava}}{
  \matiere{Harmonisation Java}
  \code{F2B002x}
  \type{}
}{}

\ifthenelse{\boolean{imta@mode@glpo}}{
  \matiere{Génie Logiciel \& Programmation Objet}
  \code{INF205}
  \type{}
}{}

\ifthenelse{\boolean{imta@mode@majeure}}{
  \matiere{Génie Logiciel \& Programmation Objet (M)}
  \code{INF205}
  \type{}
}{}
\ifthenelse{\boolean{imta@mode@fip2a}}{
  \matiere{Architectures des applications persistantes}
  \code{FIP INF210}
  \type{}
}{}
%old one: TB 3A ``idl tb''
\ifthenelse{\boolean{imta@mode@idl}}{
  \matiere{Ingénierie du développement logiciel}
  \code{F2B304}
  \type{}
}{}
\ifthenelse{\boolean{imta@mode@mpo}}{
  \matiere{Méthodes et programmation objet}
  \code{PC-DI-MPO}
  \type{}
}{}
%new generation, different content ``idl imta''
\ifthenelse{\boolean{imta@mode@idlimta}}{
  \matiere{Ingénierie du développement logiciel}
  \code{DLR-IDL}
  \type{}
}{}
\ifthenelse{\boolean{imta@mode@dclcodecamp}}{
  \matiere{Semaine de rentrée}
  \code{Codecamp}
  \type{}
}{}
\ifthenelse{\boolean{imta@mode@dclshell}}{
  \matiere{Semaine de rentrée}
  \code{Shell}
  \type{}
}{}
\ifthenelse{\boolean{imta@mode@ilsdihmgit}}{
  \matiere{Introduction to Git}
  \code{ILSD -- IHM}
  \type{}
}{}
\ifthenelse{\boolean{imta@mode@fipsit211}}{
  \matiere{Modélisation et validation des logiciels}
  \code{FIP SIT211}
  \type{}
}{}
\ifthenelse{\boolean{imta@mode@harmobjet}}{
  \matiere{Harmonisation paradigme objet}
  \code{HARMO-COO}
  \type{}
}{}


\newcommand{\modulecode}{\imta@var@code}

% %%%% Environment definitions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definition of environments corresponding to modes
% If the mode is activated (by the option), it is a do nothing env,
% else its content is thrown away (= comment)
% Warnings go to the log file
\@for\@unevar:=\imta@MODE@LIST\do{%
  \edef\imta@tempa{\@unevar}%
  \edef\imta@tempb{\noexpand\boolean{imta@mode@\@unevar}}%
  \ifthenelse{\imta@tempb}{%
    \newenvironment{\imta@tempa}{}{}
    \ClassInfo{\imta@CLASSNAME}{\imta@tempa, will appear}%
  }{%
    \expandafter\def\csname \imta@tempa\endcsname{\comment}
    \expandafter\def\csname end\imta@tempa\endcsname{\endcomment}
    \ClassInfo{\imta@CLASSNAME}{\imta@tempa, will not appear}%
  }
}
% Definition of environment sujet and modification of correction
% - correction: text in red and indicating we are in a correction part (used for the style of listings)
% - sujet: similar to modes (but directed by the correction Option)
% - notesenseignants: text in purple and indicating we are in a notesenseignants part (used for the style of listings)

\RequirePackage{boites-tb}

\newcommand{\forceCorrection}{
  \colorlet{shadecolor}{red!10!white}
  \colorlet{framecolor}{red}
  \renewenvironment{correction}{%
    \setboolean{imta@boolean@inCorrection}{true}%
    \begin{breakbox}%
    }{%
    \end{breakbox}%
    \setboolean{imta@boolean@inCorrection}{false}%
  }
}
%notesenseignants
\newcommand{\forceNotesenseignants}{
  \colorlet{shadecolor}{purple!10!white}
  \colorlet{framecolor}{purple}
  \renewenvironment{notesenseignants}{%
    \setboolean{imta@boolean@inNotesenseignants}{true}%
    \begin{breakbox}%
    }{%
    \end{breakbox}%
    \setboolean{imta@boolean@inNotesenseignants}{false}%
  }
}


\AtBeginDocument{
  %mode correction
  \ifthenelse{\boolean{imta@mode@correction} \OR \boolean{imta@boolean@withCorrection}}{%
    \colorlet{shadecolor}{red!10!white}
    \colorlet{framecolor}{red}
  }{
    \ifthenelse{\boolean{imta@mode@notesenseignants} \OR \boolean{imta@boolean@withNotesenseignants}}{%
    \colorlet{shadecolor}{purple!10!white}
    \colorlet{framecolor}{purple}
  }{
    \colorlet{shadecolor}{white}
    \colorlet{framecolor}{black}
  }

  }
  \ifthenelse{\boolean{imta@boolean@withCorrection}}{%
    \renewenvironment{correction}{%
      \setboolean{imta@boolean@inCorrection}{true}%
      \begin{breakbox}%
    }{%
      \end{breakbox}%
      \setboolean{imta@boolean@inCorrection}{false}%
    }
  }{}
%  %mode notesenseignants
%  \ifthenelse{\boolean{imta@mode@notesenseignants} \OR \boolean{imta@boolean@withNotesenseignants}}{%
%    \colorlet{shadecolor}{purple!10!white}
%    \colorlet{framecolor}{purple}
%  }{
%    \colorlet{shadecolor}{white}
%    \colorlet{framecolor}{black}
%  }
  \ifthenelse{\boolean{imta@boolean@withNotesenseignants}}{%
    \renewenvironment{notesenseignants}{%
      \setboolean{imta@boolean@inNotesenseignants}{true}%
      \begin{breakbox}%
    }{%
      \end{breakbox}%
      \setboolean{imta@boolean@inNotesenseignants}{false}%
    }
  }{}
}

\setlength{\FrameSep}{2mm}
\setlength{\FrameRule}{1pt}

\ifthenelse{\boolean{imta@mode@correction}}{%
  \renewenvironment{correction}{%
    \bigskip%
    \setboolean{imta@boolean@inCorrection}{true}%
    \begin{breakbox}%
    }{%
    \end{breakbox}%
    \setboolean{imta@boolean@inCorrection}{false}%
  }
  \newenvironment{sujet}{\comment}{\endcomment}
}{%
%  \newenvironment{sujet}{}{}
%temporary...
  \ifthenelse{\boolean{imta@mode@notesenseignants}}{%
    \renewenvironment{notesenseignants}{%
      \bigskip%
      \setboolean{imta@boolean@inNotesenseignants}{true}%
      \begin{breakbox}%
      }{%
      \end{breakbox}%
      \setboolean{imta@boolean@inNotesenseignants}{false}%
    }
    \newenvironment{sujet}{\comment}{\endcomment}
  }{%
    \newenvironment{sujet}{}{}
  }


}


\ifthenelse{\boolean{imta@mode@booklet}}{%
  \newenvironment{notinbooklet}{\comment}{\endcomment}
}{%
  \newenvironment{notinbooklet}{}{}
}
% %%%% Définitions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\RequirePackage{manfnt}
%\newlength{\dbendheight}
%\newcommand{\centereddbend}{%
%  \settoheight{\dbendheight}{\dbend}%
%  \divide\dbendheight by 2%
%  \raisebox{\dbendheight}{\dbend}
%}
%
%\newcommand{\mailtb}[1]{
%  \href{mailto:#1@imt-atlantique.fr}{\tt #1@imt-atlantique.fr}
%}
%\newcommand{\link}[1]{
%  \href{http://#1}{\tt #1}
%}
%
\RequirePackage{calc}
\newcommand{\annexes}{
 \newbox\maboite%
 \newlength{\boitewidth}%
 \setlength{\boitewidth}{\textwidth-2\fboxsep-2\fboxrule}%
 \setbox\maboite=\hbox to \boitewidth{\hfil\Huge \textbf{Annexes}\hfil}
 \appendix%
 \vspace{1cm}\par%
 \fbox{\ht\maboite=11mm \dp\maboite=6mm\box\maboite}
 \par\vspace{5mm}
}
%
%\newcommand{\afaire}[1]{{\LARGE\textbf{\color{magenta}AFAIRE : #1}}}
\newcommand{\fluo}[1]{{\colorbox{yellow}{#1}}}
\newcommand{\bfvert}[1]{{\color{green}\textbf{#1}}}

%
\newenvironment{remarque}{%
 \ifthenelse{\boolean{imta@boolean@french}}{%
   \textbf{Remarque :}\par\smallskip
 }{%
   \textbf{Remark:}\par\smallskip
 }
 \normalfont\itshape
}{%
}
%\newenvironment{remarques}{%
%  \ifthenelse{\boolean{imta@boolean@french}}{%
%    \textbf{Remarques :}\par\smallskip
%  }{%
%    \textbf{Remarks:}\par\smallskip
%  }
%  \normalfont\itshape\begin{itemize}
%}{%
%  \end{itemize}
%}

\RequirePackage{todo}

\newcommand{\moodle}[1][]{%
  \def\@tempa{#1}%
  \ifx\@tempa\@empty%
  \ifthenelse{\boolean{imta@mode@mpo}}{
    \url{https://moodle.imt-atlantique.fr/course/view.php?id=19&section=1}%
  }{}%
  \else%
  \url{http://formations.telecom-bretagne.eu/fad/course/view.php?id=#1}%
  \fi
}
