%<identification>
\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{MultiLetter}[1995/05/01 Multiple Letter V1.0 from 1995/5/8 by Gundolf Frank]
\newcommand{\PackageName}{MultiLetter}
%<initial code>
\xdef\surnamestr{surname}
\xdef\firstnamestr{firstname}
\xdef\businessstr{business}
\xdef\divisionstr{division}
\xdef\streetstr{street}
\xdef\locationstr{location}
\xdef\telstr{telephone}
\xdef\faxstr{fax}
\xdef\emailstr{email}
\xdef\cosenamestr{cosename}
\xdef\birthdatestr{birthdate}
\xdef\paraerstr{firstsupplement}
\xdef\parazwstr{firstsupplement}
\xdef\contactstr{contact}
\xdef\sexstr{sex}
\newcommand{\adressfound}{One adress found:}
\newcommand{\rightadress}{Is this the right adress?(Yes/No)}
\def\YES{YES}
\def\yes{yes}
\def\YESshort{Y}
\def\yesshort{y}
\def\Yes{Yes}
\newcommand{\mrs}{Mistress}
\def\female{f}
\newcommand{\mr}{Sir}
\def\male{m}
\def\neutrale{n}
\newcommand{\co}{c/o}
\def\contactgood{friendly}
\def\contactbad{formal}
\xdef\openingbon{DLG}
\expandafter\def\csname\openingbon\endcsname{Dear Ladies and Gentlemen}
\xdef\openingbof{DM}
\expandafter\def\csname\openingbof\endcsname{Dear Madam}
\xdef\openingbom{DG}
\expandafter\def\csname\openingbom\endcsname{Dear Gentlemen}
\xdef\openingbf{DMrs}
\expandafter\def\csname\openingbf\endcsname{Dear Mrs.}
\xdef\openingbm{DMr}
\expandafter\def\csname\openingbm\endcsname{Dear Mr.}
\xdef\openingbn{D}
\expandafter\def\csname\openingbn\endcsname{}
\xdef\openinggf{hf}
\expandafter\def\csname\openinggf\endcsname{Hello}
\xdef\openinggm{hm}
\expandafter\def\csname\openinggm\endcsname{Hello}
\xdef\openinggn{h}
\expandafter\def\csname\openinggn\endcsname{Hello}
\xdef\closingb{Yf}
\expandafter\def\csname\closingb\endcsname{Yours faithfully}
\xdef\closingg{Yvt}
\expandafter\def\csname\closingg\endcsname{Yours very truly}
\newcommand{\mesa}{File \adressfilename\space opened.}
\newcommand{\mesb}{File \adressfilename\space closed.}
\newcommand{\errormesa}{No search option is given!}
\newcommand{\errorhelpa}{At \string\multiletter[options]{search options} you must define a search option.}
\newcommand{\errorhelpaa}{At \string\searchadress[options]{search options} you must define a search option.}
\newcommand{\errormesb}{File \adressfilename\space not found!}
\newcommand{\errorhelpb}{File *.adr with the complete adresses is missing.}
\newcommand{\warninga}{No adress found!}
\newcommand{\warnhelpa}{Please control the spelling in your adressfile\MessageBreak and in your letter.}

%<declaration of options>
\DeclareOption{german}{\xdef\surnamestr{Nachname}
                       \xdef\firstnamestr{Vorname}
                       \xdef\businessstr{Firma}
                       \xdef\divisionstr{Abteilung}
                       \xdef\streetstr{Stra�e}
                       \xdef\locationstr{Ort}
                       \xdef\telstr{Telefon}
                       \xdef\faxstr{Fax}
                       \xdef\emailstr{E-Mail}
                       \xdef\cosenamestr{Cosename}
                       \xdef\birthdatestr{Geburtsdatum}
                       \xdef\paraerstr{ersterZusatz}
                       \xdef\parazwstr{zweiterZusatz}
                       \xdef\contactstr{Kontakt}
                       \xdef\sexstr{Geschlecht}
                       \renewcommand{\adressfound}{Adresse gefunden:}
                       \renewcommand{\rightadress}{Ist dies die richtige Adresse?(Ja/Nein)}
                       \def\YES{JA}
                       \def\yes{ja}
                       \def\YESshort{J}
                       \def\Yes{Ja}
                       \def\yesshort{j}
                       \renewcommand{\mrs}{Frau}
                       \def\female{w}
                       \renewcommand{\mr}{Herrn}
                       \def\male{m}
                       \def\neutrale{n}
                       \renewcommand{\co}{z.H.}
                       \def\contactgood{herzlich}
                       \def\contactbad{formal}
                       \xdef\openingbon{sgDH}
                       \expandafter\def\csname\openingbon\endcsname{Sehr geehrte Damen und Herren}
                       \xdef\openingbof{sgDn}
                       \expandafter\def\csname\openingbof\endcsname{Sehr geehrte Damen}
                       \xdef\openingbom{sgHn}
                       \expandafter\def\csname\openingbom\endcsname{Sehr geehrte Herren}
                       \xdef\openingbf{sgF}
                       \expandafter\def\csname\openingbf\endcsname{Sehr geehrte}
                       \xdef\openingbm{sgH}
                       \expandafter\def\csname\openingbm\endcsname{Sehr geehrter}
                       \xdef\openingbn{sg}
                       \expandafter\def\csname\openingbn\endcsname{Sehr geehrtes}
                       \xdef\openinggf{le}
                       \expandafter\def\csname\openinggf\endcsname{Liebe}
                       \xdef\openinggm{lr}
                       \expandafter\def\csname\openinggm\endcsname{Lieber}
                       \xdef\openinggn{ls}
                       \expandafter\def\csname\openinggn\endcsname{Liebes}
                       \xdef\closingb{mfg}
                       \expandafter\def\csname\closingb\endcsname{Mit freundlichen Gr��en}
                       \xdef\closingg{mhg}
                       \expandafter\def\csname\closingg\endcsname{Mit herzlichen Gr��en}
                       \renewcommand{\mesa}{Datei \adressfilename\space ge�ffnet.}
                       \renewcommand{\mesb}{Datei \adressfilename\space geschlossen.}
                       \renewcommand{\errormesa}{Es wurde kein Suchkriterium angegeben!}
                       \renewcommand{\errorhelpa}{Bei \string\multiletter[Optionen]{suchkriterium} musz ein Suchkriterium angegeben\MessageBreak werden.}
                       \renewcommand{\errorhelpaa}{Bei \string\searchadress[Optionen]{suchkriterium} musz ein Suchkriterium angegeben\MessageBreak werden.}
                       \renewcommand{\errormesb}{Datei \adressfilename\space nicht gefunden!}
                       \renewcommand{\errorhelpb}{Datei mit der Endung 'adr' mit der kompletten Adressenliste wurde nicht\MessageBreak gefunden.}
                       \renewcommand{\warninga}{Keine passende Adresse gefunden!}
                       \renewcommand{\warnhelpa}{Bitte kontrollieren Sie die Schreibweisen in der\MessageBreak Adressenliste und im Brief.}
                       \def\Adresse{\address}
                      }
\DeclareOption*{\InputIfExists{\CurrentOption.STY}{}{}}
\ProcessOptions

%<package loading>
\RequirePackage{ifthen}

%<main code>
%Here you can change the filename, where your adresses are written in.
\providecommand{\adressfilename}{Brief.ADR}

\expandafter\def\csname\surnamestr\endcsname{\surname}
\expandafter\def\csname\firstnamestr\endcsname{\firstname}
\expandafter\def\csname\businessstr\endcsname{\business}
\expandafter\def\csname\divisionstr\endcsname{\division}
\expandafter\def\csname\streetstr\endcsname{\street}
\expandafter\def\csname\locationstr\endcsname{\location}
\expandafter\def\csname\telstr\endcsname{\tel}
\expandafter\def\csname\faxstr\endcsname{\fax}
\expandafter\def\csname\emailstr\endcsname{\emailname}
\expandafter\def\csname\cosenamestr\endcsname{\cosename}
\expandafter\def\csname\birthdatestr\endcsname{\birthdate}
\expandafter\def\csname\paraerstr\endcsname{\paraer}
\expandafter\def\csname\parazwstr\endcsname{\parazw}
\expandafter\def\csname\contactstr\endcsname{\contact}
\expandafter\def\csname\sexstr\endcsname{\sex}

\newboolean{mletterboo}
\setboolean{mletterboo}{false}
\newboolean{noteofile}
\setboolean{noteofile}{false}
\newboolean{listadress}
\setboolean{listadress}{true}

\DeclareRobustCommand{\searchadress}[2][empty]{%
   \let\crlf=\\%
   \let\\=\space%
   \def\option{#1}%
   \def\searchoption{#2}%
   \setboolean{noteofile}{true}%
   \ifthenelse{\equal{\searchoption}{}}%
    {\ifthenelse{\boolean{mletterboo}}%
       {\PackageError{\PackageName}{\errormesa}{\errorhelpa}}%
       {\PackageError{\PackageName}{\errormesa}{\errorhelpaa}}}{}%
   \ifthenelse{\boolean{mletterboo}}%
     {}%
     {\IfFileExists{\adressfilename}%
        {\openin7=\adressfilename%
         \typeout{Package \PackageName\space Info: \mesa}}%
        {\setboolean{noteofile}{false}%
         \PackageError{\PackageName}{\errormesb}{\errorhelpb}}}%
   \ifthenelse{\equal{\option}{empty} \or%
               \equal{\option}{\surnamestr}}%
     {\def\option{surname}}{}%
   \ifthenelse{\equal{\option}{\firstnamestr}}%
     {\def\option{firstname}}{}%
   \ifthenelse{\equal{\option}{\businessstr}}%
     {\def\option{business}}{}%
   \ifthenelse{\equal{\option}{\divisionstr}}%
     {\def\option{division}}{}%
   \ifthenelse{\equal{\option}{\streetstr}}%
     {\def\option{street}}{}%
   \ifthenelse{\equal{\option}{\locationstr}}%
     {\def\option{location}}{}%
   \ifthenelse{\equal{\option}{\telstr}}%
     {\def\option{tel}}{}%
   \ifthenelse{\equal{\option}{\faxstr}}%
     {\def\option{fax}}{}%
   \ifthenelse{\equal{\option}{\emailstr}}%
     {\def\option{email}}{}%
   \ifthenelse{\equal{\option}{\birthdatestr}}%
     {\def\option{birthdate}}{}%
   \ifthenelse{\equal{\option}{\cosenamestr}}%
     {\def\option{cosename}}{}%
   \ifthenelse{\equal{\option}{\paraerstr}}%
     {\def\option{paraer}}{}%
   \ifthenelse{\equal{\option}{\parazwstr}}%
     {\def\option{parazw}}{}%
   \ifthenelse{\equal{\option}{\sexstr}}%
     {\def\option{sex}}{}%
   \ifthenelse{\equal{\option}{\contactstr}}%
     {\def\option{contact}}{}%
   \def\test{---}%
   \whiledo{\boolean{noteofile}}%
    {\def\liofchar{}%
     \endlinechar=-1%
     \read7 to\liofchar%
     \endlinechar=`\^^M%
     \ifx\liofchar\test%
       \endlinechar=-1%
       \read7 to\surname%
       \read7 to\firstname%
       \read7 to\business%
       \read7 to\division%
       \read7 to\street%
       \read7 to\location%
       \read7 to\tel%
       \read7 to\fax%
       \read7 to\email%
       \read7 to\cosename%
       \ifthenelse{\equal{\cosename}{}}%
         {\def\cosename{\firstname}}%
         {}%
       \read7 to\birthdate%
       \read7 to\paraer%
       \read7 to\parazw%
       \read7 to\sex%
       \read7 to\contact%
       \endlinechar=`\^^M%
       \ifthenelse{\equal{\searchoption}{\csname\option\endcsname}}%
        {\setboolean{noteofile}{false}%
         \typeout{\adressfound}%
         \ifthenelse{\equal{\business}{}}%
          {}%
          {\typeout{\business}}%
         \ifthenelse{\equal{\division}{}}%
          {}%
          {\typeout{\divisionstr\space\division}}%
         \typeout{\firstname\space\surname}%
         \typeout{\street}%
         \typeout{\location}%
         \typeout{}%
         \ifthenelse{\boolean{mletterboo}}%
          {}%
          {\endlinechar=-1%
           \typein[\choose]{\rightadress}%
           \endlinechar=`\^^M%
           \setboolean{noteofile}{true}%
           \ifthenelse{\equal{\yes}{\choose}}%
             {\setboolean{noteofile}{false}}{}%
           \ifthenelse{\equal{\yesshort}{\choose}}%
             {\setboolean{noteofile}{false}}{}%
           \ifthenelse{\equal{\YES}{\choose}}%
             {\setboolean{noteofile}{false}}{}%
           \ifthenelse{\equal{\YESshort}{\choose}}%
             {\setboolean{noteofile}{false}}{}%
           \ifthenelse{\equal{\Yes}{\choose}}%
             {\setboolean{noteofile}{false}}{}%
          }%
         \ifthenelse{\boolean{noteofile}}% 
          {}%
          {\def\option{found}%
           \def\address{%
            \ifx\business\empty%
              \ifx\sex\female%
                \mrs%
               \else%
                \mr%
              \fi%
              \\%
              \firstname\space\surname\\%
             \else
              \business\\%
              \ifx\division\empty%
               \else%
               \divisionstr\space\division\\%
              \fi%
              \ifx\sex\neutrale%
                \underline{\space}\\%
               \else
                \co\space%
               \ifx\sex\female%
                  \mrs%
                 \else
                  \mr%
                \fi
                \space\surname\\%
              \fi
            \fi
            \street\\%
            \location}%
           \def\openingtext{\csname\openingbon\endcsname,}%
           \ifx\contact\contactgood%
             \ifx\sex\male%
               \def\openingtext{\csname\openinggm\endcsname\space\cosename,}
             \fi
             \ifx\sex\female%
               \def\openingtext{\csname\openinggf\endcsname\space\cosename,}%
             \fi%
             \ifx\sex\neutrale%
               \def\openingtext{\csname\openinggn\endcsname\space\cosename,}%
             \fi%
           \fi%
           \ifx\contact\contactbad%
             \ifx\sex\male%
               \def\openingtext{\csname\openingbm\endcsname\space\surname,}%
             \fi
             \ifx\sex\female%
               \def\openingtext{\csname\openingbf\endcsname\space\surname,}%
             \fi
             \ifx\sex\neutrale%
               \def\openingtext{\csname\openingbon\endcsname,}%
             \fi
           \fi
           \def\closingtext{\csname\closingb\endcsname}%
           \ifx\contact\contactgood%
             \def\closingtext{\csname\closingg\endcsname}%
           \fi
           \ifx\contact\contactbad%
            \def\closingtext{\csname\closingb\endcsname}%
           \fi
          }}{}%
     \fi%
     \ifeof7%
       \setboolean{noteofile}{false}%
     \fi}%
   \let\\=\crlf%
   \ifthenelse{\boolean{mletterboo}}%
     {\ifeof7% 
        \closein7%
        \setboolean{mletterboo}{false}%
        \typeout{Package \PackageName\space Info: \mesb}%
        \typeout{}%
        \PackageWarningNoLine{\PackageName}{\warninga}%
        \PackageInfo{\PackageName}{\warnhelpa}%
        \ifthenelse{\boolean{listadress}}%
           {\address}{}%
      \fi}%
     {\closein7%
      \typeout{Package \PackageName\space Info: \mesb}%
      \typeout{}%
      \ifthenelse{\equal{\option}{found}}%
       {\ifthenelse{\boolean{listadress}}%
         {\address}{}}%
       {\PackageWarningNoLine{\PackageName}{\warninga}%
        \PackageInfo{\PackageName}{\warnhelpa}}%
        \ifthenelse{\boolean{listadress}}%
         {#2}{}}%
}%

\def\print@mletter{\catcode`\\=0%
                   \catcode`\ =10%
                   \catcode`\%=14%
                   \catcode`\^^@=9%
                   \catcode`\^^?=15%
                   \catcode`\^^M=5%
                   \catcode`\{=1%
                   \catcode`\}=2%
                   \catcode`\/=12%
                   \catcode`\|=12%
                   \catcode`\<=12%
                   \catcode`\>=12%
                   \IfFileExists{\adressfilename}%
                     {\openin7=\adressfilename%
                      \setboolean{mletterboo}{true}%
                      \setboolean{noteofile}{true}%
                      \setboolean{listadress}{false}%
                      \def\address{empty}%
                      \typeout{Package \PackageName\space Info: \mesa}}%
                     {\PackageError{\PackageName}{\errormesb}{\errorhelpb}}%
                   \whiledo{\boolean{mletterboo}}%
                    {\searchadress[\fpar]{\spar}%
                     \ifthenelse{\boolean{mletterboo}}%
                       {\input{\jobname.MLT}}%
                       {}%
                    }%
                  }%
\begingroup%
\catcode`\@=11%
\gdef\multiletter{\@ifnextchar [{\multi@letter}{\multi@letter[empty]}}%
\gdef\multi@letter[#1]#2{%
   \gdef\fpar{#1}%
   \gdef\spar{#2}%
   \@mletter}%
\catcode`\^^M\active%
\catcode`\^^L\active\let^^L\relax%
\catcode`\^^I\active%
\gdef\@mletter{%
  \chardef\mletter@file15 %
  \ch@ck7\mletter@file\write%
  \typeout{}%
  \typeout{Writing File `\jobname.MLT'}%
  \immediate\openout\mletter@file=\jobname.MLT\relax%
  \if@tempswa%
    \immediate\write\mletter@file{%
      \@percentchar\@percentchar\space LaTeX2e file `\jobname.MLT'^^J%
      \@percentchar\@percentchar\space  generated by the %
         `Multiletter' environment^^J%
      \@percentchar\@percentchar\space from source `\jobname' on %
         \number\year/\number\month/\number\day.^^J%
      \@percentchar\@percentchar}\relax%
  \fi%
  \let\do\@makeother\dospecials%
  \@vrend%
  \catcode`\^^L\active%
  \let\L\@undefined%
  \def^^L{\@ifundefined L^^J^^J^^J}%
  \catcode`\^^I\active%
  \let\I\@undefined%
  \def^^I{\@ifundefined I\space\space}%
  \catcode`\^^M\active%
  \def^^M##1^^M%
    {\def\test@b{##1}%
     \catcode`\ =9%
     \ifx\test@b\test@a%
       \catcode`\ =10%
       \expandafter\end\expandafter{\@currenvir}%
     \else%
       \catcode`\ =10%
       \immediate\write\mletter@file{##1}\relax\expandafter^^M%
     \fi}}%
\endgroup%
\begingroup
\catcode`\@=11
\catcode`/=0
\catcode`<=\catcode`{
\catcode`>=\catcode`}
\catcode`|=\catcode`\%
/catcode`/\=12
/catcode`/{=12
/catcode`/}=12
/catcode`/%=12
/gdef/@percentchar<%>
/gdef/@vrend</edef/test@a<\end{/@currenvir}>>
/gdef/endmultiletter<|
  /immediate/closeout/mletter@file/relax
  /ifx/L/@undefined/else
    /typeout<Form Feed has been converted to Blank Line>|
  /fi
  /ifx/I/@undefined/else
    /typeout<Tab has been converted to Space>|
  /fi
  /immediate/write/@unused<>/relax|
  /print@mletter>
/endgroup

\endinput
