% cernletter.sty
% A modern CERN letter head
% 2018-09-05 by Axel Naumann, axel@cern.ch
% (c) CERN, 2018

% --- Class structure: identification part
% ---
\ProvidesClass{cernletter}[2008/09/05 version 0.01 CERN letterhead]
\NeedsTeXFormat{LaTeX2e}

% --- Class structure: declaration of options part
% ---
% This class extends the letter class
% Read all the documentclass options; pass them to letter,
% unless the file "<currentoption>.min" exists, then it is loaded.
% E.g. \documentclass[a4paper,11pt,robert]{cernletter}
% reads robert.min if it exists and passed a4paper and 11pt to
% the letter class.
\DeclareOption*{\InputIfFileExists{\CurrentOption.min}{}{
%
    \PassOptionsToClass{\CurrentOption}{letter}}
}

% --- Class structure: execution of options part
% ---
\ProcessOptions \relax

% --- Class structure: declaration of options part
% ---
%\LoadClass{letter}
\LoadClass[a4paper,
  fontsize=12pt,
  parskip=full,
  firstfoot=false]{scrlttr2}

\RequirePackage[utf8]{inputenc}
\RequirePackage[english]{babel}
\RequirePackage[T1]{fontenc}
\RequirePackage{paratype}
\RequirePackage{graphicx}
\RequirePackage{calc}
\RequirePackage{color}
\RequirePackage{ifthen}
\RequirePackage{scrlayer-scrpage}

\KOMAoptions{footwidth=\useplength{firstfootwidth}}
\KOMAoption{subject}{titled}

\ForEachLayerOfPageStyle*{plain.scrheadings}{%
  \ifstrstart{#1}{plain.scrheadings.foot}{%
    \ModifyLayer[
      voffset={\useplength{firstfootvpos}-1em}
    ]{#1}
  }{}
}% shift the footer down

%% \clearpairofpagestyles


\addtokomafont{pagehead}{\normalfont \sffamily}
\definecolor{CERNblue}{RGB}{0,93,171}
\setkomafont{pagefoot}{\usekomafont{pagehead} \color{CERNblue}}

\setkomavar{firsthead}{
  \begingroup
  \usekomafont{pagefoot}
  \begin{tabular}{l l}
    \vspace{1em}\\
    \raisebox{-.5\height}{%
      \includegraphics[width=2cm,height=2cm]{CERN-badge.pdf}%
    }
    &
    \parbox[t][2em][t]{\textwidth-3cm}{%
        European Organization for Nuclear Research\\
        \textit{Organisation europ\'eenne pour la recherche nucl\'eaire}%
    }%
  \end{tabular}%
  \endgroup
}

\chead*{
  \ifthenelse{\equal{\thepage}{1}}%
             {}%
             {- \thepage{} -}
}
\cfoot*{
  \begingroup
  \scriptsize
  \textbf{home.cern} %
  \hfill%
  \textbf{Switzerland} CH-1211 Geneva 23 | \textbf{France} F-01631 CERN-CEDEX
  \endgroup
}

\def\thecerngroup{}
\newcommand{\cerngroup}[1]{\def\thecerngroup{#1}}
\def\themailbox{}
\newcommand{\mailbox}[1]{\def\themailbox{#1}}

\setkomavar{fromaddress}{
  CERN \thecerngroup\\
  CH-1211 Geneva 23\\
  Switzerland
}
\setkomavar{place}{Geneva}

\pagestyle{plain}
\usepackage{xpatch}
\xapptocmd\opening{\thispagestyle{plain}}{}{}% <- first pages will have pagestyle plain too
\addtolength{\textheight}{40pt}
\@setplength{locvpos}{\useplength{toaddrvpos}}

\setkomavar{location}{%
  \scriptsize
  \begin{tabular}{l l}
    \multicolumn{2}{l}{\usekomavar{fromname}}\\
    \multicolumn{2}{l}{CERN \thecerngroup}\\
    \multicolumn{2}{l}{Mailbox \themailbox}\\
    \multicolumn{2}{l}{CH-1211 Geneva 23}\\
    \multicolumn{2}{l}{Switzerland}\\[1em]
    Tel.: & \usekomavar{fromphone}\\
    Email: & \usekomavar{fromemail}
  \end{tabular}
}
