% -*- coding: utf-8; -*-
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
%                                                                                                  |
% (C)opyright A. Hilbig, mail@andrehilbig.de                                                       |
%             http://www.andrehilbig.de/Publications                                               |
%                                                                                                  | 
% diese Datei: ahilbig-semester.sty                                                                |
%                                                                                                  |
% Das Dokument steht unter der Lizenz: Creative Commons by-nc-sa Version 4.0                       |
% http://creativecommons.org/licenses/by-nc-sa/4.0/deed.de                                         |
%                                                                                                  |
% Nach dieser Lizenz darf das Dokument beliebig kopiert und bearbeitet werden,                     |
% sofern das Folgeprodukt wiederum unter gleichen Lizenzbedingungen vertrieben                     |
% und auf die ursprünglichen Urheber verwiesen wird.                                               |
% Eine kommerzielle Nutzung ist ausdrücklich ausgeschlossen.                                       |
%                                                                                                  |
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
%                                                                                                  |
% HINWEISE/ERWEITERUNG                                                                             |
% Diese Pakete basieren auf den entsprechenden Paketen von Prof. Dr. Ludger Humbert                |
% (http://ddi.uni-wuppertal.de/ -- humbert@uni-wuppertal.de) und wurden zum Teil angepasst. Seine  |
% Pakete basieren wiederum auf Entwicklungen von Prof. Dr. Till Tantau                             |
% (http://www.tcs.uni-luebeck.de/de/mitarbeiter/tantau/). Beiden gilt mein Dank hierfür.           |
%                                                                                                  |
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
%                                                                                                  |

\ProvidesPackage{ahilbig-semester}
 
\RequirePackage{pgffor}
\RequirePackage{pgfcalendar}
\RequirePackage{soul,color}  % wg. \hl ... = highlight
\RequirePackage{multicol}

\RequirePackage[
   backend=biber      
  ,sortlocale=de_DE%.UTF-8  
  ,style=authoryear
  ,bibencoding=UTF8  
  ,block=space
  ,autocite=inline % \autocite[..][..]{..} erzeugt Literaturverweise mit runden Klammern 
]{biblatex}


\RequirePackage[scale=1.9]{ccicons}
\newenvironment{topcolumns}{\begin{columns}[t]}{\end{columns}}
\RequirePackage{isodate}
\newcommand*\jahr{\number\year}
\newcounter{jahrp}
\addtocounter{jahrp}{\jahr}
\stepcounter{jahrp}
\newcommand*\sommersemester{\jahr}
\newcommand*\wintersemester{\jahr/\thejahrp}

%
% Semester dates
%

% Sets the dates of the semester.
%
% Usage: \setsummerdates{first date of semester}{first lecture date}%
%                   {last lecture date}{last date of semester}

\def\setsummerdates#1#2#3#4{%
  \edef\lec@summerstart{#1}
  \edef\lec@summerfirstlec{#2}
  \edef\lec@summerlastlec{#3}
  \edef\lec@summerend{#4}
}

\def\setwinterdates#1#2#3#4{%
  \edef\lec@winterstart{#1}
  \edef\lec@winterfirstlec{#2}
  \edef\lec@winterlastlec{#3}
  \edef\lec@winterend{#4}
}

% Getter and setter:

\def\getsummerstartdate{\lec@summerstart}
\def\getsummerfirstlecturedate{\lec@summerfirstlec}
\def\getsummerlastlecturedate{\lec@summerlastlec}
\def\getsummerenddate{\lec@summerend}

\def\getwinterstartdate{\lec@winterstart}
\def\getwinterfirstlecturedate{\lec@winterfirstlec}
\def\getwinterlastlecturedate{\lec@winterlastlec}
\def\getwinterenddate{\lec@winterend}

%
% Lectures
%

% Sets the lecture dates.
%
% Usage:
%
% \lecturelistofdates{
%   2013-01-01,
%   2013-02-28,
%   2013-12-24,
% }

\def\lecturelistofdates#1{
  \foreach \lec@date[count=\lec@i] in {#1} {
    \expandafter\xdef\csname lec@date@\lec@i\endcsname{\lec@date}
  }
}

% Sets the exercise dates.
%
% Usage:
%
% \exerciselistofdates{
%   2013-01-01->2013-01-08,
%   2013-02-28->2013-03-07,
%   2013-12-24->2013-12-31,
% }

\def\exerciselistofdates#1{%
  \def\ex@parsedates##1->##2\foo{%
    \def\ex@tutorial{##1}%
    \def\ex@deadline{##2}%
  }%
  \foreach \ex@date[count=\ex@i] in {#1} {%
    \edef\ex@tmpdate{\ex@date}%
    \expandafter\ex@parsedates\ex@tmpdate\foo
    \expandafter\xdef\csname ex@tutorial@\ex@i\endcsname{\ex@tutorial}%
    \expandafter\xdef\csname ex@deadline@\ex@i\endcsname{\ex@deadline}%
  }%
}

\newcount\lec@lastlecnumber % stores the last lecture id
\newcount\ex@lastexercisenumber % same for exercise id

% Sets the chronological list of lectures with their ids. 
%
% \lecturelistofids{
%   <lecture id>,
%   <lecture id>,
%   <lecture id>,
% }

\def\lecturelistofids#1{
  \foreach \lec@id[count=\lec@i] in {#1} {
    \expandafter\xdef\csname lec@id@\lec@i\endcsname{\lec@id}%
    \expandafter\xdef\csname lec@number@\lec@id\endcsname{\lec@i}%
    \global\lec@lastlecnumber=\lec@i
  }
}

% Sets the chronological list of exercises with their ids. 
%
% \exerciselistofids{
%   <exercise id>,
%   <exercise id>,
%   <exercise id>,
% }

\def\exerciselistofids#1{
  \foreach \ex@id[count=\ex@i] in {#1} {
    \expandafter\xdef\csname ex@id@\ex@i\endcsname{\ex@id}%
    \expandafter\xdef\csname ex@number@\ex@id\endcsname{\ex@i}%
    \global\ex@lastexercisenumber=\ex@i
  }
}


% Testing whether a lecture exists

\def\ddiiflectureidexists#1#2#3{%
  \expandafter\ifx\csname lec@number@#1\endcsname\relax%
  #3\else#2\fi%
}


% Testing whether an exercise exists

\def\ddiifexerciseidexists#1#2#3{%
  \expandafter\ifx\csname ex@number@#1\endcsname\relax%
  #3\else#2\fi%
}


% Starts a new lecture. This defines the lecture title, subtitle, and
% the date for the next \maketitle command. Furthermore it sets the
% lecture number and defines a corresponding label.
%
% Usage: \lecturewithid{<lecture id>}

\def\lecturewithid#1#2#3{
  \lecture[\getlecturenumberfromid{#1}]{#2}{lecture-#1}
  \subtitle{#3}
  \date{\getlecturedatefromid{#1}}
}

% Starts a new exercise. This defines the sheet number, date of the
% tutorial, and the date for the next \maketitle command. Furthermore
% it sets the exercise number and defines a corresponding label.
%
% Usage: \exercisewithid{<exercise id>}

\def\exercisewithid#1{%
  \def\insertsheetnumber{\getexercisenumberfromid{#1}}%
  \edef\ddi@tmpdeadline{\get@exercisedeadlinedatefromid{#1}}%
  \def\ddi@none{none}%
  \let\insertdeadline=\undefined
  \ifx\ddi@none\ddi@tmpdeadline\else
    \def\insertdeadline{\getexercisedeadlinedatefromid{#1}}%
  \fi
}

% Getter and setter:
%
% Lecture
\def\getlecturedatefromid#1{%
  \edef\ddi@tmp@isodate{\csname lec@date@\getlecturenumberfromid{#1}\endcsname}%
  \expandafter\ddi@isodatetohumanreadable\ddi@tmp@isodate\pgf@stop%
}
\def\sem@getisodatefromlectureid#1{\csname lec@date@\getlecturenumberfromid{#1}\endcsname}
\def\getlecturenumberfromid#1{\csname lec@number@#1\endcsname}

% Exercise
\def\getexercisedeadlinedatefromid#1{%
  \edef\ddi@tmp@deadlineisodate{\csname ex@deadline@\getexercisenumberfromid{#1}\endcsname}%
  \edef\ddi@tmp@none{none}%
  \edef\ddi@tmp{\ddi@tmp@deadlineisodate}%
    \ifx\ddi@tmp@none\ddi@tmp
      \def\ddi@tmp@deadlinedate{keine Abgabe}%
    \else
      \def\ddi@tmp@deadlinedate{\expandafter\ddi@isodatetohumanreadable\ddi@tmp@deadlineisodate\pgf@stop}%
    \fi
  \ddi@tmp@deadlinedate%
}

\def\get@exercisedeadlinedatefromid#1{%
  \csname ex@deadline@\getexercisenumberfromid{#1}\endcsname
}

\def\getexercisetutorialdatefromid#1{%
  \edef\ddi@tmp@isodate{\csname ex@tutorial@\getexercisenumberfromid{#1}\endcsname}%
  \expandafter\ddi@isodatetohumanreadable\ddi@tmp@isodate\pgf@stop%
}
\def\sem@getisodatefromexerciseid#1{\csname ex@date@\getexercisenumberfromid{#1}\endcsname}
\def\getexercisenumberfromid#1{\csname ex@number@#1\endcsname}

\def\lecturegetfirstdate{\sem@getisodatefromlectureid{1}}
\def\lecturegetlastdate{\sem@getisodatefromlectureid{\lec@lastlecnumber}}

\def\setsummerfirstlectureid#1{\edef\lec@summerfirstlectureid{#1}}
\def\setsummerlastlectureid#1{\edef\lec@summerlastlectureid{#1}}
\def\setwinterfirstlectureid#1{\edef\lec@winterfirstlectureid{#1}}
\def\setwinterlastlectureid#1{\edef\lec@winterlastlectureid{#1}}

\def\getsummerfirstlectureid{\lec@summerfirstlectureid}
\def\getsummerlastlectureid{\lec@summerlastlectureid}
\def\getwinterfirstlectureid{\lec@winterfirstlectureid}
\def\getwinterlastlectureid{\lec@winterlastlectureid}

%
% Lecture titles and subtitles.
%

\def\getlecturetitlefromid#1{\sem@getlecturetitlesfromid{#1}\sem@lecturetitle}
\def\getlecturesubtitlefromid#1{\sem@getlecturetitlesfromid{#1}\sem@lecturesubtitle}

\newif\ifsem@continuereadfile

\def\sem@empty{}
\def\sem@lecturetitle{}
\def\sem@lecturesubtitle{}

\newread\sem@lecturetitlesinputstream
\newread\sem@exercisetitlesinputstream

% Searches the body file of lecture with id #1 for the first
% occurrence of \lecturewithid at the very beginning of a line and
% stores the corresponding lecture title and subtitle in the macros
% \sem@lecturetitle and \sem@lecturesubtitle respectively.
%
\newread\sem@inputstream%

\def\sem@getlecturetitlesfromid#1{%
  % Set iteration condition.
  \sem@continuereadfiletrue%
  %
  % Macro for extracting and saving the result.
  \def\sem@extractlecturetitles##1##2##3 \lecturewithid{%
    \def\sem@lecturetitle{##2}%
    \def\sem@lecturesubtitle{##3}%
  }%
  %
  % Macro for examining the line.
  \long\def\sem@checkandgetargument##1\lecturewithid##2\pgf@stop{%
    % Prepare for \ifx check.
    \def\sem@tmp{##1}%
    \def\sem@tmpa{##2}%
    \ifx\sem@tmp\sem@empty%
      \ifx\sem@tmpa\sem@empty%
      \else%
        % We found the line!
        \sem@continuereadfilefalse%
        % Extract and store the result.
        \expandafter\sem@extractlecturetitles##2%
      \fi%
    \fi%
  }%
  %
  % Open the corresponding file.
  \immediate\openin\sem@lecturetitlesinputstream=\sem@lecgetbodyfilefromid{#1}%
  % Iterate over the lines.
  \loop \ifsem@continuereadfile%
    % Read the next line.
    \immediate\read\sem@lecturetitlesinputstream to \sem@nextline
    % Examine the line.
    \expandafter\sem@checkandgetargument\sem@nextline\lecturewithid\pgf@stop%
  \repeat%
  % We're done. Close the input stream.
  \immediate\closein\sem@lecturetitlesinputstream
}

\def\sem@getexercisetitlesfromid#1{%
  % Set iteration condition.
  \sem@continuereadfiletrue%
  %
  % Macro for extracting and saving the result.
  \def\sem@extractexercisetitles##1 \title{%
    \def\sem@exercisetitle{##1}%
  }%
  %
  % Macro for examining the line.
  \long\def\sem@checkandgetargument##1\title##2\pgf@stop{%
    % Prepare for \ifx check.
    \def\sem@tmp{##1}%
    \def\sem@tmpa{##2}%
    \ifx\sem@tmp\sem@empty%
      \ifx\sem@tmpa\sem@empty%
      \else%
        % We found the line!
        \sem@continuereadfilefalse%
        % Extract and store the result.
        \expandafter\sem@extractexercisetitles##2%
      \fi%
    \fi%
  }%
  %
  % Open the corresponding file.
  \immediate\openin\sem@exercisetitlesinputstream=\sem@exgetbodyfilefromid{#1}%
  % Iterate over the lines.
  \loop \ifsem@continuereadfile%
    % Read the next line.
    \immediate\read\sem@exercisetitlesinputstream to \sem@nextline
    % Examine the line.
    \expandafter\sem@checkandgetargument\sem@nextline\title\pgf@stop%
  \repeat%
  % We're done. Close the input stream.
  \immediate\closein\sem@exercisetitlesinputstream
}

% To my future me: 
% You once really liked the two macros above.

%
% Lecture Parts
%

\newcount\sem@numberofparts % stores the number of ranges

% Sets the lecture parts.
%
% Usage:
% \lecturelistofparts{
%   <id of first lecture in part>=<parttitle>,
%   <id of first lecture in part>=<parttitle>,
% }

\def\lecturelistofparts#1{
  \foreach \sem@partstartidandtitle[count=\map@i] in {#1} {
    \expandafter\sem@parse@partstartidandtitle\sem@partstartidandtitle\pgf@stop{\map@i}
    \global\sem@numberofparts=\map@i
  }
}

\def\sem@parse@partstartidandtitle#1=#2\pgf@stop#3{
  \expandafter\gdef\csname sem@partstart@lectureid@#3\endcsname{#1}
  \expandafter\gdef\csname sem@partstart@title@#3\endcsname{#2}
}

% Getter and setter.

\def\sem@getnumberofsemesterparts{\the\sem@numberofparts}
\def\sem@getlectureidfrompartnumber#1{\csname sem@partstart@lectureid@#1\endcsname}
\def\sem@getparttitlefrompartnumber#1{\csname sem@partstart@title@#1\endcsname}


% Translates lecture ids to file names. This is required for
%   - automatically inserting the lecture targets into the map,
%   - extraction of the lecture titles and subtitles of the body
%     files.
%
% This translation process should be removed, but this requires
% appropriate renaming of the folders within lecture_slides.

\def\lectureidstofoldernamelist#1{
  \foreach \sem@idfilenametuple in {#1} {
    \expandafter\sem@parse@idandfilename\sem@idfilenametuple\pgf@stop
  }
}

\def\sem@parse@idandfilename#1=#2\pgf@stop{
  \expandafter\xdef\csname sem@filename@#1\endcsname{../#2/body.tex}
}

\def\sem@getbodyfilefromid#1{\csname sem@filename@#1\endcsname}
\def\sem@lecgetbodyfilefromid#1{../lecture_slides/#1/body.tex}
\def\sem@exgetbodyfilefromid#1{../exercises/#1/body.tex}



% The following macro creates the file  podcasts.aux  that is required
% for creating the XML files for Till's podcasts. In this file, the
% i'th line consists of the title and the subtitle of the i'th
% lecture, separated by two hashmarks.

\def\makeauxfiles{%
  % Open outstreams
  \message{^^JOpening outstreams.^^J}
  \newwrite\pod@auxfile       \immediate\openout\pod@auxfile=podcasts.aux
  \newwrite\pres@auxfile      \immediate\openout\pres@auxfile=upload-presentation-files.aux
  \newwrite\preswiki@auxfile  \immediate\openout\preswiki@auxfile=wiki-presentation-files.aux
  \newwrite\ex@auxfile        \immediate\openout\ex@auxfile=upload-exercise-files.aux
  \newwrite\exwiki@auxfile    \immediate\openout\exwiki@auxfile=wiki-exercise-files.aux

  % Define counters for the following loop that iterates over all the
  % lecture numbers.
  \newcount\pod@tmpcnt \pod@tmpcnt=0
  \newcount\pod@lecmax \pod@lecmax=\lec@lastlecnumber

  \newcount\ex@tmpcnt \ex@tmpcnt=0
  \newcount\ex@maxcnt \ex@maxcnt=\ex@lastexercisenumber

  % Create temporary token registers
  \newtoks\pod@titletoks
  \newtoks\pod@subtitletoks

  % Generate lecture files
  \message{^^JGenerating lecture files.}
  \message{Considering the following lecture IDs:^^J}
  \loop
    \def\lectureid{\csname lec@id@\the\pod@tmpcnt\endcsname}
    \advance\pod@tmpcnt by 1
    {
      \sem@getlecturetitlesfromid{\lectureid}
      \expandafter\global\expandafter\pod@titletoks\expandafter=\expandafter{\sem@lecturetitle}
      \expandafter\global\expandafter\pod@subtitletoks\expandafter=\expandafter{\sem@lecturesubtitle}
      \message{\the\pod@tmpcnt. \lectureid: \the\pod@titletoks^^J}
    }
    \immediate\write\pod@auxfile{\the\pod@titletoks__\the\pod@subtitletoks}
    \immediate\write\pres@auxfile{\lectureid}
    \edef\ddi@tmp@isodate{\csname lec@date@\getlecturenumberfromid{\lectureid}\endcsname}%
    \immediate\write\preswiki@auxfile{%
      1. [attachment:\lectureid.pdf Vorlesung: \the\pod@titletoks],
      \expandafter\ddi@isodatetohumanreadable\ddi@tmp@isodate\pgf@stop%
    }
  \ifnum\pod@tmpcnt<\pod@lecmax
  \repeat

  \message{^^JFinished generating lecture files.^^J}

  % Create temporary token registers
  \newtoks\ex@titletoks

  \message{^^JGenerating exercise files.}
  \message{Considering the following exercise IDs:^^J}

  % Generate exercise files
  \loop
    \def\exerciseid{\csname ex@id@\the\ex@tmpcnt\endcsname}
    \advance\ex@tmpcnt by 1
    {
      \sem@getexercisetitlesfromid{\exerciseid}
      \expandafter\global\expandafter\ex@titletoks\expandafter=\expandafter{\sem@exercisetitle}
      \message{\the\ex@tmpcnt. \exerciseid: \the\ex@titletoks^^J}
    }
    \immediate\write\ex@auxfile{\exerciseid}
    \edef\ddi@tmp@tutorialisodate{\csname ex@tutorial@\getexercisenumberfromid{\exerciseid}\endcsname}%
    \edef\ddi@tmp@tutorialdate{\expandafter\ddi@isodatetohumanreadable\ddi@tmp@tutorialisodate\pgf@stop}%
    \edef\ddi@tmp@deadlineisodate{\csname ex@deadline@\getexercisenumberfromid{\exerciseid}\endcsname}%
    % Check whether sheet has deadline or not
    \def\ddi@tmp@none{none}%
    \edef\ddi@tmp{\ddi@tmp@deadlineisodate}%
    \ifx\ddi@tmp@none\ddi@tmp
      \def\ddi@tmp@deadlinedate{keine Abgabe}
    \else
      \def\ddi@tmp@deadlinedate{\expandafter\ddi@isodatetohumanreadable\ddi@tmp@deadlineisodate\pgf@stop}%
    \fi
    \immediate\write\exwiki@auxfile{%
      || [attachment:\exerciseid.pdf \the\ex@tmpcnt. Übung: \the\ex@titletoks] 
      || \ddi@tmp@tutorialdate || \ddi@tmp@deadlinedate ||%
    }
  \ifnum\ex@tmpcnt<\ex@maxcnt
  \repeat

  \message{^^JFinished generating exercise files.^^J}
  \message{^^JClosing outstreams.^^J}

  % Close outstreams
  \closeout\pod@auxfile     
  \closeout\pres@auxfile    
  \closeout\preswiki@auxfile
  \closeout\ex@auxfile      
  \closeout\exwiki@auxfile  

  \message{Finished. Bye^^J^^J}
}

\def\ddi@isodatetohumanreadable#1-#2-#3\pgf@stop{%
  \number#3. \translate{\ddi@arabmonth@to@textmonth{#2}} #1}

\def\ddi@arabmonth@to@textmonth#1{\csname ddi@monthname@#1\endcsname}

\expandafter\def\csname ddi@monthname@01\endcsname{Januar}
\expandafter\def\csname ddi@monthname@02\endcsname{Februar}
\expandafter\def\csname ddi@monthname@03\endcsname{März}
\expandafter\def\csname ddi@monthname@04\endcsname{April}
\expandafter\def\csname ddi@monthname@05\endcsname{Mai}
\expandafter\def\csname ddi@monthname@06\endcsname{Juni}
\expandafter\def\csname ddi@monthname@07\endcsname{Juli}
\expandafter\def\csname ddi@monthname@08\endcsname{August}
\expandafter\def\csname ddi@monthname@09\endcsname{September}
\expandafter\def\csname ddi@monthname@10\endcsname{Oktober}
\expandafter\def\csname ddi@monthname@11\endcsname{November}
\expandafter\def\csname ddi@monthname@12\endcsname{Dezember}
