%% ---------------------------------------------------------------------------------------------
%% LaTeX Base Class Reykjavík University Theses/Dissertations/Books,
%% ---------------------------------------------------------------------------------------------
%% Original Author: Bjórn Þór Jónsson (bjorn AT ru.is)
%% Refactored/Simplified on 2020-02-03: Joseph Timothy Foley <foley AT ru.is>
%% Questions, comments, complaints: latex-help@LIST.RU.IS
%% Created on 2020-02-03
%% This code is distributed under the MIT license
%% Details at LICENSE.md

%%% WARNING:  This does not work without ruthesis.cls!!!

%% --------------- Identification ----------------------------
\newcommand{\fileversion@ruthesis}{$Revision: 361 $}
\newcommand{\filedate@ruthesis}{2020/02/03}% must be YYYY/MM/DD!
\newcommand{\fileauthor@ruthesis}{Joseph Timothy Foley, foley at RU.IS}
\ProvidesPackage{ruthesis}[\filedate@ruthesis by \fileauthor@ruthesis]
\NeedsTeXFormat{LaTeX2e}
\typeout{Thesis Class for Reykjavik University \fileversion@ruthesis <\filedate@ruthesis>, by \fileauthor@ruthesis}

%% ---------------- Execution of Options -----------------------------
%\ProcessOptions\relax% this stops searching for a * in \ProcessOtions*
%\LoadClassWithOptions{rubook}% our parent class.
\RequirePackage{rubook}%

%%%%%%%%%%%%%%%%%%%% Packages %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%% TextPos %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% These macros allow you to arbitrarily position things on the page
%% This is needed for any cover pages/graphics

%\RequirePackage{printlen}% display lengths in arbitrary units
%\ifbool{debug}{\PassOptionsToPackage{showboxes}{textpos}}
%\RequirePackage[absolute, verbose]{textpos}%

%% These commands set the units in \begin{textblock} when not 
%%  directly specified
%% We set them to the page sizes so we can align from them
%\setlength{\TPHorizModule}{\paperwidth}
%\setlength{\TPVertModule}{\paperheight}
%%\setlength{\TPMargin}{2mm} %% margins inside the textblock*
%\textblockorigin{0mm}{0mm} % start everything near the top-left corner


%%%%%%%%%%%%%%%%%%%% Addional Font Adjustments%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% rubook sets up some reasonable fonts, but we may need further adjustments
%%  Note that you can leave off elements and it will use the last value set (or default??)
% \def\myfont{\fontencoding{T1}\fontfamily{lm}\fontseries{m}\fontshape{n}\fontsize{14pt}{16pt}\selectfont}
% \fontsize{SIZE}{BASELINESKIP}

%% Default font is halfway between Times and Latin Modern
\RequirePackage{newtxtext}
\RequirePackage{newtxmath}

%% This is the special font used on the PhD cover
\def\phdcovertitlefont{\fontfamily{phv}\fontseries{b}\fontshape{n}\fontsize{20.74}{24}\selectfont}
\def\phdcovertitletext{\fontfamily{phv}\fontseries{m}\fontshape{n}\fontsize{14.4}{16}\selectfont}

%% We need the paralist and dashrule package in order to format the signature pages
\RequirePackage{paralist}
\RequirePackage{dashrule}

%% Information about Institution, Author, Degree, and Program
\newcommand{\theInstitution}{Reykjav\'{\i}k University}%
\newcommand{\theInstitutionAddress}{Menntavegur 1\\101 Reykjavík, Iceland}
\newcommand{\theInstitutionPhone}{+354 599 6200}
\newcommand{\theInstitutionURL}{www.ru.is}%

\newcommand{\DegreeAndDepartment@ruthesis}{\DegreeLong@ruthesis{} (\DegreeAbbrv@ruthesis)  in \Program@ruthesis{}}

\newcommand{\DocumentType@ruthesis}{Dissertation}
\newcommand{\DegreeAbbrv@ruthesis}{Ph.D.}
\newcommand{\DegreeLong@ruthesis}{Doctorate}
\newcommand{\Program@ruthesis}{\LaTeX{} Magic}
\newcommand{\DocumentInfo}[4]{
  \renewcommand{\DocumentType@ruthesis}{#1}
  \renewcommand{\DegreeAbbrv@ruthesis}{#2}
  \renewcommand{\DegreeLong@ruthesis}{#3}
  \renewcommand{\Program@ruthesis}{#4}
}

%ECTS Only applicable on BSc. and MSc.
\newcommand{\ects}[1]{\newcommand{\ects@ruthesis}{#1}}
\newcommand{\School@ruthesis}{School of Awesomeness}
\newcommand{\School}[1]{\renewcommand{\School@ruthesis}{#1}}


\newcommand{\supervisors@ruthesis}{\item No Advisor}
\newcommand{\Supervisors}[1]{\renewcommand{\supervisors@ruthesis}[1]{#1}}
\newcommand{\supervisorheading@ruthesis}{Supervisor}
\newcommand{\SupervisorHeading}[1]{\renewcommand{\supervisorheading@ruthesis}{#1}}

\newcommand{\examiners@ruthesis}{\item No Examiner}
\newcommand{\Examiners}[1]{\renewcommand{\examiners@ruthesis}[1]{#1}}
\newcommand{\examinerheading@ruthesis}{Examiner}
\newcommand{\ExaminerHeading}[1]{\renewcommand{\examinerheading@ruthesis}{#1}}

\newcommand{\personinfo}[5]{
\item[]  #1, #2\\#3, #4, #5 %Stub to be customized later
}

\newenvironment{peoplelist}
{\begin{compactitem}[\hspace{10mm}]}
{\end{compactitem}}

\newenvironment{rolelist}
{\begin{compactitem}[\hspace{10mm}]}
{\end{compactitem}}


%% Memoir titlepage definition and info at p.64
%% maketitlehookc is between the author and the date
\newcommand{\thetitleIS}{**Title in Icelandic**}
\newcommand{\titleIS}[1]{\renewcommand{\thetitleIS}{#1}}
\newcommand{\thedateIS}{**Date in Icelandic**}
\newcommand{\dateIS}[1]{\renewcommand{\thedateIS}{#1}}


%% Clean up some formatting for the text
\renewcommand{\thepage}{\roman{page}}

%% Footnote adjustments. These are mostly unnecessary
% \let\footnoterule\relax \null % Turn off the footnote rule line
%\renewcommand{\thefootnote}{\scriptsize\arabic{footnote}} %to fix large footnote text errors in some documents

\renewcommand{\bibname}{Bibliography} %Renaming the bibliography section (if omitted it will be named "references")

%%%%%%%%%%%%%%%% MARGINS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% WARNING: memoir class has its own margin macros
%%    Do not use the geometry package!!!!

% \setlength{\TPMargin}{0mm} %% margins inside the textblock*

\semiisopage
%   % The MS word template uses even 25.4mm margins, this is not as good if is bound
\setlrmarginsandblock{30mm}{25.4mm}{*}
%   % {left}{right}{ratio}, * for calculate
%%% if you need it to be even (for online), use this
\ifbool{online}{\setulmarginsandblock{25.4mm}{25.4mm}{*}}
%   % {upper}{lower}{ratio} * for calculate
\checkandfixthelayout% implement the margins

%% Some debugging code to check length calculation.
\newcommand{\showlengths@ruthesis}{
  \uselengthunit{mm} % showlen package
  Lengths: (with round off error from floats)\\
  covermarginl:\printlength{\covermarginl}\\
  covermarginr:\printlength{\covermarginr}\\
  covermargint:\printlength{\covermargint}\\
  covermarginb:\printlength{\covermarginb}\\
  coverwindowheight:\printlength{\coverwindowheight}\\
  pagecenterx:\printlength{\pagecenterx}\\
  pagecentery:\printlength{\pagecentery}\\
}


%% ----- PRINTED FRONT COVER ----------------------------------------------------------
%% This cover will be on the outside (not with window)
% \newcommand{\printedcover}{
%   \begin{covernonum}
%     \setlength{\TPMargin}{0mm} %% margins inside the textblock*
%     \sffamily    
%     \begin{textblock*}{\textwidth}[0.5,0.5](\pagecenterx,68mm)%
%       {\centering\includegraphics[width=55mm]{\frontlogo@ruthesis}\\}%
%     %
%     \setSingleSpace{1.8}\SingleSpacing%
%     \begin{textblock*}{\textwidth}[0,0](\covernowinmarginl,120mm)%
%       \textcolor{rugray}{\frontcovertitlefont\titleCover@ruthesis}\par%
%       %% Originally title was forced uppercase, but no-one seems to have
%       %%   noticed, so I'm removing it.
%     \end{textblock*}%
%     %
%     \setSingleSpace{1.5}\SingleSpacing%    
%     \begin{textblock*}{\textwidth}[0,0](\covernowinmarginl,222mm)%
%       \begin{minipage}{\textwidth}
%         \textcolor{rugray}{ 
%           \frontcovertitletext{}
%           \textbf{\theauthor}\\% 
%           \DegreeLong@ruthesis\\% 
%           \whensigned@ruthesis\\%
%           \School@ruthesis\\%
%           \theInstitution%        
%         }\par
%         \vspace*{5mm}
%         \textcolor{rured}{
%           \frontcovertitlefont{}
%           \documenttype@ruthesis
%         }\par
%         \vspace*{3mm}
%         {\scriptsize\color{rugray}\Bookidnumber}%                 
%       \end{minipage}
%     \end{textblock*}        
%   \setSingleSpace{1.0}
%   \end{covernonum}
%   \newpage\quad%% I am not sure why we must put this to ensure that the covers don't overlap
%   \newpage  % without this, page numbers show up.  Weird.
% }
  

\newcommand{\placelogo@ruthesis}[2]{%
  \begin{textblock*}{\textwidth}[0.5,0.5](\windowcenterx,#1)%
    {\centering\includegraphics[width=#2]{\frontlogo@ruthesis}\\}%
  \end{textblock*}
}

%% ----- INTERNAL TITLE PAGE WITH COMMITTEE ---------------------------------------------------
%% TODO: make counter for supervisors in the very rare case that there
%% is only one supervisor

\renewcommand{\maketitlehookc}{\begin{center}\DegreeAbbrv@ruthesis{} \DocumentType@ruthesis{} %
         \ifdef\ects@ruthesis{of \ects@ruthesis{} ECTS credits}%
         submitted to the \School@ruthesis\ \\%
         at \theInstitution{} in partial fulfillment\\% 
         of the requirements for the degree of \\%
         \textbf{\DegreeLong@ruthesis}\par\end{center}}

%% maketitlehookd is after the date
\renewcommand{\maketitlehookd}{{\titletextfont%
  \vspace{2\baselineskip}
  % change \personinfo to make the entries needed
  % Because this is inside a command, we escape the #1 as ##1      
  \renewcommand{\personinfo}[5]{\item[]  ##1, ##2\\##3, ##4, ##5}
  \setlength{\pltopsep}{0.5\baselineskip}  % first/top space
  \setlength{\plparsep}{0.5\baselineskip} % space between entries
  \setlength{\plitemsep}{0.5\baselineskip} % space between lines
  \begin{rolelist}
  \item \supervisorheading@ruthesis{}:
    \begin{peoplelist}
      \supervisors@ruthesis{}
%    \end{peoplelist}
%  \item \examinerheading@ruthesis{}:%% Old MSc template
%    \begin{peoplelist}
%      \examiners@ruthesis{}
    \end{peoplelist}
  \end{rolelist}
}\vfill}


%% ----- SIGNATURE PAGE, ONLY THESIS -----------------------------------------------------

\newlength{\signaturey}
\setlength{\signaturey}{8mm}
\newlength{\signaturex}
\setlength{\signaturex}{100mm}

\newcommand{\signaturepage}{
  \begin{cover}
    The undersigned hereby certify that they recommend to the
    \School@ruthesis{} at \theInstitution{} for acceptance this
    \DocumentType@ruthesis{} entitled \textbf{\thetitle}
    submitted by \textbf{\theauthor} in partial fulfillment
    of the requirements for the degree of \textbf{\DegreeAndDepartment@ruthesis}%
    \vspace*{2\baselineskip}
    
    \newcommand{\signline}[2]{\item \hdashrule{##2}{1pt}{1pt}\\##1}
    \renewcommand{\personinfo}[5]{\item\hdashrule{\signaturex}{1pt}{1pt}\\##1, ##2\\##3, ##4, ##5\\\vspace{\signaturey}\\}
    \setlength{\pltopsep}{2\baselineskip}  % first/top space
    \setlength{\plparsep}{2\baselineskip} % space between entries
    \setlength{\plitemsep}{0.5\baselineskip} % space between lines
    {\sigtextfont% 
      % change \personinfo to make the entries needed
      % Because this is inside a command, we escape the #1 as ##1
      \renewcommand{\personinfo}[5]{&\hdashrule{\signaturex}{1pt}{1pt}\\&##1\\\vspace{\signaturey}\\}
      % \begin{compactitem}[]
      %   \signline{date}{50mm}
      % \end{compactitem}
      \begin{tabular}{lll}
        Student:\\
        \personinfo{\theauthor}{}{}{}{}\\
        \supervisorheading@ruthesis:\\
        \supervisors@ruthesis{}\\
        %\examinerheading@ruthesis:\\%% MSc old template
        %\examiners@ruthesis{}
      \end{tabular}
    }% end sigtextfont      
  \end{cover}
}


%% ----- ARCHIVAL SIGNATURE, ONLY THESIS ----------------------------------------------------
%% TODO:  Icelandic translation?
\newcommand{\archivesigpage}{
  \begin{cover}
    \enableindents{}
    The undersigned hereby grants permission to
    the \theInstitution{} Library to reproduce single
    copies of this \DocumentType@ruthesis{} entitled \textbf{\thetitle}
    and to lend or sell such copies for private, scholarly or
    scientific research purposes only.
    
    The author reserves all other publication and
    other rights in association with the copyright in the
    \DocumentType@ruthesis{}, and
    except as herein before provided, neither the
    \DocumentType@ruthesis{} nor
    any substantial portion thereof may be printed or otherwise
    reproduced in any material form whatsoever without the
    author's prior written permission.
    
    \vspace*{2\baselineskip}
    \newcommand{\signline}[2]{\item \hdashrule{##2}{1pt}{1pt}\\##1}
    \setlength{\pltopsep}{2\baselineskip}  % first/top space
    \setlength{\plparsep}{2\baselineskip} % space between entries
    \setlength{\plitemsep}{0.5\baselineskip} % space between lines
    \begin{compactitem}[]
      \signline{Date}{50mm}
      \signline{\theauthor\\\DegreeLong@ruthesis}{0.75\textwidth}
    \end{compactitem}        
  \end{cover}
}


\renewenvironment{abstract}[3]
{\begin{coverleft}{\centering%
    {\titletitlefont#2\\}%%
    \vspace{\baselineskip}
    \theauthor\\%
    \vspace{\baselineskip}
    #3\\%
  }%
  \setlength{\parskip}{\baselineskip}%
  \par\vspace*{2\baselineskip}%
  \textbf{#1}\par\vspace{1ex}}
{\par\end{coverleft}\newpage}



%% ----- END OF FRONT MATERIALS ---------------------------------------------------------------
%\RequirePackage{stackengine}%% not sure why this is here
%% ---------------------------------------------------------------------------------------------
%% This command makes the back-cover materials
\newcommand{\backcover}{%
  \thispagestyle{empty}%
  \pagestyle{empty}%
  \cleartoevenpage{}% clear to chapter setting  
  \disableindents%
  \titletextfont\SingleSpacing%
  \vplace[0.8]{
    {\centering 
      \belowbaseline[-\normaltextsize]{
        \includegraphics[width=4cm]{\frontlogo@ruthesis} 
      }
      \quad
      \begin{minipage}[t]{10cm}%
        {\titletextfont\textcolor{rugray}{%
            \School\\            
            \Institution\\
            \InstitutionAddress\\
            \InstitutionPhone\\
            \InstitutionURL\\  %% NO \\, it is in the ID macro
            \Bookidnumber%
          }
        }            
      \end{minipage}\\% 
    }
  }
  \setlength{\TPMargin}{0mm}%% margins inside the textblock
  \enableindents{}%
}

%% ----- END OF BACK MATERIALS ---------------------------------------------------------------


%%% from the old cs template, if so desired
%\makepagestyle{oldstyle}
%\makeoddhead{oldstyle}{\footnotesize{\authorHeader@ruthesis}}{}{\footnotesize{\thepage}}
%\makeevenhead{oldstyle}{\footnotesize{\thepage}}{}{\footnotesize{\thetitle}}

