% Rolf Niepraschk, Rolf.Niepraschk@ptb.de

\setcounter{errorcontextlines}{100}  \listfiles

\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesClass{ptbletter}
    [2015/05/08 v0.1a PTB-Brief-Klasse (RN)]

\DeclareOption{german}{\def\ptb@lang{main=ngerman}}
\DeclareOption{ngerman}{\def\ptb@lang{main=ngerman}}
\DeclareOption{de}{\def\ptb@lang{main=ngerman}}
\DeclareOption{english}{\def\ptb@lang{main=english}}
\DeclareOption{en}{\def\ptb@lang{main=english}}

\DeclareOption{visualize}{%
  \PassOptionsToClass{\CurrentOption}{scrlttr2}
  \AtEndOfClass{%
    \showfields{head,address,location,refline,foot}%
    %\showfields{refline}%
    \addtokomafont{field}{\color{lightgray}}%
    \setshowstyle{rule}%
    %\setshowstyle{edges}%
    %\setshowstyle{frame}% default
  }
}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{scrlttr2}}

\ExecuteOptions{pagesize}
\ProcessOptions\relax

\LoadClass[%
,fontsize=10pt
,DINmtext
,fromrule=off% ???
,backaddress=plain
,refline=nodate
]{scrlttr2}

\RequirePackage{hyphsubst}% Trennregeln austauschen
\HyphSubstIfExists{ngerman-x-latest}{%
  \HyphSubstLet{ngerman}{ngerman-x-latest}}{}
\HyphSubstIfExists{german-x-latest}{%
  \HyphSubstLet{german}{german-x-latest}}{}

\RequirePackage{ifxetex,ifluatex,url}
\newif\ifXeOrLua \XeOrLuafalse
\ifxetex\XeOrLuatrue\else\ifluatex\XeOrLuatrue\fi\fi

\ifXeOrLua% XeLaTeX oder luaLaTeX
  \RequirePackage{fontspec}
  \setmainfont{TeX Gyre Pagella}[Ligatures=TeX]
  \setsansfont{TeX Gyre Heros}[Scale=MatchUppercase,Ligatures=TeX]
  \setmonofont{AnonymousPro-Regular}[%
    Extension = .ttf,
    BoldFont = AnonymousPro-Bold,
    ItalicFont = AnonymousPro-Italic,
    BoldItalicFont = AnonymousPro-BoldItalic,
    Scale     = MatchLowercase
  ]
\else
  \RequirePackage[utf8]{inputenc}
  \RequirePackage[T1]{fontenc}
  \RequirePackage{textcomp}
  \RequirePackage{mathpazo}
  \RequirePackage{tgpagella}
  \RequirePackage{tgheros}
  \RequirePackage[ttdefault=true,scale=1.034]{AnonymousPro}
\fi

%\RequirePackage{etoolbox}
%\preto\@classoptionslist{ngerman,}
% Ohne Sprachangabe "deutsch" verwenden. Bessere Lösung?

\RequirePackage[\ptb@lang]{babel}
\RequirePackage{iflang}
\IfLanguageName{ngerman}{%
  \typeout{german}
  \RequirePackage[babel=once,german=guillemets]{csquotes}
  \AtEndOfClass{\shorthandon{"}}
  \defineshorthand{"`}{\openautoquote}
  \defineshorthand{"'}{\closeautoquote}
%  \MakeAutoQuote{»}{«}
}{%
  \typeout{english}
  \RequirePackage[babel=once]{csquotes}
}
\@setplength{toaddrhpos}{72bp}
\@setplength{toaddrvpos}{123bp}
%\@setplength{toaddrwidth}{85mm}

\@setplength{lochpos}{-354bp}% "-" --> vom linken Blattand aus
\@setplength{locvpos}{146bp}%      --> vom oberen Blattand aus
\@setplength{locwidth}{212bp}
\@setplength{locheight}{132bp}

\@setplength{refaftervskip}{75bp}

%\@setplength{refvpos}{1sp}
%\@setplength{refhpos}{1sp}
%\@setplength{refwidth}{110bp}
\removereffields

\setkomavar{backaddressseparator}{\,\,\textbullet\,\,}

\addtokomafont{addressee}{\sffamily}
\addtokomafont{refvalue}{\normalfont}

\setkomavar{location}{% TODO: Tabelle!
\raggedright\usekomafont{refvalue}%
{\usekomafont{refname}\usekomavar*{yourref}:}~\usekomavar{yourref}\\
{\usekomafont{refname}\usekomavar*{yourmail}:}~\usekomavar{yourmail}\\
{\usekomafont{refname}\usekomavar*{myref}:}~\usekomavar{myref}\\
{\usekomafont{refname}\usekomavar*{mymail}:}~\usekomavar{mymail}\\
~\\
{\usekomafont{refname}\usekomavar*{handledby}:}~\usekomavar{handledby}\\
{\usekomafont{refname}\usekomavar*{myphone}:}~\usekomavar{myphone}\\
{\usekomafont{refname}\usekomavar*{myfax}:}~\usekomavar{myfax}\\
{\usekomafont{refname}\usekomavar*{fromemail}:}~\usekomavar{fromemail}\\
~\\
{\usekomafont{refname}\usekomavar*{date}:}~\usekomavar{date}
}

\newkomavar[\mymailname]{mymail}
\newkomavar[\myphonename]{myphone}
\newkomavar[\myfaxname]{myfax}
\newkomavar[\handledbyname]{handledby}

\defcaptionname{american,australian,british,canadian,%
    english,newzealand,UKenglish,USenglish}\yourrefname{Your reference}%
\defcaptionname{american,australian,british,canadian,%
  english,newzealand,UKenglish,USenglish}\yourmailname{My letter of}%
\defcaptionname{american,australian,british,canadian,%
  english,newzealand,UKenglish,USenglish}\myrefname{My reference}%
\defcaptionname{american,australian,british,canadian,%
  english,newzealand,UKenglish,USenglish}\myphonename{Telephone}%
\defcaptionname{american,australian,british,canadian,%
  english,newzealand,UKenglish,USenglish}\myfaxname{Fax}%
\defcaptionname{american,australian,british,canadian,%
  english,newzealand,UKenglish,USenglish}\emailname{E-mail}%
\defcaptionname{american,australian,british,canadian,%
  english,newzealand,UKenglish,USenglish}\handledbyname{Handled by}%

\defcaptionname{german,ngerman,austrian,naustrian,%
  swissgerman,nswissgerman}\yourmailname{Ihre Nachricht vom}%
\defcaptionname{german,ngerman,austrian,naustrian,%
  swissgerman,nswissgerman}\myrefname{Mein Zeichen}%
\defcaptionname{german,ngerman,austrian,naustrian,%
  swissgerman,nswissgerman}\mymailname{Meine Nachricht vom}%
\defcaptionname{german,ngerman,austrian,naustrian,%
  swissgerman,nswissgerman}\myphonename{Telefondurchwahl}%
\defcaptionname{german,ngerman,austrian,naustrian,%
  swissgerman,nswissgerman}\myfaxname{Telefaxdurchwahl}%
\defcaptionname{german,ngerman,austrian,naustrian,%
  swissgerman,nswissgerman}\handledbyname{Bearbeitet von}%

\endinput
