% Copyright 2008 by Nathanaël COTTIN <www.ncottin.net>.
%
% This program can be redistributed and/or modified under the terms
% of the GNU Public License, version 3.

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{smartletter}[2008/07/07 version 0.0.2]

\RequirePackage{ifthen}

\provideboolean{@french}
\provideboolean{@scientific}
\provideboolean{@addtitlespace}
\provideboolean{@pdf}

\DeclareOption{french}{\@frenchtrue}
\DeclareOption{scientific}{\@scientifictrue}
\DeclareOption{pdf}{\@pdftrue}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax

\RequirePackage{eqparbox}
\RequirePackage{vmargin}

\if@french
	\RequirePackage[french]{babel}
	\RequirePackage[T1]{fontenc}
	\RequirePackage[latin1]{inputenc}
\fi

\if@scientific
	\RequirePackage[scaled=.92]{helvet}
	\RequirePackage{courier}
\fi

%--------------------------------------
% This class makes use of 'article'
%--------------------------------------

\LoadClass[oneside]{article}

% Hyperref package must be inserted after article class loading
\if@pdf
	\RequirePackage[
		pdftex,
		bookmarks=false,
		colorlinks,
		breaklinks=true,
		citecolor=black,
		filecolor=black,
		linkcolor=black,
		urlcolor=black,
		pdfborder={0 0 0}
	]{hyperref}
\fi

% Change margins
% setpapersize is required as margins are modified by hyperref!!!
% (is this a bug ??)
\if@french
	\setpapersize{A4}
\fi

\def\@cvmargin{2cm}
\setmargnohfrb
	{\@cvmargin}%	Left margin
	{\@cvmargin}%	Top margin
	{\@cvmargin}%	Right margin
	{\@cvmargin}%	Bottom margin

%--------------------------------------

\def\@thename{}
\def\@thecompanyname{}
\def\@theaddress{}
\def\@thelocality{}
\def\@thecountry{}
\def\@thephone{}
\def\@theemail{}
\def\@thewebsite{}
\def\@thepurpose{}
\def\@thesignature{}
\def\@thesignatureextra{}

\def\@thedestinatoryname{}
\def\@thedestinatorycompanyname{}
\def\@thedestinatoryaddress{}
\def\@thedestinatorylocality{}
\def\@thedestinatorycountry{}

\def\@theletterdate{}
\def\@thereference{}
\def\@theattachment{}
\def\@theopening{}
\def\@theclosing{}
\def\@theextra{}

\def\@dest{to}
\def\@theref{Ref}
\def\@theatt{Att}
\def\@theobj{Subject}
\if@french
	\def\@dest{à}
	\def\@theref{Réf}
	\def\@theatt{PJ}
	\def\@theobj{Objet}
	\def\@theextra{}
\fi

\def\@thedefaultspace{6ex}
\def\@afternamespace{1ex}
\def\@therefspace{1ex}
\def\@thebeforesignaturespace{18ex}

% For pdf generation only
\def\@thekeywords{}

%--------------------------------------
% Available commands and environments
%--------------------------------------

\newcommand{\name}[1]
	{\def\@thename{#1}}

\newcommand{\companyname}[1]
	{\def\@thecompanyname{#1}}

\newcommand{\address}[1]
	{\def\@theaddress{#1}}

\newcommand{\locality}[1]
	{\def\@thelocality{#1}}

\newcommand{\country}[1]
	{\def\@thecountry{#1}}

\newcommand{\phone}[1]
	{\def\@thephone{#1}
	\@addtitlespacetrue}

\newcommand{\email}[1]
	{\def\@theemail{#1}
	\@addtitlespacetrue}

\newcommand{\website}[1]
	{\def\@thewebsite{#1}
	\@addtitlespacetrue}

\newcommand{\purpose}[1]
	{\def\@thepurpose{#1}}

\newcommand{\signature}[1]
	{\def\@thesignature{#1}}

\newcommand{\signatureextra}[1]
	{\def\@thesignatureextra{#1}}

\newcommand{\destinatoryname}[1]
	{\def\@thedestinatoryname{#1}}

\newcommand{\destinatorycompanyname}[1]
	{\def\@thedestinatorycompanyname{#1}}

\newcommand{\destinatoryaddress}[1]
	{\def\@thedestinatoryaddress{#1}}

\newcommand{\destinatorylocality}[1]
	{\def\@thedestinatorylocality{#1}}

\newcommand{\destinatorycountry}[1]
	{\def\@thedestinatorycountry{#1}}

\newcommand{\letterdate}[1]
	{\def\@theletterdate{#1}}

\newcommand{\reference}[1]
	{\def\@thereference{#1}}

\newcommand{\attachment}[1]
	{\def\@theattachment{#1}}

\newcommand{\opening}[1]
	{\def\@theopening{#1}}

\newcommand{\closing}[1]
	{\def\@theclosing{#1}}

\newcommand{\extra}[1]
	{\def\@theextra{#1}}

\newcommand{\interline}[1]
	{\renewcommand{\baselinestretch}{#1}}

\newcommand{\defaultspace}[1]
	{\def\@thedefaultspace{#1}}

\newcommand{\beforesignaturespace}[1]
	{\def\@thebeforesignaturespace{#1}}

\newcommand{\pdfkeywords}[1]
	{\def\@thekeywords{#1}}

%\newcommand{\margins}[1]
%	{\def\@cvmargin{#1}}

\newcommand{\urlref}[1]
	{\if@pdf
		\href{#1}{#1}
	\else
		#1
	\fi}

\newcommand{\emailref}[1]
	{\if@pdf
		\href{mailto:#1}{#1}
	\else
		#1
	\fi}

\newcommand{\terminate}
	{\@theclosing%
	\ifthenelse
		{\equal{\@thesignature}{}}
		{}
		{\@addspace{\@thebeforesignaturespace}%
		\\\@thesignature}%
	\ifthenelse
		{\equal{\@thesignatureextra}{}}
		{}
		{\\\@thesignatureextra}%
	\vfill}

%--------------------------------------
% Reserved commands and environments
%--------------------------------------

\newcommand{\@display}[1]
	{\ifthenelse
		{\equal{#1}{}}
		{}
		{\\#1}}

\newcommand{\@displaypurpose}
	{\ifthenelse
		{\equal{\@thepurpose}{}}
		{}
		{\@theobj: \@thepurpose}}

\newcommand{\@addspace}[1]
	{\vspace{#1}}

\renewcommand{\maketitle}
	{\begin{flushleft}
		\@thename%
		\@addspace{\@afternamespace}%
		\@display{\@thecompanyname}%
		\\\@theaddress%
		\\\@thelocality%
		\@display{\@thecountry}%
		\@display{\@thephone}%
		\ifthenelse
			{\equal{\@theemail}{}}
			{}
			{\@display{\emailref{\@theemail}}}%
		\ifthenelse
			{\equal{\@thewebsite}{}}
			{}
			{\@display{\urlref{\@thewebsite}}}%
	\end{flushleft}%
	\begin{center}
		\@dest
	\end{center}
	\begin{flushright}
		\begin{minipage}{.4\linewidth}
			\@thedestinatoryname
			\@addspace{\@afternamespace}
			\@display{\@thedestinatorycompanyname}
			\\\@thedestinatoryaddress
			\@display{\@thedestinatorylocality}
		\end{minipage}
	\end{flushright}
	\ifthenelse
		{\equal{\@theletterdate}{}}
		{}
		{\@addspace{\@thedefaultspace}
		\begin{flushright}
			\@theletterdate
		\end{flushright}}
	\@displaypurpose
	\@setextrainfo
	\vfill
	\@addspace{\@thedefaultspace}
	\@theopening
	\@addspace{\@thedefaultspace}}

\newcommand{\@setextrainfo}
	{\@display{\@theextra}%
	\newboolean{@hasref}%
	\ifthenelse
		{\equal{\@thereference}{}}
		{}
		{\@hasreftrue
		\@addspace{\@therefspace}\\%
		{\footnotesize\@theref: \@thereference}}
	\ifthenelse
		{\equal{\@theattachment}{}}
		{}
		{\if@hasref
		\else%
			\@addspace{\@therefspace}%
		\fi%
		{\\\footnotesize\@theatt: \@theattachment}}}

\newcommand{\@addsectiontitlespace}
	{\@addspace{\@sectiontitlespace}}

\newcommand{\@initpdf}
	{\if@pdf
		\hypersetup{%
			pdftitle={\@thepurpose},%
			pdfauthor={\@thename},%
			pdfkeywords={\@thekeywords},%
			pdfsubject={\@thereference}}
	\fi}

\newcommand{\@changefonts}
	{\if@scientific
		\renewcommand{\rmdefault}{cmss}
		\fontfamily{phv}
		\selectfont
	\fi}

\AtBeginDocument
	{\@changefonts%
	\@initpdf%
	\setlength{\parindent}{0pt}%
	\setlength{\parskip}{1ex}%
	\maketitle}

\endinput
