\NeedsTeXFormat{LaTeX2e}
%\ProvidesPackage{anrechnung}[1.2 Revision tu wien anrechungen]
\usepackage{ifthen}
\usepackage{graphics}
\RequirePackage{graphicx}
\DeclareGraphicsExtensions{.pdf,.ps,.eps}
\usepackage{eso-pic}
%\usepackage{calc}
\usepackage{fp-basic}
\usepackage{fp-eval}
% does some background magic, provides *ShipoutPicture
\RequirePackage{eso-pic}

% only needed for (optional) braces in columns 6/7
\usepackage{multirow}
\usepackage{bigdelim}

\pagestyle{empty}

\parindent0pt
\parskip0pt

\setlength{\voffset}{0in}
\setlength{\unitlength}{1mm}
\setlength{\topmargin}{-20mm}
\setlength{\oddsidemargin}{-21mm}
\setlength{\evensidemargin}{0mm}
\setlength{\textheight}{295mm}
\setlength{\textwidth}{210mm}
\setlength{\footskip}{0mm}
\setlength{\headsep}{0mm}
\setlength{\topskip}{0mm}

% for braces ...
\newcount\lfpm
\newcount\rfpm

%
% at load time: check whether the .eps are present
%
\IfFileExists{front.eps} {
} {
	\typeout{}
	\typeout{Achtung!}
	\typeout{}
	\typeout{you should download the pdf and make front/back.eps}
	\errmessage{dieses .tex braucht front.eps/back.eps}
}

% nicked from wallpaper.sty CenterWallPaper
\newcommand{\background}[1]{%
\AddToShipoutPicture{\put(0,0){%
     \parbox[b][1\paperheight]{1\paperwidth}{%       
       \vfill
       \centering
       \includegraphics[width=\paperwidth,height=\paperheight,%
                        keepaspectratio]{#1}%
       \vfill
     }}}
}  
\newcommand{\clearbackground}{\ClearShipoutPicture}

% puts a box on the front side (unrotated)
\newcommand{\frontbox}[5]{%
\put(#1,#2){\makebox(#3,#4){
%\fbox
{\parbox[t][#4mm]{#3mm}{%
#5\\
}}}}
}

% major difference: rotated by 90 degrees. same measurements from
% portrait view

\newcommand{\backbox}[5]{%
\put(#1,#2){\rotatebox{90}{%
\makebox(#3,#4){
%\fbox
{\parbox[b][#4mm][c]{#3mm}{%
#5%
}}}}}
}

% anrechnungstabelle (einzeilig, platziert)
\newcommand{\backline}[5]{%
\backbox{#1}{6}{279}{5}{%
\begin{tabular}[t]{%
p{47mm}@{\hspace{5mm}} % alt
p{7mm}@{\hspace{1mm}} % v
p{7mm}@{\hspace{1mm}} % ue
p{7mm}@{\hspace{1mm}} % datum
p{26mm}@{\hspace{1mm}} % ws
p{1mm}% @{\hspace{9mm}} % note
p{2mm}@{\hspace{2mm}}% (right) braces to summarize old courses + space
p{1mm}% (left) braces to summarize new courses
p{49mm}@{\hspace{3mm}} % lv-titel neu
p{25mm}@{\hspace{1mm}} % lv-nr
p{15mm}@{\hspace{1mm}} % stunden
p{8mm}@{\hspace{10mm}} % ???
p{8mm}@{\hspace{1mm}} % note neu
p{42mm}} % (unterschrift)
#2 &
\ifthenelse{\equal{#3}{}}{}{\lfpm=#3\multiply\lfpm2\ldelim\}{\lfpm}*[]} &%
\ifthenelse{\equal{#4}{}}{}{\rfpm=#4\multiply\rfpm2\rdelim\{{\rfpm}*[]} &%
#5
\end{tabular}
}%
}


\newcounter{anrnr}

%
%  formats front page (need not be called directly)
%
\newcommand{\frontpage}[0]{
\clearbackground
\setlength{\topmargin}{-20mm}
\setlength{\oddsidemargin}{-21mm}
\background{front}
\begin{picture}(210,295)
% relevant info:

% bogen nr
\frontbox{175}{263}{6}{6}{\Large {\bf \theanrnr}}
% matrikelnr
\frontbox{160}{247}{20}{5}{\Large {\bf \anrmnr}}
% Familienname, Vorname
\frontbox{15}{196}{123}{5}{\Large {\bf \anrname}}
% Geburtsdatum (tt.mm.jjjj)
 \frontbox{152}{195}{26}{5}{\Large {\bf \anrgeb}}
% Adresse
\frontbox{15}{179}{165}{5}{\Large {\bf \anradr}}
% fuer die Studienrichtung ...
\frontbox{15}{154}{105}{5}{\bf \anrfachneu (\anrkennz\ifthenelse{\equal\anrkennza{}}{}{, \anrkennza})}
% an der Universitaet/Hochschule/whatever ...
\frontbox{15}{137}{122}{5}{\bf \anruni}
% ... fuer die Studienrichtung ...
\frontbox{15}{120}{105}{5}{\bf \anrfachalt}
% in der Zeit vom Studienjahr ...
\frontbox{15}{102}{20}{5}{\bf \anrvon}
% bis zum Studienjahr ...
\frontbox{75}{102}{20}{5}{\bf \anrbis}
% abgelegt.

% Datum (wenn gewuenscht) tt.mm.jjjj
\frontbox{32}{52}{20}{5}{\bf \anrdatum}


\end{picture}
% second page (anrechungszeilen)

\clearpage
}

%
%  formats reverse page (need not be called directly)
%
\newcounter{@anrybase}
\setcounter{@anrybase}{52}
\newcounter{@anryoffset}

\newcommand{\backpage}[0]{
\setlength{\topmargin}{-14mm}
\setlength{\oddsidemargin}{-24mm}
\clearbackground
\begin{picture}(210,280)
\background{back}

% Anerkennung von pruefungen fuer ...
% matrikelnr
\backbox{9}{94}{20}{5}{\Large {\bf \anrmnr}}
% Familienname, Vorname
\backbox{9}{128}{115}{5}{\Large {\bf \anrname}}
% (abgelegt) an der 
\backbox{26}{25}{90}{5}{{\bf \anruni}}
% an der TU Wien gleichwertig mit ...
\backbox{26}{214}{73}{5}{{\bf \anrfachneu}}
% Kennzahl1
\backbox{9}{256}{14}{5}{\Large {\bf \anrkennz}}
% Kennzahl2
\backbox{9}{272}{14}{5}{\Large {\bf \anrkennza}}

%\backline{52}{altes fach & X & x & 13 & 13.11.1991 & 3 & neues fach & 123.456 & 5 & & b3 & unterschrift \\}
%	\setcounter{inpage@anr}{1}
	\forLoop[1]{1}{\themodulo@anr}{inpage@anr}{
		\FPeval{@anryofffp}{( ( 8.45 * (\theinpage@anr - 1) )+ 51.5 ) }
		%\setcounter{@anryoffset}{\real{8.5}}
		\typeout{processing line no \theinpage@anr{}: 
			\@anryofffp
%			\@nameuse{@anr-\theanrnr-\theinpage@anr}
		}
		\backline{\@anryofffp}{\@nameuse{@anr-\theanrnr-\theinpage@anr-left}}
		                      {\@nameuse{@anr-\theanrnr-\theinpage@anr-bl}}
		                      {\@nameuse{@anr-\theanrnr-\theinpage@anr-br}}
		                      {\@nameuse{@anr-\theanrnr-\theinpage@anr-right}}
	}

\end{picture}
\clearpage
}

\newcounter{modulo@anr}
\newcounter{page@anr}
\def\lines@anr{17} % lines-1
\setcounter{anrnr}{1}
\setcounter{modulo@anr}{0}
\newcounter{inpage@anr}

% nicked from poetry.sty ...
\newcommand{\@nameedef}[1]{
  \expandafter\protected@edef\csname #1\endcsname}

\newcommand{\forLoop}[5][1] {% 
	\setcounter{#4}{#2} \ifthenelse{
		\value{#4}<#3 }% 
	{% 
#5 \addtocounter{#4}{#1}
		\forLoop[#1]{\value{#4}}{#3}{#4}{#5} }% 
	{% 
#5 
	}% 
}

\newcommand{\anrechnenflush}{

	\frontpage
	\backpage
	\stepcounter{anrnr}
	\setcounter{modulo@anr}{0}
	\typeout{new page \theanrnr}
}


\newcommand{\anrechnen}[4]{
		\stepcounter{modulo@anr}
	\typeout{line number \themodulo@anr{} of \lines@anr}
%		\expandafter\protected@edef\csname \@anr-\anrnr-\modulo@anr\endcsname{#1}
%		\@nameedef{\@anr{}-\anrnr{}-\modulo@anr}{#1}
		\@nameedef{@anr-\theanrnr-\themodulo@anr-left}{#1}
		\@nameedef{@anr-\theanrnr-\themodulo@anr-bl}{#2}
		\@nameedef{@anr-\theanrnr-\themodulo@anr-br}{#3}
		\@nameedef{@anr-\theanrnr-\themodulo@anr-right}{#4}
	\ifthenelse{\themodulo@anr=\lines@anr}{%
		\anrechnenflush
	}{}% else/fi
}


