%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright 2013 koeart, john, ben                                         %
%                                                                          %
% This program is free software: you can redistribute it and/or modify it  %
% under the terms of the GNU General Public License as published by the    %
% Free Software Foundation, either version 3 of the License, or (at your   %
% option) any later version.                                               %
%                                                                          %
% This program is distributed in the hope that it will be useful, but      %
% WITHOUT ANY WARRANTY; without even the implied warranty of               %
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        %
% General Public License for more details.                                 %
%                                                                          %
% You should have received a copy of the GNU General Public License along  %
% with this program.  If not, see <http://www.gnu.org/licenses/>.          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ProvidesClass{datenschleuder}[%
	2013/07/19 v0.1 Datenschleuder
]
\LoadClass[%
	pdftex,
	a5paper,
	twoside,
%	twocolumn,
	DIV=15,
]{scrartcl}

\RequirePackage[paper=a5paper,inner=20mm, outer=12mm,top=20mm,bottom=30mm]{geometry}
\RequirePackage{multicol}
\RequirePackage{xkeyval}
\newcommand{\@volume}{}
\DeclareOptionX{volume}{\renewcommand{\@volume}{#1}}%
\newcommand{\@year}{}
\DeclareOptionX{year}{\renewcommand{\@year}{#1}}%
\ProcessOptionsX

\RequirePackage{ifluatex}
\ifluatex
	\RequirePackage{luatextra}
	\RequirePackage{lineno}
	\RequirePackage[EU2]{fontenc}
	\RequirePackage{fontspec}
	\setmainfont[Mapping=tex-text,Numbers={OldStyle}]{Linux Libertine O}
	\setsansfont[Mapping=tex-text,Numbers={OldStyle}]{Linux Biolinum O}
%	\setmonofont[Mapping=tex-text]{Courier Prime}
\fi

\RequirePackage{fmtcount}

\RequirePackage{scrpage2}
\RequirePackage{graphicx}
\RequirePackage{tikz}
\RequirePackage[pdftex,%
	citebordercolor={0.9 0.9 1},%
	filebordercolor={0.9 0.9 1},%
	linkbordercolor={0.9 0.9 1},%
	pagebordercolor={0.9 0.9 1},%
	urlbordercolor={0.9 0.9 1},%
	pdfborder={0.9 0.9 1},%
	pagebackref,plainpages=false,pdfpagelabels=true]{hyperref}
\RequirePackage{float}

\pgfdeclareimage[height=5mm]{Pesthoernchen}{pesthoernchen.pdf}
\pgfdeclareimage[width=10mm]{CCCLogo}{Logo_CCC}

\newfloat{impressum}{tpbh}{loi} %impressum als Float, damit kann Text fließen

\pagestyle{scrheadings}

\clearscrheadfoot
\setkomafont{pageheadfoot}{%
\normalfont\normalcolor\small
}
%FIXME: use pgfdeclared image for saving objects in pdf

\rehead[]{\tikz[overlay]{\node[xshift=0.3mm,yshift=0.9mm]{\pgfuseimage{CCCLogo}};}}
\rohead[]{\tikz[overlay]{\node[xshift=0.3mm,yshift=0.9mm]{\pgfuseimage{CCCLogo}};}}
\chead{\directlua{tex.print(ruhetmp or "Bitte runninghead setzen")}}
\setheadsepline{0.5pt}

\ifoot[]{Datenschleuder. \@volume / \@year}
\ofoot[]{\texttt{0x\ifthenelse{\value{page}>15}{}{0}\hexadecimal{page}}}
\setfootsepline{0.5pt}

%FIXME: use abstract environment in prior of nasty DSabstract command
\renewenvironment{abstract}{%
	\begin{center}
		\bfseries
}{%
	\end{center}
}
\newcommand{\@DSabstract}{}
\newcommand{\DSabstract}[1]{\renewcommand{\@DSabstract}{#1}}

\newtoks{\@DSauthorstok}
\@DSauthorstok={Test, }%
\@DSauthorstok=\expandafter{\the\@DSauthorstok Hallo, }
\newcommand{\@addauthor}[1]{\@DSauthorstok=\expandafter{\the\@DSauthorstok #1, }}
\newcommand{\addauthor}[1]{\@addauthor{#1}}
\addauthor{Eris Discordia}
\addauthor{Ein anderer Test}

\renewcommand{\maketitle}{%
%	\twocolumn[
	%	\renewcommand{\@runninghead}{\@runningheadtmp}
		\textsf{\Huge\@title}

		\begin{center}%
			\textsf{von \@author}%FIXME: implement mail and array of authors
		\end{center}
        \addauthor{Noch ein Test}

		{\bfseries\@DSabstract}
		\vspace*{3mm}
%	]
		\directlua{ruhetmp=nxruhetmp nxruhetmp=nil}
	\normalsize
}

\define@key{DSarticleKeys}{title}{\title{#1}}%
\define@key{DSarticleKeys}{author}{\author{#1}}
\define@key{DSarticleKeys}{DSabstract}{\renewcommand{\@DSabstract}{#1}}
%\newcommand{\@runninghead}{}
%\newcommand{\@runningheadtmp}{}
%\newcommand{\runninghead}[1]{\renewcommand{\@runningheadtmp}{#1}}
\newcommand{\runninghead}[1]{\directlua{nxruhetmp = "\luatexluaescapestring{#1}"}}
\newcommand{\manrunninghead}[1]{\directlua{ruhetmp = "\luatexluaescapestring{#1}"}}
\define@key{DSarticleKeys}{runninghead}[\@title]{\runninghead{#1}}

\newenvironment{DSarticle}[1][]{%
    \newpage
    \setcounter{section}{0}
	\setkeys{DSarticleKeys}{#1}%
    \newpage
   	\maketitle
    \begin{multicols}{2}
    }{%
    \end{multicols}
}
\newenvironment{DSfigure}
  {\par\medskip\noindent\minipage{\linewidth}
  }{%
  \endminipage\par\medskip}


\newcounter{NumOfLetters}

\newenvironment{DSletters}{%
\manrunninghead{Leserbriefe}
\setcounter{NumOfLetters}{0}
\begin{multicols}{2}
}
{%
\end{multicols}
}

\newenvironment{letter}{%
	\setlength{\parskip}{2.5mm}%
	\setlength{\parindent}{0mm}%
	\ifthenelse{\equal{\arabic{NumOfLetters}}{0}}{}{%
		\vspace{-3mm}%
		\begin{center}%
			\hfill%
			\hfill%
%FIXME: use pgfdeclared image for saving objects in pdf
			\pgfuseimage{Pesthoernchen}
			\hfill%
			\pgfuseimage{Pesthoernchen}
			\hfill%
			\pgfuseimage{Pesthoernchen}
			\hfill%
			\hfill\hspace*{0cm}%
		\end{center}%
		\vspace{-5mm}%
	}%
	\stepcounter{NumOfLetters}%
}{%
}
\newenvironment{question}{%
}{%
\par 
}
\newenvironment{answer}{%
		\itshape%
}{%
}

%%%%%%%%
% Die folgenden Kommandos können auch aus dem Dokument gesetzt werden. Evtl. als DSimpressum Keys machen
%%%%%%%%
\newcommand{\@DSherausgeber}{}
\newcommand{\DSherausgeber}[1]{\renewcommand{\@DSherausgeber}{#1}}
\DSherausgeber{%
    \textbf{Herausgeber}\\
    (Abos, Adressen, Verwaltungstechnisches etc.)\\
    CCC e.V. Postfach 60 04 80, 22204 Hamburg,
    Tel.: +49 40 401801-0, Fax: +49 40 401801-41,
    \textit{<office@ccc.de>}
}

\newcommand{\@DSredaktion}{}
\newcommand{\DSredaktion}[1]{\renewcommand{\@DSredaktion}{#1}}
\DSredaktion{%
    \textbf{Redaktion}\\
    (Artikel, Leserbriefe, Inhaltliches, Geldspenden)\\
    Redaktion Datenschleuder,
    Postfach 64 02 36, 10048 Berlin,
    Tel.: +49 40 401801-44, Fax: +49 40 401801-54,
    \textit{<ds@ccc.de}
}

\newcommand{\@DSdruck}{}
\newcommand{\DSdruck}[1]{\renewcommand{\@DSdruck}{#1}}

\DSdruck{%
    \textbf{Druck}\\
    Discordia Druck Dresden,\\
    \url{http://pinguindruck.de}
}

\newcommand{\@DSvisdp}{}
\newcommand{\DSvisdp}[1]{\renewcommand{\@DSvisdp}{#1}}
\DSvisdp{%
    \textbf{V.i.S.d.P }\\
    Eris Discordia
}

\newcommand{\@DSauthors}{\textbf{Autoren }\\\the\@DSauthorstok}
\newcommand{\DSauthors}{\@DSauthors}

%\define@key{DSimpressumKeys}{ViSdP}{%
%    \DSvisdp{#1}
%}
\newcommand{\DSimpressum}{%
    \begin{impressum*}[b!]
            %minipage: pos., höhe, textpos, breite
        \colorbox{lightgray}{
        \begin{minipage}[t]{1\linewidth}
                    \large \textbf{Die Datenschleuder Nr. \@volume}
                    \begin{multicols}{2}
                   \normalsize
                   \@DSherausgeber\\
 \@DSredaktion\\
 \@DSdruck\\
 \@DSvisdp\\
                    \DSauthors
                \end{multicols}
            \end{minipage}
        }
        \end{impressum*}
}
