%--------------------
%                    
%       NGT          
%                    
%--------------------
%                    
% Marian Gencev, 2020
%                    
%--------------------


\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{ngt}[2020/02/29]

\LoadClass[12pt]{article}

\newcount\PocetVoleb
\newif\ifdatabaze

\DeclareOption{databaze}{%
  \databazetrue
  \advance\PocetVoleb by 1}

\DeclareOption{pisemka}{%
  \databazefalse
  \advance\PocetVoleb by 1}

\ProcessOptions
%-------------------------------




% spolecna nastaveni
%-------------------------------
\def\bfdefault{b}

\RequirePackage{lmodern}
\RequirePackage[utf8]{inputenc}
\RequirePackage[T1]{fontenc}

\RequirePackage[margin=2cm]{geometry}

\newcount\PocetDatabazi

\ifnum\PocetVoleb=1
\else
  \ClassError{ngt}{Prilis mnoho nactenych voleb nebo zadna volba}{}%
\fi

\parindent=0pt
\let\OdsadReseni\bigskip
\let\ea\expandafter
\let\nx\noexpand

\def\ReseniIkona{%
  \pdfliteral{%
   q
  .7 g
   .9 0 0 .9 0 0 cm
   0 0 4 4 re f
   1 0 0 1 5 0 cm
   0 0 4 4 re f
   1 0 0 1 0 5 cm
   0 0 4 4 re f
   .7071 .7071 -.7071 .7071 -5 1 cm
   1 0 0 rg
   0 0 4 4 re f
   Q}}


\setbox 0\hbox to 1.8em{\hskip3pt\vbox to 1em{\vss\ReseniIkona}\hss}
\pdfxform 0
\edef\PDFreseni{\the\pdflastxform}
\def\NavestiReseni{%
  \leavevmode
  \pdfrefxform\PDFreseni}


\def\otevri{%
  \immediate\openout }
\def\zapis{%
  \immediate\write }
\def\zavri{%
  \immediate\closeout }

\def\PredLomitkem#1/#2,{#1}
\def\ZaLomitkem#1/#2,{#2}



%  Pocitadlo znaku
%---------------------------
\def\UkonciPocitadlo{}
\def\UkonciPocitadloToken{\UkonciPocitadlo}
\newcount\pocet

\def\PocetVyskytu[#1,#2]{%
  \def\TestovanyZnak{#1}%
  \pocet=0
  \newcount#2%
  \let\pocitadlo#2%
  \let\next\nacti\next}

\long\def\nacti#1{%
  \def\znak{#1}%
  \ifx\znak\UkonciPocitadloToken
    \def\next{\pocitadlo=\pocet}%
  \else
    \ifx\znak\TestovanyZnak
      \advance\pocet by 1
    \fi
  \fi
  \next}


%  Nastaveni jazyku
%---------------------------
\newcount\PocetJazyku
\newcount\PoradiJazyku
\newif\ifjazyky
\newif\ifjazyk
  \jazykyfalse
  \jazykfalse

\newwrite\nastaveniJazyky

\def\jazyky#1 {%
  \jazykytrue
  \ifdatabaze
    \otevri\nastaveniJazyky jazyky.uzv
    \zapis\nastaveniJazyky{\nx\jazyky #1}%
    \zavri\nastaveniJazyky
  \fi
  \PocetVyskytu[/,\PocetLomitek]#1\UkonciPocitadlo
  \ifnum\PocetLomitek=0
    \def\JedinyJazyk{#1}%
    \PocetJazyku=1
  \else
    \xjazyky#1,/,
    \def\SeznamJazyku{\xseznam#1,/,}
    \ifnum\PocetJazyku=1
      \def\JedinyJzk{\ZaLomitkem #1, }
      \ea\jazyk\JedinyJzk
    \fi
  \fi}

\def\xjazyky#1,{%
  \ifx/#1%
    \PocetJazyku=\the\PoradiJazyku
  \else
    \PridejJazyk#1/%
    \advance\PoradiJazyku by 1
    \ifnum\PoradiJazyku=1
      \def\PrvniJzk{\ZaLomitkem #1, }
    \fi
    \ea\xjazyky
  \fi}

\def\xseznam#1,{%
  \ifx/#1%
  \else
    \PredLomitkem#1,,%
    \ea\xseznam
  \fi}

\def\PridejJazyk#1/#2/{%
  \ea\def\csname jazyk:#2\endcsname{#1}%
  \ea\def\csname #2\endcsname##1{}}

\def\jazyk#1 {%
  \jazyktrue
  \def\AktivniJzk{#1}%
  \ifcsname jazyk:#1\endcsname
    \ea\def\csname #1\endcsname##1{##1}%
  \else
    \ifjazyky
      \ClassError{ngt}{Neznamy jazykovy identifikator '#1'}{}%
    \fi
  \fi}

\let\origdocument\document
\def\document{%
  \endgroup
  %
  \ifjazyky
    \ifjazyk
    \else
      \ifnum\PocetJazyku>1
        \ea\jazyk\PrvniJzk
        \ClassWarning{ngt}{Nastaveno \string\jazyk\space `\PrvniJzk'}%
      \fi
    \fi
  \fi
  %
  \ifjazyky
    \ifnum\PocetLomitek=0
      \usepackage[\JedinyJazyk]{babel}%
    \else
      \usepackage[\SeznamJazyku]{babel}%
    \fi
  \else
    \ifjazyk
      \ClassError{ngt}{Doplnte deklaraci \string\jazyky}{}%
    \else
      \usepackage[czech]{babel}
    \fi
  \fi
  %
  \begingroup\origdocument
  %
  \ifjazyky
    \ifjazyk
      \ifnum\PocetJazyku=1
      \else
        \ea\ea\ea\selectlanguage
        \ea\ea\ea{\csname jazyk:\AktivniJzk\endcsname}
      \fi
    \else
      \ifnum\PocetJazyku>1
        \ea\ea\ea\selectlanguage
        \ea\ea\ea{\csname jazyk:\PrvniJzk\endcsname}
      \fi
    \fi
  \fi}


%  Vytvareni 0/1 prepinacu
%---------------------------
\def\etiam{1}
\def\nihil{0}

\def\OIprepinac#1/#2 {%
  \def\init{#2}%
  \ea\newif\csname if#1\endcsname
  \ifx\init\etiam
    \csname #1true\endcsname
  \else
    \ifx\init\nihil
      \csname #1false\endcsname
    \else
      \ClassError{ngt}{Neocekavana inicialni hodnota '#2' v '#1'}{}%
    \fi
  \fi
  %
  \ea\def\csname #1\endcsname##1 {%
    \def\hodnota{##1}%
    \ifx\hodnota\etiam
      \csname #1true\endcsname
    \else
      \ifx\hodnota\nihil
        \csname #1false\endcsname
      \else
        \ClassError{ngt}{Neocekavana hodnota (#1)}{}%
      \fi
    \fi}}

\OIprepinac vyres/1
\OIprepinac novy/1
\OIprepinac body/1



%  Makra s parametrem do konce radku (OPmac)
%---------------------------
\def\eoldef#1{%
  \def#1{\begingroup \catcode`\^^M=12 \eoldefA#1}%
   \ea\def\csname\string#1:M\endcsname}
{\catcode`\^^M=12 \gdef\eoldefA#1#2^^M{%
  \endgroup\csname\string#1:M\endcsname{#2}}}



%  Rozdeleni argumentu
%---------------------------
\long\def\RozdelArgument#1#2***{%
  \def\PrvniToken{#1}%
  \long\def\ZbyleTokeny{#2}}


\newread\testin

\def\softinput #1 {%
  \let\next\relax
  \openin\testin=#1
  \ifeof\testin
  \else
    \closein\testin
    \def\next{\input #1 }%
  \fi
  \next}

\softinput nastaveni.tex


\newif\ifoddil


\ifdatabaze

  % nastaveni databaze
  %-------------------------------
  \def\OdsadUlohu{\vskip1.5em}
  
  \newcount\PoradiUlohy
  \newcount\sekce
  
  \newwrite\zadaniSoubor
    \newtoks\zadaniTokeny
    \newtoks\reseniTokeny
  
    \otevri\zadaniSoubor databaze.pkd
  
  \def\ZapisDefinici#1#2#3{%
    \zapis#1{\nx\ea\def\nx\csname#2\endcsname{#3}}}
  
  \def\ctverecek{%
    \pdfliteral{%
     q .7 g 0 0 6 6 re f Q}}
  
  \setbox 0\hbox to 1em{\vbox to 1em{\vss\ctverecek}\hss}
  \pdfxform 0
  \edef\PDFctverecek{\the\pdflastxform}
  \def\NavestiUlohy{%
    \leavevmode
    \llap{\pdfrefxform\PDFctverecek\hskip5pt}{\bfseries\the\sekce.\the\PoradiUlohy}~~}

  
  \def\databaze#1/#2b{%
    \ifnum\sekce>0
      \ZapisDefinici\zadaniSoubor{PocetUloh|\the\sekce|}{\the\PoradiUlohy}%
    \fi
    \PoradiUlohy=0
    \OdsadUlohu
    \section{#1}
    \oddiltrue
    \sekce=\arabic{section}%
    \ZapisDefinici\zadaniSoubor{Body|\the\sekce|}{#2}}
  %
  \eoldef\subdatabaze#1{%
    \ifoddil\else\OdsadUlohu\fi
    \subsection{#1}
    \oddiltrue}
  %-----------------

  \long\def\RozdelZadaniReseni#1\reseni#2***{%
    \long\def\xzadani{#1}%
    \long\def\xreseni{#2}}
  
  \def\hvezda{*}
  
  \long\def\uloha#1***{%
    \RozdelArgument#1***
    \PocetVyskytu[\reseni,\PocetReseni]#1\UkonciPocitadlo
    \ifx\PrvniToken\hvezda
    \else
      \advance\PoradiUlohy by 1
      \ifoddil
        \oddilfalse
      \else
        \OdsadUlohu
      \fi
      \ifcase\PocetReseni
        % #\reseni=0
        %------------
        \zadaniTokeny={#1}
        \NavestiUlohy #1\par
        \ZapisDefinici\zadaniSoubor{U|\the\sekce|\the\PoradiUlohy|}{\the\zadaniTokeny}
      \or
        % #\reseni=1
        %------------
        \RozdelZadaniReseni#1***
        \ea\zadaniTokeny\ea=\ea{\xzadani}
        \ea\reseniTokeny\ea=\ea{\xreseni}
        %
        \ZapisDefinici\zadaniSoubor{U|\the\sekce|\the\PoradiUlohy|}{\the\zadaniTokeny}
        \ZapisDefinici\zadaniSoubor{R|\the\sekce|\the\PoradiUlohy|}{\the\reseniTokeny}
        %
        \NavestiUlohy\xzadani\par
        \ifvyres
          \OdsadReseni
          {\footnotesize\NavestiReseni\xreseni}\par
        \fi
      \else
        % #\reseni>1
        %------------
        \ClassError{ngt}{%
          V prostredi \string\uloha\space muzete pouzit \string\reseni\space nejvyse jednou}{}%
      \fi
    \fi}
  %-----------------  

  \AtEndDocument{%
    \ZapisDefinici\zadaniSoubor{PocetUloh|\the\sekce|}{\the\PoradiUlohy}
    \zapis\zadaniSoubor{\PocetDatabazi=\the\sekce}
    \zavri\zadaniSoubor}
  %-------------------------------





\else

  % nastaveni pisemky
  %-------------------------------
  \advance\topmargin by -20pt
  \input databaze.pkd
  \softinput jazyky.uzv
  
  \def\OdsadUlohu{%
    \bigskip\bigskip}

  \def\DnuVMesici{%
    \ifcase\month
    \or 31
    \or\ifnum\numexpr\year/4*4-\year=0 29 \else 28 \fi
    \or 31 \or 30 \or 31 \or 30
    \or 31 \or 31 \or 30 \or 31
    \or 30 \or 31 \fi}
  
  \def\PrictiDny#1{%
    \advance\day#1\relax
    \ifnum#1>0
      \OpravDatumVpred
    \else
      \OpravDatumVzad
    \fi}

  \def\OpravDatumVpred{%
    \ifnum\day>\DnuVMesici
      \advance\day by -\DnuVMesici
      \advance\month by 1
      \ifnum\month>12
        \month=1
        \advance\year by 1
      \fi
    \ea\OpravDatumVpred
    \fi}

  \def\OpravDatumVzad{%
    \ifnum\day<1
      \advance\month by -1
      \ifnum\month<1
        \month=12
        \advance\year by -1
      \fi
      \advance\day\DnuVMesici
      \ea\OpravDatumVzad
    \fi}

  \def\Datum{\today}
  
  \eoldef\datum#1{%
    \RozdelArgument#1***
    \gdef\Datum{%
      \if\PrvniToken+
        {\PrictiDny{\ZbyleTokeny}\today}%
      \else
        \if\PrvniToken-
          {\PrictiDny{-\ZbyleTokeny}\today}%
        \else#1%
        \fi
      \fi}}

  \def\JmenoIkona{%
    \pdfliteral{%
     q
     1 G .5 g 2 w 0 0 m
      0  5  5 10 10 10 c
     15 10 20  5 20  0 c h B
     %
     .3 G .4 w 30 0 m
     200 0 l s
     %
     .7 G .7 g 10 10 m
      4 10    5 17.5 10 17.5 c 10 17.5 m
     15 17.5 16 10   10 10   c h f
     Q}}
  
  \def\OsobniCisloIkona{%
    \pdfliteral{%
     q
     .5 G .5 g 2 w
     2.5 0 m  5   10 l s
     6   0 m  8.5 10 l s
      .7 3 m 10    3 l s
     1.5 7 m 10.7  7 l s
     Q}}

  \def\Hlavicka{%
    \ifvyres
    \else
      \hrule height 1.5pt
      \par\vskip30pt
      \leavevmode\JmenoIkona
      \par\medskip
      \leavevmode\OsobniCisloIkona\hskip10pt\OsobniCisloIkona
    \fi
    \hfill
    \Datum\medskip\par
    \hrule\par\vskip 2pt
    \hrule height 1.5pt\bigskip}%
  
  \def\hlavicka#1{%
    \ifx#10
      \let\Hlavicka\relax
    \else
      \xhlavicka#1%
    \fi}

  \long\def\xhlavicka#1***{%
    \def\Hlavicka{{#1}}}%

  \newcount\PoradiDatabaze
  \newdimen\sirka
%   \newbox\MujBox
  
  \def\BodyBox{%
%     \setbox\MujBox\hbox{\scriptsize\sffamily\csname Body|\the\PoradiDatabaze|\endcsname}%
    \hbox to 0pt{\hss\scriptsize\sffamily\csname Body|\the\PoradiDatabaze|\endcsname\hss}%
%     \sirka=\wd\MujBox
%     \divide\sirka by 2
%     \hskip-\sirka\relax\rlap{\box\MujBox}
  }

  \def\BodyIkona{%
    \llap{%
      \pdfliteral{%
       q
      .8 0 0 .8 0 0 cm
      .85 g
      .5 G
      .7 w
      -10 0 m
      -10 5 -3 12 0 12 c
      3 12 10 5 10 0 c
      10 0 10 -5 0 -5 c
      -10 -5 -10 0 -10 0 c B
      %
      -2 11 m
      -5 15 l
      0 13 0 18 5 15 c
      2 11 l B 
      %
      .2 G
      1.5 w
      -2.5 11 m
      0 11.5 0 11.5 2.5 11 c B Q}%
      \BodyBox\hskip 2.5em}}
   
  \def\NavestiUlohy{%
    \leavevmode
    \ifbody
      \BodyIkona
    \fi
    {\bfseries\the\PoradiDatabaze. }}

  \def\tiskni #1 {%
    {\csname #1\endcsname}\par}

  \newwrite\pametSoubor

  \def\ZapisPamet #1|#2|#3|{%
    \ifcsname #1|#2|#3|\endcsname
      \zapis\pametSoubor{%
        \nx\ea\def\nx\csname #1P|#2|\endcsname{%
          \nx\csname #1|#2|#3|\endcsname}}
    \fi}

  \newcount\NahodneCislo
  
  \def\generujNC{%
    \NahodneCislo=\pdfuniformdeviate\csname PocetUloh|\the\PoradiDatabaze|\endcsname
    \advance\NahodneCislo by 1}

  \def\generujTEST{%
    \PoradiDatabaze=1
    \ifvyres
    \else
      \thispagestyle{empty}
    \fi
    \Hlavicka
    \ifnovy
      \otevri\pametSoubor vyber.pmt
    \else
      \input vyber.pmt
    \fi
    %
    \loop\ifnum\PocetDatabazi<\PoradiDatabaze
    \else
      \OdsadUlohu
      \ifnovy
        \generujNC
        \NavestiUlohy\tiskni U|\the\PoradiDatabaze|\the\NahodneCislo|
        \ZapisPamet U|\the\PoradiDatabaze|\the\NahodneCislo|
        \ZapisPamet R|\the\PoradiDatabaze|\the\NahodneCislo|
        \ifvyres
          \ifcsname R|\the\PoradiDatabaze|\the\NahodneCislo|\endcsname
            \OdsadReseni
            {\footnotesize\NavestiReseni\tiskni R|\the\PoradiDatabaze|\the\NahodneCislo| }
          \fi
        \fi
      \else
        \NavestiUlohy
        \tiskni UP|\the\PoradiDatabaze|
        \ifvyres
          \OdsadReseni
          \ifcsname RP|\the\PoradiDatabaze|\endcsname
            {\footnotesize\NavestiReseni\tiskni RP|\the\PoradiDatabaze| }
          \fi
        \fi
      \fi
      \advance\PoradiDatabaze by 1
    \repeat
    %
    \ifnovy
      \zavri\pametSoubor
    \fi}%
\fi
%-------------------------------