%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definition
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{pletter}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Required Packages
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\RequirePackage{ifthen}
\RequirePackage{pifont}
\RequirePackage{babel}

\renewcommand{\familydefault}{cmss}
%\renewcommand{\rmdefault}{pag}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% German 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\lettergerman}{
	\def\myrefname{\textsl{Mein Zeichen}}
	\def\yrefname{\textsl{Ihr Zeichen}}
	\def\ymailname{\textsl{Ihre Nachricht vom}}
	\def\subjectname{\underline{Betrifft}}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% English
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\letterenglish}{
	\def\myrefname{\textsl{My Ref.}}
	\def\yrefname{\textsl{Your Ref.}}
	\def\ymailname{\textsl{Your letter from}}
	\def\subjectname{\underline{Subject}}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% French
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\letterfrench}{
	\def\myrefname{\textsl{Ma r\'ef\'erence}}
	\def\yrefname{\textsl{Votre r\'ef\'erence}}
	\def\ymailname{\textsl{Votre lettre du}}
	\def\subjectname{\underline{Concerne}}
}

\DeclareOption{german} {
	\lettergerman
}
\DeclareOption{english} {
	\letterenglish
}
\DeclareOption{french} {
	\letterfrench
}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{letter}}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{babel}}
\ExecuteOptions{german}
\ProcessOptions
\LoadClass[a4paper,11pt]{letter}

\setlength{\hoffset}{-1in}
\setlength{\voffset}{-1in}

\setlength{\topmargin}{10mm}
\setlength{\marginparwidth}{20mm}

\setlength{\oddsidemargin}{30mm}
\setlength{\evensidemargin}{26mm}

\setlength{\textwidth}{154mm}
\setlength{\textheight}{250mm}

\setlength{\headheight}{30pt}
\setlength{\headsep}{30pt}

\newdimen\leftfield
\setlength{\leftfield}{90mm}

\newdimen\rightfield
\setlength{\rightfield}{52mm}

\newcommand{\myname}{Christian Ribeaud}
\newcommand{\mystreet}{Kapfrain 2/2}
\newcommand{\mytown}{DE-79588 \underline{Efringen-Kirchen}}
\newcommand{\mycall}{\ding{38} +41-78-729 32 48}
\newcommand{\myemail}{chri@ribeaud.ch}

\let\name\@undefined
\let\fromname\@undefined

\let\address\@undefined
\let\fromaddress\@undefined

\let\location\@undefined
\let\fromlocation\@undefined

\let\telephone\@undefined
\let\telephonenum\@undefined

\def\ps@firstpage {
	\def\@oddhead {
		\parbox[b]{\leftfield} {
			\newfont{\mbf}{cmssbx10 scaled 2488}
			\mbf \myname\\[-8pt]
			\rule{\leftfield}{.4pt}}
			\hfill
		\parbox[b]{\rightfield} {
			\fontsize{9}{12pt}\sffamily
			\mystreet\\ \mytown\\ \mycall\\ e-mail:~\myemail
		}
	}
	\def\@oddfoot {}
	\def\@evenhead {}
	\def\@evenfoot{}
}

\def\ps@headings {
	\def\@oddhead {
		\parbox{\textwidth} {
			{\usefont{OT1}{cmss}{m}{n}\myname}\\[8pt]
			\normalfont\slshape \headtoname\ \ignorespaces\toname
			\hfill \@date \hfill \pagename\ \thepage\\[-8pt]
			\rule{\textwidth}{.4pt}
		}
	}
	\let\@evenhead=\@oddhead
	\def\@oddfoot{}
	\let\@evenfoot=\@oddfoot
}

\ps@headings

\def\opening#1{
	\thispagestyle{firstpage} {
		\raggedleft\@date\par
	}
	\vspace{2\parskip}
	{\raggedright\toname\\ \toaddress \par}
	\refbox\vspace{2\parskip} #1\par\nobreak
}

\long\def\closing#1 {
	\par\nobreak\vspace{\parskip}
	\stopbreaks \noindent \hspace*{\longindentation}
	\parbox{\indentedwidth}{
		\centering
		\ignorespaces #1\\[6\medskipamount]
		\ifthenelse {
			\equal{\fromsig}{}
		}
		{\myname}
		{\fromsig}
		\strut
	}
	\par
}

\newcommand*{\@myref}{}
\newcommand*{\@yref}{}

\newcommand*{\@ymail}{}
\newcommand*{\@subject}{}

\newcommand*{\myref}[1]{
	\renewcommand*{\@myref}{\myrefname:\ #1}
}
\newcommand*{\yref}[1]{
	\renewcommand*{\@yref}{\yrefname:\ #1}
}
\newcommand*{\ymail}[1]{
	\renewcommand*{\@ymail}{\ymailname:\ #1}
}
\newcommand*{\subject}[1]{
	\renewcommand*{\@subject}{\subjectname:\ #1}
}

\newcommand{\refbox} {
	\setbox0\vbox {
		\makebox[\textwidth][1] {
			\ifthenelse{\equal{\@ymail}{}}{}{\@ymail\hfill}
			\ifthenelse{\equal{\@yref}{}}{}{\@yref\hfill}
			\@myref\hfill
		}
		\@subject
	}
	\ifthenelse{\lengthtest{\ht0 >\z@}}{\vspace{2\parskip} \box0}{}
}