%% -*-TeX-*-
 % ###################################################################
 % 
 %  FILE: "addressbook.cls"
 %                                    created: 2005-06-28 16:43:12 
 %                                last update: 2007-01-12 16:05:54 
 %  Author: Fr�d�ric Boulanger
 %  E-mail: Frederic.Boulanger@supelec.fr
 %    mail: Sup�lec - D�partement Informatique
 %          3 rue Joliot-Curie, 91192 Gif-sur-Yvette, France
 %     www: http://wwwsi.supelec.fr/fb/
 %  
 %  Description: 
 %  
 %  A LaTeX document class to typeset address books.
 %  The address book should be obtained from an Apple address book 
 %  using the addressbook.tcl Tcl script. This script uses the 
 %  Tcladdressbook extension by Bernard Desgraupes:
 %    <http://webperso.easyconnect.fr/bdesgraupes/tcl.html#tclab>
 % 
 %  History
 % 
 %  modified   by  rev reason
 %  ---------- --- --- -----------
 %  1940-01-26 FBO 1.0 original
 % ###################################################################
 %%
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{addressbook}[2005/03/11 v1.0 ^^J
 A class to typeset diaries]
% \DeclareOption{francais}{\newcommand{\tongue}{francais}}
% \ProcessOptions\relax
\LoadClass[a4paper,twoside,12pt]{article}
% \ifx\tongue\undefined
% \else
% \RequirePackage[\tongue]{babel}
% \fi

\RequirePackage[
  a4paper,
  includehead,
  includefoot,
  nofoot,
  top=1cm,
  headheight=\baselineskip,
  left=1.5cm,right=1.5cm,
]{geometry}

\setlength{\parindent}{0pt}

\newcommand{\settototalheight}[2]{%
  \setbox\@tempboxa\hbox{{#2}}%
  #1\ht\@tempboxa\advance#1\dp\@tempboxa
  \setbox\@tempboxa\box\voidb@x
}

% New page for each new letter, plus mark for the page header
\newcommand{\newLetter}[1]{%
  \newpage
  \mark{#1}%
}

% Get the information about one person and print it
\newcommand{\person}[1]{%
  \let\adb@name\relax
  \let\adb@first\relax
  \let\adb@middle\relax
  \let\adb@title\relax
  \let\adb@org\relax
  \let\adb@dpt\relax
  \let\adb@phone\relax
  \let\adb@urls\relax
  \let\adb@address\relax
  \let\adb@note\relax
  \let\adb@email\relax
  \let\adb@homepage\relax
  #1\relax\printperson
}

\newlength{\adb@tmplen}
\newlength{\adb@boxwidth}
\newlength{\adb@boxwidthi}
\newlength{\adb@phoneheight}
\newlength{\adb@addressheight}
\newsavebox{\adb@tmpbox}
\newsavebox{\adb@phonebox}
\newsavebox{\adb@addressbox}

\newcommand{\adb@namepart}{0.4}
\newcommand{\adb@titlepart}{0.3}
\newcommand{\adb@phonepart}{0.3}
\newcommand{\adb@emailext}{2}
\newcommand{\adb@urlext}{2}
\newcommand{\adb@addresspart}{0.7}

% Print an address book entry
\newcommand{\printperson}{%
  % Name
  \ifx\adb@name\relax
    \ifx\adb@org\relax
      \ifx\adb@first\relax
      \else
        \let\adb@name\adb@first
        \let\adb@first\relax
      \fi
    \else
      \let\adb@name\adb@org
      \let\adb@org\relax
    \fi
  \fi
  \settowidth{\adb@boxwidth}{\bfseries\adb@name\ \adb@first}%
  \let\adb@end\relax
  \ifdim\adb@boxwidth>\adb@namepart\linewidth
    \setlength{\adb@boxwidth}{\adb@namepart\linewidth}%
    \def\adb@end{\par\vspace{0.5\baselineskip}}%
  \fi
  \parbox[t]{\adb@boxwidth}{%
    \raggedright\bfseries\adb@name\ \adb@first\adb@end
  }\hfill% \dotfill
  %
  % Title/organization
  \settowidth{\adb@boxwidth}{\bfseries\itshape\adb@title}%
  \settowidth{\adb@boxwidthi}{\bfseries\adb@org}%
  \ifdim\adb@boxwidthi>\adb@boxwidth
    \setlength{\adb@boxwidth}{\adb@boxwidthi}%
  \fi
  \settowidth{\adb@boxwidthi}{\bfseries\adb@dpt}%
  \ifdim\adb@boxwidthi>\adb@boxwidth
    \setlength{\adb@boxwidth}{\adb@boxwidthi}%
  \fi
  \ifdim\adb@boxwidth>\adb@titlepart\linewidth
    \setlength{\adb@boxwidth}{\adb@titlepart\linewidth}%
  \fi
  \ifdim\adb@boxwidth>0pt
    \addtolength{\adb@boxwidth}{1em}%
    \ \parbox[t]{\adb@boxwidth}{%
    \centering\bfseries
      \ifx\adb@title\relax
        \adb@org
        \ifx\adb@dpt\relax\else
          \\\relax{\small\adb@dpt}%
        \fi
      \else
        \textit{\adb@title}%
        \ifx\adb@org\relax
        \else
          \par\adb@org
          \ifx\adb@dpt\relax\else
            \\\relax{\small\adb@dpt}%
          \fi
        \fi
      \fi
    }\hfill%\,\dotfill
  \fi
  % Telephone/e-mail/web site
  \savebox{\adb@phonebox}{\parbox[t]{\adb@phonepart\linewidth}{%
    \ifx\adb@phone\relax\else
      \savebox{\adb@tmpbox}{%
        \let\named\named@phone
        \noindent\parbox[t]{\adb@emailext\linewidth}{%
          \raggedleft
          \begin{tabular}{@{}l@{~}c@{ }r@{}}
            \adb@phone
          \end{tabular}%
        }%
      }%
      \settototalheight{\adb@tmplen}{\usebox{\adb@tmpbox}}%
      \addtolength{\adb@tmplen}{-\baselineskip}%
      \makebox[\linewidth][r]{\raisebox{-0.5\adb@tmplen}{\usebox{\adb@tmpbox}}}%
    \fi
    %
    \ifx\adb@email\relax\else
      \savebox{\adb@tmpbox}{%
        \let\named\named@email
        \noindent\parbox[t]{\adb@emailext\linewidth}{%
          \raggedleft
          \begin{tabular}{@{}l@{~}c@{ }r@{}}
            \adb@email
          \end{tabular}%
        }%
      }%
      \settototalheight{\adb@tmplen}{\usebox{\adb@tmpbox}}%
      \addtolength{\adb@tmplen}{-\baselineskip}%
      \par\noindent\makebox[\linewidth][r]{%
        \raisebox{-0.5\adb@tmplen}{\usebox{\adb@tmpbox}}%
      }%
    \fi
    \ifx\adb@urls\relax\else
      \savebox{\adb@tmpbox}{%
        \let\named\named@url
        \noindent\parbox[t]{\adb@emailext\linewidth}{%
          \raggedleft
          \begin{tabular}{@{}l@{~}c@{ }r@{}}
            \adb@urls
          \end{tabular}%
        }%
      }%
      \settototalheight{\adb@tmplen}{\usebox{\adb@tmpbox}}%
      \addtolength{\adb@tmplen}{-\baselineskip}%
      \par\noindent\makebox[\linewidth][r]{%
        \raisebox{-0.5\adb@tmplen}{\usebox{\adb@tmpbox}}%
      }%
    \fi
  }}%
  \settototalheight{\adb@phoneheight}{\usebox{\adb@phonebox}}%
  \addtolength{\adb@phoneheight}{-\baselineskip}%
  \,\raisebox{0pt}[0pt][0pt]{\usebox{\adb@phonebox}}
  \relax\let\named\undefined
  \par\nopagebreak
  % Address
  \ifx\adb@address\relax
    \vspace{\adb@phoneheight}%
  \else
    \let\named\named@address
    %
    \let\adb@addstreet\relax
    \let\adb@addzip\relax
    \let\adb@addcity\relax
    \let\adb@addstate\relax
    \let\adb@addcountry\relax
    \let\adb@addcountrycode\relax
    \adb@address
    \savebox{\adb@addressbox}{\parbox[t]{\adb@addresspart\linewidth}{%
      \ifx\adb@addstreet\relax\else
        \adb@addstreet\par
      \fi
      \ifx\adb@addzip\relax\else
        \adb@addzip\ 
      \fi
      \adb@addcity
      \ifx\adb@addcountry\relax\else
        \par\adb@addcountry
      \fi
      \ifx\adb@note\relax\else
        \par\vspace*{0.2\baselineskip}\textit{\adb@note}%
      \fi
    }}%
    \settototalheight{\adb@addressheight}{\usebox{\adb@addressbox}}%
    \ifdim\adb@phoneheight>\adb@addressheight
      \setlength{\adb@addressheight}{\adb@phoneheight}%
    \fi
    \parbox[t][\adb@addressheight][t]{\adb@addresspart\linewidth}{%
      \usebox{\adb@addressbox}%
    }%
    \par\nopagebreak\vspace*{0.5\baselineskip}%
  \fi
  \relax\let\named\undefined
  \par\nopagebreak
%   \vspace*{0.5\baselineskip}%
  \hrulefill\par
}

%% Commands that get the information in an entry
\newcommand{\Address}[1]{%
 \def\adb@address{#1}%
}

\newcommand{\Birthday}[1]{%
  #1
}

\newcommand{\Email}[1]{%
  \def\adb@email{#1}%
}

\newcommand{\First}[1]{%
  \def\adb@first{#1}%
}

\newcommand{\HomePage}[1]{%
  \def\adb@homepage{#1}%
}

\newcommand{\JobTitle}[1]{%
  \def\adb@title{#1}%
}

\newcommand{\Last}[1]{%
  \def\adb@name{#1}%
}

\newcommand{\MaidenName}[1]{%
  #1
}

\newcommand{\Middle}[1]{%
  \def\adb@middle{#1}%
}

\newcommand{\NickName}[1]{%
  #1
}

\newcommand{\Note}[1]{%
  \def\adb@note{#1}%
}

\newcommand{\Organization}[1]{%
  \def\adb@org{#1}%
}

\newcommand{\ABDepartment}[1]{%
  \def\adb@dpt{#1}%
}

\newcommand{\Phone}[1]{%
  \def\adb@phone{#1}%
}

\newcommand{\URLs}[1]{%
  \def\adb@urls{#1}%
}
  
\newcommand{\Title}[1]{%
  \def\adb@title{#1}%
}

%%
\newcommand{\City}[1]{%
  \def\adb@addcity{#1}%
}

\newcommand{\Country}[1]{%
  \def\adb@addcountry{#1}%
}

\newcommand{\CountryCode}[1]{%
  \def\adb@addcountrycode{#1}%
}

\newcommand{\State}[1]{%
  \def\adb@addstate{#1}%
}

\newcommand{\Street}[1]{%
  \def\adb@addstreet{#1}%
}

\newcommand{\ZIP}[1]{%
  \def\adb@addzip{#1}%
}
%%
\newcommand{\ingroup}[1]{%
}
%%
%% Parse the label and value of a tagged field.
%% 
%% Label names can be changed in addressbook.cfg using
%% the commands \newphonelabel, \newemaillabel and \newurllabel
\newcommand{\named@}[3]{%
  \expandafter\adb@strip@cust@prefix #3:\end
  \expandafter\ifx\csname adb@#1@label@#2\endcsname\relax
    \def\adb@cur@line{#2 & : & \adb@custvalue\\}%
  \else
    \def\adb@tmp@cmd{}%
    \expandafter\ifx\csname adb@#1@label@#2\endcsname\adb@tmp@cmd
      \if|\adb@custpref|
        \def\adb@cur@line{ & & \adb@custvalue\\}%
      \else
        \def\adb@cur@line{\adb@custpref & : & \adb@custvalue\\}%
      \fi
    \else
      \def\adb@cur@line{\csname adb@#1@label@#2\endcsname & \adb@custvalue\\}%
    \fi
  \fi
  \adb@cur@line\par
}
% Telephone
\newcommand{\named@phone}[2]{\named@{phone}{#1}{#2}}
%%
% URLs
\newcommand{\named@url}[2]{\named@{url}{#1}{#2}}
% E-mail
\newcommand{\named@email}[2]{\named@{email}{#1}{#2}}
% Adresse
\newcommand{\named@address}[2]{%
  \def\adb@address@name{#1}#2%
}%
%%%
%% Define new labels
%% #1 is the type of the label (phone, email or url)
%% #2 is the tag of the label as read from the address book
%% #3 is the label as you want it printed
%% Telephone, e-mail and url entries are typeset in a lcr tabular,
%% the label and the separator are in the first two columns, so #3 
%% should contain one '&' character.
\newcommand{\newadblabel}[3]{%
  \expandafter\def\csname adb@#1@label@#2\endcsname{#3}%
}
% Short hand for \newadblabel{phone}{...}{...}
\newcommand{\newphonelabel}[2]{%
  \newadblabel{phone}{#1}{#2}%
}
% Short hand for \newadblabel{email}{...}{...}
\newcommand{\newemaillabel}[2]{%
  \newadblabel{email}{#1}{#2}%
}
% Short hand for \newadblabel{url}{...}{...}
\newcommand{\newurllabel}[2]{%
  \newadblabel{url}{#1}{#2}%
}
% Look for a custom prefix in a field value: if a value starts with 
% <prefix>:, <prefix> is interpreted as a custom prefix that can be 
% used to override the label of the value.
% The prefix is stored in \adb@custpref, and the value (with the 
% prefix and the ':' removed) is stored in \adb@custvalue.
\def\adb@strip@cust@prefix#1:#2\end{%
  \if|#2|
    \xdef\adb@custpref{}%
    \xdef\adb@custvalue{#1}%
  \else
    \xdef\adb@custpref{#1}%
    \expandafter\adb@strip@column #2%
  \fi
}
\def\adb@strip@column #1:{%
  \xdef\adb@custvalue{#1}%
}%
%% Page header and footer
\def\ps@repertoire{%
  \def\@oddhead{%
    \parbox{\textwidth}{%
      \rule{\textwidth}{0.5mm}\par
      \thepage\hfill\textbf{\firstmark}\par
      \vspace{-0.5\baselineskip}%
      \rule{\textwidth}{0.5mm}\par
    }%
  }%
  \def\@evenhead{%
    \parbox{\textwidth}{%
      \rule{\textwidth}{0.5mm}\par
      \textbf{\firstmark}\hfill\thepage\par
      \vspace{-0.5\baselineskip}%
      \rule{\textwidth}{0.5mm}\par
    }%
  }%
  \let\@oddfoot\@empty
  \let\@evenfoot\@empty
}

% Title of the addressbook.
% Default is to use the french name {R\'epertoire}. Can be changed in 
% addressbook.cfg
\newcommand{\adb@repname}{R\'epertoire}

% First page of the address book is special
\def\adb@firstpage{%
  \pagestyle{empty}
  \vspace*{\fill}
  \makebox[\textwidth][l]{\hfill\textit{\textbf{\Huge\adb@repname}}\hfill}\par
  \vspace{\stretch{3}}
  \makebox[\textwidth][l]{\hspace{\stretch{6}}\textit{\today}\hfill}
  \cleardoublepage
  \pagestyle{repertoire}
}

% Read config file if any
\InputIfFileExists{addressbook.cfg}
\relax

% At the beginning of the document, make ':' not active so that it 
% can be used as a custom prefix separator, and build the first page.
\AtBeginDocument{%
  \expandafter\ifx\csname shorthandoff\endcsname\relax\else
    \shorthandoff{:}%
  \fi
  \adb@firstpage
}
% At the end, write the number of the address book to help
% the pdfbooklet package turn it into a 4 pages by sheet booklet. 
\newwrite\pagefile
\AtEndDocument{%
  \immediate\openout\pagefile\jobname.pages
  \immediate\write\pagefile{\thepage}%
  \immediate\closeout\pagefile
}
\endinput
% The end
