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

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{enstb}[2004/02/10 classe pour les sujets de majeure isi]

\newcommand{\cmd}[1]{\textnormal{\texttt{#1}}}

\newcommand{\enstb@type}{{MR2 Informatique}}
\newcommand{\@matiere}{{Module MMPFPSD}}
\newcommand{\@numero}{{Examen}}

\newif\ifenstb@correction
\enstb@correctionfalse

\PassOptionsToClass{a4paper,12pt}{article} % format a4paper par défaut
\DeclareOption{correction}{\enstb@correctiontrue}
\DeclareOption{enseignant}{\enstb@correctiontrue}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax

\LoadClass{article}

%%%% Package loading %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifx\pdfoutput\undefined
\RequirePackage[colorlinks=true, filecolor=blue, urlcolor=blue, linkcolor=blue, dvips]{hyperref}
\else
\RequirePackage[colorlinks=true, filecolor=blue, urlcolor=blue, linkcolor=blue, pdftex]{hyperref}
\fi

\RequirePackage{xcolor}
\RequirePackage{graphicx}
\RequirePackage{amssymb}
\RequirePackage{ntheorem}
\RequirePackage{fancyhdr}
\RequirePackage[french]{babel}
\RequirePackage{vmargin}
\RequirePackage[override]{cmtt}
\RequirePackage[latin1]{inputenc}
\RequirePackage{url}
\RequirePackage{calc}
\RequirePackage{listings}
\RequirePackage{lastpage}
\RequirePackage{fltpoint}

%%%%% Declaration of options %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\pagestyle{fancy}

%%%% Modification de la page %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\addtolength{\headheight}{\baselineskip}
\setlength{\parindent}{0pt}
\setlength{\parskip}{4pt}
\setlength{\headsep}{15pt}
\setlength{\headheight}{14pt}
\addtolength{\textwidth}{2cm}
\addtolength{\textheight}{8mm}
\addtolength{\headwidth}{2.5cm}
\addtolength{\oddsidemargin}{-1cm}

\overfullrule=5pt

%%%% Définitions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\partie}[1]{{\color{green}\textbf{#1}}}

\lstdefinelanguage{myjava}
  {keywords={abstract,boolean,break,byte,case,catch,char,class,%
      const,continue,default,do,double,else,extends,false,final,%
      finally,float,for,goto,if,implements,import,instanceof,int,%
      interface,label,long,native,new,null,package,private,protected,%
      public,return,short,static,super,switch,synchronized,this,throw,%
      throws,transient,true,try,void,volatile,while},%
   sensitive,%
   alsoletter={+-!*=/<>?\%|\&},%
   ndkeywords={?,/,+,-,!,--,++,*,=,==,!=,<,>,<=,>=,\%,|,||,\&,\&\&},%
   morecomment=[l]//,%
   morecomment=[s]{/*}{*/},%
   morestring=[b]",%
   morestring=[b]',%
  }%[keywords,comments,strings]%
\lstset{
  language=myjava,
  keywordstyle=\bfseries,
  ndkeywordstyle=\ttfamily,
  stringstyle=\color{magenta},
  identifierstyle=\color{blue}\ttfamily,
  extendedchars=true,
  flexiblecolumns=true,
  showstringspaces=false,
  texcl=true}

\newcommand{\java}[1]{\lstinline!#1!}
\lstnewenvironment{Java}{}{}
\newcommand{\inputJava}[1]{%
  \lstinputlisting[%
  basicstyle=\small,%
  backgroundcolor=\color{background},%
  frame=single,%
  frameround=tttt%
  ]{#1}%
}
\newcommand{\inputJavaSimple}[1]{%
  \lstinputlisting[%
  numbers=left,
  numberstyle=\tiny,
  ]{#1}%
}
\newcommand{\inputJavaCorrection}[1]{%
  \ifenstb@correction%
  {\color{black}%
  \lstinputlisting[%
  basicstyle=\small,%
  backgroundcolor=\color{backgroundCorrection},%
  frame=single,%
  rulecolor=\color{red},%
  frameround=tttt%
  ]{#1}%
  }%
  \fi%
}
\newcommand{\inputLineJava}[1]{%
  \lstinputlisting[%
  numbers=left,%
  stepnumber=5,%
  numbersep=5pt,%
  numberstyle=\small,%
  basicstyle=\small,%
  backgroundcolor=\color{background},%
  frame=single,%
  frameround=tttt%
  ]{#1}%
}


%% Page de titre
\renewcommand{\@title}{}
\newcommand{\short@title}{}
\newcommand{\@subtitle}{}
\renewcommand{\@date}{}

\newcommand{\correction}[1]{%
  \ifenstb@correction%
    {\color{red}#1}%
  \fi%
}
\newcommand{\inputCorrection}[1]{%
  \ifenstb@correction%
    {\color{red}\input{#1}}%
  \fi%
}

\ifenstb@correction%
  \newenvironment{Correction}{\color{red}}{\color{black}}
  \def\Sujet{\comment}
  \def\endSujet{\endcomment}
\else
  \def\Correction{\comment}
  \def\endCorrection{\endcomment}
  \newenvironment{Sujet}{}{}
\fi%

\renewcommand{\title}[2][]{%
  \renewcommand{\@title}{{#2}}%
  \def\opt{#1}%
  \ifx\opt\empty%
    \renewcommand{\short@title}{{#2}}%
  \else%
    \renewcommand{\short@title}{{#1}}%
  \fi
}
\newcommand{\subtitle}[1]{\renewcommand{\@subtitle}{{#1}}}
\renewcommand{\date}[1]{\renewcommand{\@date}{{#1}}}
\newcommand{\numero}[1]{\renewcommand{\@numero}{{#1}}}

\newcommand{\repTP}{\texttt{\@numero}}

\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{\unSchema}[3]{%
  \begin{figure}[htbp]
    \begin{center}
      \includegraphics{#1}
    \end{center}
    \caption{#2}
    \label{#3}
  \end{figure}
}

\renewcommand{\maketitle}
  {\par\begin{center}
        \includegraphics{logoenst}
    \vspace*{1.0em}\par
    {\color{blue}\LARGE\bf \@numero{} -- \@title}\\[2mm]
    \ifx\@subtitle\@empty\else%
    {\color{blue}\Large\bf \@subtitle}%
    \fi%
    \ifenstb@correction%
      \\[2mm]{\color{red}\Large\bf Correction}%
    \fi%
  \end{center}%
  \thispagestyle{plain}
  \addvspace{2em}}

% Afficher une commande ou une séquence de touches

\newcommand{\afaire}[1]{{\LARGE\textbf{\color{magenta}AFAIRE : #1}}}
\newcommand{\fluo}[1]{{\colorbox{yellow}{#1}}}

\AtBeginDocument{%
  \lhead{\small\textsc{\@matiere}}
  \chead{}
  \rhead{\small\@numero{}}
  \lfoot{\scriptsize ENST Bretagne -- \enstb@type{}}
  \cfoot{\textrm{\thepage{} sur {\color{black}\pageref{LastPage}}}}
  \rfoot{\scriptsize \@date}
  \colorlet{background}{gray!30}
  \colorlet{backgroundCorrection}{red!10}
}

%%% Environnement pour les exercices
\theoremstyle{plain}
\newtheorem{Exemple}{Exemple}

{\theorembodyfont{\normalfont\itshape} 
 \theoremstyle{nonumberbreak}
 \theoremseparator{ :}
 \newtheorem{remarque}{Remarque}
 \newtheorem{remarques}{Remarques}
}

\usepackage{newfile}
\newoutputstream{out}
\def\pscode#1#2{%
  \@bsphack
  \def\monnom{#1}
  \def\moncommentaire{#2}
  \openoutputfile{temp.out}{out}
  \let\do\@makeother\dospecials
  \catcode`\^^M\active
  \verbatim@startline
  \verbatim@addtoline
  \verbatim@finish
  \def\verbatim@processline{%
    \immediate\write\@nameuse{outoutstre@m}{\the\verbatim@line}}%
  \verbatim@start}
\def\endpscode{
  \closeoutputstream{out}
  \immediate\write18{perl buildmacro.pl \monnom}
  \@esphack
  \unSchema{\monnom}{\moncommentaire}{fig:\monnom}
}

\newcounter{fileCounter}
\newcommand{\newFileName}{
  \stepcounter{fileCounter}
  diag\thefileCounter
}

\def\herepscode#1{%
  \@bsphack
  \def\monnom{#1}
  \openoutputfile{temp.out}{out}
  \let\do\@makeother\dospecials
  \catcode`\^^M\active
  \verbatim@startline
  \verbatim@addtoline
  \verbatim@finish
  \def\verbatim@processline{%
    \immediate\write\@nameuse{outoutstre@m}{\the\verbatim@line}}%
  \verbatim@start}
\def\endherepscode{
  \closeoutputstream{out}
  \immediate\write18{perl buildmacro.pl \monnom}
  \@esphack
  \begin{center}
    \includegraphics{\monnom}
  \end{center}
}

\newcounter{probleme}
\stepcounter{probleme}
\newcounter{problemequestion}
\newcounter{tempstotal}
\newcounter{maxtempstotal}
\newcommand{\SetTempsTotal}[1]{\setcounter{maxtempstotal}{#1}}

\newcounter{heures}
\newcounter{minutes}
\newcommand{\duree}{%
  \setcounter{heures}{\themaxtempstotal/60}%
  \setcounter{minutes}{\themaxtempstotal - \theheures * 60}%
  \theheures{}h\ifnum\c@minutes=0\else
     \ifnum\theminutes<10 0\theminutes\else\theminutes\fi\fi}

\AtEndDocument{%
  \ifnum\c@maxtempstotal=\c@tempstotal
  \else
    \@latex@warning{Vous devriez relancer LaTeX pour afficher le temps total correct.}
  \fi
  \if@filesw\immediate\write\@auxout{\string\SetTempsTotal{\the\c@tempstotal}}\fi
}

\newcommand{\mnToPt}[1]{%
 \fpRegSet{toto}{#1}%
 \fpRegSet{tutu}{6}%
 \fpRegDiv{toto}{tutu}%
 \fpRegRound{toto}{-1}%
 \fpRegGet{toto}{\bareme}%
 \bareme}

\newcommand{\Probleme}[2]{
  \section*{Exercice \theprobleme{} (\emph{\large #1}) \hfill [#2 mn -- \mnToPt{#2}/20]}
  \protected@edef\@currentlabel{\csname theprobleme\endcsname}%
  \stepcounter{probleme}
  \setcounter{problemequestion}{1}
  \addtocounter{tempstotal}{#2}%
}

\newcommand{\Question}{
  \subsection*{Question \theproblemequestion{}}
  \protected@edef\@currentlabel{\csname theproblemequestion\endcsname}%
  \stepcounter{problemequestion}
}

\newcommand{\quest}{\makebox[0pt][r]{$\rhd$\hspace*{1ex}}}
\newenvironment{question}{\bfseries\quest\hspace{-1ex}}{}
