% LaTeX PSL thesis cover template
% Copyright (c) 2026, Pierre Guillou
% All rights reserved.
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program 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 General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.
% If not, see <http://www.gnu.org/licenses/>.

\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage{psl-cover}

\RequirePackage{tabularx}
\RequirePackage{etoolbox}
\RequirePackage{iftex}
\RequirePackage{tikz}
\usetikzlibrary{backgrounds}

\definecolor{pslblue}{RGB}{36, 56, 141}

\newcommand*{\institute}[1]{\gdef\@institute{#1}}
\newcommand*{\specialty}[1]{\gdef\@specialty{#1}}
\newcommand*{\supervisor}[1]{\gdef\@supervisor{#1}}
\newcommand*{\doctoralschool}[2]{
  \gdef\@doctoralschoolname{#1}%
  \gdef\@doctoralschoolnumber{#2}
}
%% default values
\newcommand*{\@institute}{Mines Paris - PSL}
\newcommand*{\@specialty}{Sp\'{e}cialit\'{e} de l'ED}

\newcommand*{\@supervisor}{Prénom NOM}
\newcommand{\thesupervisor}{\@supervisor}

\newcommand*{\@doctoralschoolname}{Nom de l'ED}
\newcommand*{\@doctoralschoolnumber}{XXX}

\newcommand{\jurymember}[4]{
  \ifnum#1=1
  \gdef\@jiname{#2}\gdef\@jitt{#3}\gdef\@jirole{#4}
  \fi
  \ifnum#1=2
  \gdef\@jiiname{#2}\gdef\@jiitt{#3}\gdef\@jiirole{#4}
  \fi
  \ifnum#1=3
  \gdef\@jiiiname{#2}\gdef\@jiiitt{#3}\gdef\@jiiirole{#4}
  \fi
  \ifnum#1=4
  \gdef\@jivname{#2}\gdef\@jivtt{#3}\gdef\@jivrole{#4}
  \fi
  \ifnum#1=5
  \gdef\@jvname{#2}\gdef\@jvtt{#3}\gdef\@jvrole{#4}
  \fi
  \ifnum#1=6
  \gdef\@jviname{#2}\gdef\@jvitt{#3}\gdef\@jvirole{#4}
  \fi
  \ifnum#1=7
  \gdef\@jviiname{#2}\gdef\@jviitt{#3}\gdef\@jviirole{#4}
  \fi
  \ifnum#1=8
  \gdef\@jviiiname{#2}\gdef\@jviiitt{#3}\gdef\@jviiirole{#4}
  \fi
  \ifnum#1=9
  \gdef\@jixname{#2}\gdef\@jixtt{#3}\gdef\@jixrole{#4}
  \fi
  \ifnum#1=10
  \gdef\@jxname{#2}\gdef\@jxtt{#3}\gdef\@jxrole{#4}
  \fi
  \ifnum#1=11
  \gdef\@jxiname{#2}\gdef\@jxitt{#3}\gdef\@jxirole{#4}
  \fi
  \ifnum#1=12
  \gdef\@jxiiname{#2}\gdef\@jxiitt{#3}\gdef\@jxiirole{#4}
  \fi
}

%% empty default values
\newcommand*{\@jiname}{} \newcommand*{\@jitt}{} \newcommand*{\@jirole}{}
\newcommand*{\@jiiname}{} \newcommand*{\@jiitt}{} \newcommand*{\@jiirole}{}
\newcommand*{\@jiiiname}{} \newcommand*{\@jiiitt}{} \newcommand*{\@jiiirole}{}
\newcommand*{\@jivname}{} \newcommand*{\@jivtt}{} \newcommand*{\@jivrole}{}
\newcommand*{\@jvname}{} \newcommand*{\@jvtt}{} \newcommand*{\@jvrole}{}
\newcommand*{\@jviname}{} \newcommand*{\@jvitt}{} \newcommand*{\@jvirole}{}
\newcommand*{\@jviiname}{} \newcommand*{\@jviitt}{} \newcommand*{\@jviirole}{}
\newcommand*{\@jviiiname}{} \newcommand*{\@jviiitt}{} \newcommand*{\@jviiirole}{}
\newcommand*{\@jixname}{} \newcommand*{\@jixtt}{} \newcommand*{\@jixrole}{}
\newcommand*{\@jxname}{} \newcommand*{\@jxtt}{} \newcommand*{\@jxrole}{}
\newcommand*{\@jxiname}{} \newcommand*{\@jxitt}{} \newcommand*{\@jxirole}{}
\newcommand*{\@jxiiname}{} \newcommand*{\@jxiitt}{} \newcommand*{\@jxiirole}{}

\newcommand*{\pslassetspath}[1]{\gdef\@pslassetspath{#1}}
\newcommand*{\@pslassetspath}{.}

\newcommand{\frabstract}[1]{\long\gdef\@frabstract{#1}}
\newcommand*{\@frabstract}{}
\newcommand{\thefrabstract}{\@frabstract}
\newcommand{\enabstract}[1]{\long\gdef\@enabstract{#1}}
\newcommand*{\@enabstract}[1]{}
\newcommand{\theenabstract}{\@enabstract}
\newcommand*{\frkeywords}[1]{\gdef\@frkeywords{#1}}
\newcommand*{\@frkeywords}{\par}
\newcommand{\thefrkeywords}{\@frkeywords}
\newcommand{\enkeywords}[1]{\gdef\@enkeywords{#1}}
\newcommand*{\@enkeywords}[1]{\par}
\newcommand{\theenkeywords}{\@enkeywords}

\ifLuaTeX{}
\newfontfamily\pslfont{arial}
\newenvironment{pslfontenv}{\pslfont}{\par}
\else
\fi
\ifXeTeX{}
\newfontfamily\pslfont{Arial}
\newenvironment{pslfontenv}{\pslfont}{\par}
\else
\fi
\ifPDFTeX{}
\newenvironment{pslfontenv}{\fontfamily{phv}\selectfont}{\par}
\else
\fi

\newif\if@nomaketitle\@nomaketitlefalse
\DeclareOption{nomaketitle}{
  \@nomaketitletrue
}

\newif\if@cotutelle\@cotutellefalse
\DeclareOption{cotutelle}{
  \@cotutelletrue
}

\ProcessOptions{}\relax{}

\if@nomaketitle
\else
%% Override \maketitle to directly insert cover
\renewcommand*{\maketitle}{
  \pslcover{}
}
\fi

\if@cotutelle
\newcommand{\entitle}[1]{\gdef\@entitle{#1}}
\newcommand{\otherinstitute}[1]{\gdef\@otherinstitute{#1}}
\newcommand{\logootherinstitute}[1]{\gdef\@logootherinstitute{#1}}
\fi

%% Whole PSL cover, front + back
\newcommand*{\pslcover}{
  \AfterPreamble{\pslfrontcover{}}
  \AtEndDocument{\pslbackcover{}}
}

%% Front PSL cover
\newcommand*{\pslfrontcover}{
  \begin{pslfontenv}
    \thispagestyle{empty}
    \begin{tikzpicture}[overlay, remember picture,
      text centered, align=center]

      %% background PSL logo
      \node[on background layer, xshift=-35mm, yshift=106mm]
      at (current page.center) {\includegraphics[height=79mm]{\@pslassetspath/front-psl-logo.jpg}};

      %% node origin
      \coordinate (o) at (current page.center);

      %% institute
      \node[xshift=-84mm, yshift=70mm, anchor=north west] at (o) {
        \begin{minipage}[h]{.7\paperwidth}
          \color{pslblue}
          \fontsize{14}{17}\selectfont
          Pr\'{e}par\'{e}e \`{a} \@institute{}\unskip\strut\par
          \if@cotutelle
          Dans le cadre d'une cotutelle avec \@otherinstitute{}\unskip\strut\par
          \fi
        \end{minipage}
      };

      %% thesis title
      \if@cotutelle
      \node[yshift=33mm] at (o) {
        \begin{minipage}[h]{.85\paperwidth}
          \centering
          \fontsize{18}{22}\selectfont
          \textbf{\@title{}}\unskip\strut\par
          \vspace{5mm}
          \color{pslblue}
          \@entitle{}\unskip\strut\par
        \end{minipage}
      };
      \else
      \node[yshift=31mm] at (o) {
        \begin{minipage}[h]{.85\paperwidth}
          \centering
          \fontsize{18}{22}\selectfont
          \textbf{\@title{}}\unskip\strut\par
        \end{minipage}
      };
      \fi

      %% author
      \node[xshift=-85mm, yshift=5mm, anchor=north west] at (o) {
        \begin{minipage}[h]{.3\paperwidth}
          \pslbluelabel{Soutenue par}\unskip\strut\par
          \vspace{1mm}
          {\fontsize{16}{19}\selectfont\textbf{\@author{}}}\unskip\strut\par
          \vspace{1mm}
          {\fontsize{12}{15}\selectfont Le \@date{}}\unskip\strut\par
        \end{minipage}
      };

      %% thesis supervisor
      \node[xshift=-85mm, yshift=-19mm, anchor=north west] at (o) {
        \begin{minipage}[h]{.3\paperwidth}
          \pslbluelabel{Dirig\'{e}e par}
          \unskip\strut\par
          \vspace{1mm}
          {\fontsize{14}{17}\selectfont\textbf{\@supervisor{}}}
          \unskip\strut\par
        \end{minipage}
      };

      %% doctoral school
      \node[xshift=-85mm, yshift=-49mm, anchor=north west] at (o) {
        \begin{minipage}[h]{.3\paperwidth}
          \pslbluelabel{\'{E}cole doctorale n\textsuperscript{o}\@doctoralschoolnumber{}}
          \unskip\strut\par
          \vspace{1mm}
          {\fontsize{14}{17}\selectfont\textbf{\@doctoralschoolname{}}}
          \unskip\strut\par
        \end{minipage}
      };

      %% doctoral specialty
      \node[xshift=-85mm, yshift=-81mm, anchor=north west] at (o) {
        \begin{minipage}[h]{.3\paperwidth}
          \pslbluelabel{Sp\'{e}cialit\'{e}}
          \unskip\strut\par
          \vspace{1mm}
          {\fontsize{14}{17}\selectfont\textbf{\@specialty{}}}
        \end{minipage}
      };

      %% jury
      \node[fill=lightgray!40, xshift=-8mm, yshift=-3mm, anchor=north west] at (o) {
        \begin{minipage}[ht]{90mm}
          \vspace{2mm}
          {\hspace{1mm}\color{pslblue}\fontsize{12}{15}\selectfont Composition du jury :}

          \vspace{5mm}
          \hspace{-2mm}
          \fontsize{9}{11}\selectfont
          \begin{tabularx}{93mm}{p{56mm}X}
            \psljurystyle{\@jiname}{\@jitt}{\@jirole}
            \psljurystyle{\@jiiname}{\@jiitt}{\@jiirole}
            \psljurystyle{\@jiiiname}{\@jiiitt}{\@jiiirole}
            \psljurystyle{\@jivname}{\@jivtt}{\@jivrole}
            \psljurystyle{\@jvname}{\@jvtt}{\@jvrole}
            \psljurystyle{\@jviname}{\@jvitt}{\@jvirole}
            \psljurystyle{\@jviiname}{\@jviitt}{\@jviirole}
            \psljurystyle{\@jviiiname}{\@jviiitt}{\@jviiirole}
            \psljurystyle{\@jixname}{\@jixtt}{\@jixrole}
            \psljurystyle{\@jxname}{\@jxtt}{\@jxrole}
            \psljurystyle{\@jxiname}{\@jxitt}{\@jxirole}
            \psljurystyle{\@jxiiname}{\@jxiitt}{\@jxiirole}
          \end{tabularx}
          \vspace{-3mm}
        \end{minipage}
      };

      %% PSL institute logo
      \node[xshift=-57mm, yshift=-122mm] at (o) {
        \includegraphics[height=35mm]{\@pslassetspath/logo-institute.png}
      };

      %% other institute logo (cotutelle)
      \if@cotutelle
      \node[xshift=65mm, yshift=125mm] at (o) {
        \includegraphics[height=1.8cm]{\@logootherinstitute}
      };
      \fi

    \end{tikzpicture}
  \end{pslfontenv}
  \clearpage
  \mbox{}
  \thispagestyle{empty}
  \clearpage
  \setcounter{page}{1}
}

%% Back PSL cover
\newcommand*{\pslbackcover}{
  \pagestyle{empty}
  \cleardoublepage{}
  \ifodd\value{page}\hbox{}\newpage\fi
  \begin{pslfontenv}
    \begin{tikzpicture}[overlay, remember picture,
      text centered, align=center]

      %% node origin
      \coordinate (o) at (current page.center);

      %% background images (PSL logo and blue semi-circle)
      \node[on background layer, xshift=44mm, yshift=26.5mm] at (current page.south west)
      {\includegraphics[height=27mm]{\@pslassetspath/back-psl-logo.jpeg}};

      \fontsize{11}{13}\selectfont
      %% french keywords
      \node[yshift=30mm, above] (frKeywords) at (o) {
        \pslbacknode{MOTS CL\'{E}S}{\@frkeywords{}}{10}{12}
      };
      %% french abstract
      \node[yshift=2mm, above] at (frKeywords.north) {
        \pslbacknode{R\'{E}SUM\'{E}}{\@frabstract{}}{9}{11}
      };
      %% english abstract
      \node[yshift=20mm, below] (enAbstract) at (o) {
        \pslbacknode{ABSTRACT}{\@enabstract{}}{9}{11}
      };
      %% english keywords
      \node[yshift=-2mm, below] at (enAbstract.south) {
        \pslbacknode{KEYWORDS}{\@enkeywords{}}{10}{12}
      };

    \end{tikzpicture}
  \end{pslfontenv}
}

%% Some text styling
\newcommand{\pslbluelabel}[1]{{\color{pslblue}\fontsize{12}{15}\selectfont #1}}
\newcommand{\psljurystyle}[3]{
  \ifdefempty{#1}{}{
    \fontsize{10}{12}\selectfont #1 & \\
    #2 & \emph{#3} \\[3.5mm]
  }
}
\newcommand{\pslbacknode}[4]{
  \begin{minipage}[ht]{.8\paperwidth}
    {\color{pslblue}\fontsize{12}{15}\selectfont #1}\unskip\strut\par
    \vspace{-2.5mm}
    \tikz{\draw[pslblue, line width=.5mm] (0, 0) -- (16.8, 0);}

    \vspace{3mm}
    {\color{black}\fontsize{#3}{#4}\selectfont #2\par}\unskip\strut\par
  \end{minipage}
}

\endinput
