% Based on thesis template by Antti-Juhani Kaijanaho and Matthieu Weber.
% You may treat this file as if it were in the public domain.
% Rewrited by Ville Isomöttönen for the use of TIE projetcs.

\def\filedate{2005/09/14}
\def\fileversion{0.1}
\def\fileinfo{JY MIT Projektit}

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{projdoc}[\filedate\space\fileversion\space\fileinfo]
\typeout{projdoc <\filedate:\fileversion> - Tietotekniikan laitoksen projekteille}

\newif\ifnumbib
\numbibtrue

\newif\ifnottoc
\nottoctrue

\def\projdoc@baseclass{report}

\newif\ifaltsubsec
\altsubsecfalse

%% Input encoding
\def\projdoc@inputenc{latin1}
%\DeclareOption{utf8}{\gdef\projdoc@inputenc{utf8}}
\DeclareOption{ansinew}{\gdef\projdoc@inputenc{ansinew}}
\DeclareOption{applemac}{\gdef\projdoc@inputenc{applemac}}
\DeclareOption{ascii}{\gdef\projdoc@inputenc{ascii}}
\DeclareOption{cp1250}{\gdef\projdoc@inputenc{cp1250}}
\DeclareOption{cp1252}{\gdef\projdoc@inputenc{cp1252}}
\DeclareOption{cp437}{\gdef\projdoc@inputenc{cp437}}
\DeclareOption{cp437de}{\gdef\projdoc@inputenc{cp437de}}
\DeclareOption{cp850}{\gdef\projdoc@inputenc{cp850}}
\DeclareOption{cp852}{\gdef\projdoc@inputenc{cp852}}
\DeclareOption{cp865}{\gdef\projdoc@inputenc{cp865}}
\DeclareOption{decmulti}{\gdef\projdoc@inputenc{decmulti}}
\DeclareOption{latin1}{\gdef\projdoc@inputenc{latin1}}
\DeclareOption{latin2}{\gdef\projdoc@inputenc{latin2}}
\DeclareOption{latin3}{\gdef\projdoc@inputenc{latin3}}
\DeclareOption{latin5}{\gdef\projdoc@inputenc{latin5}}
\DeclareOption{latin9}{\gdef\projdoc@inputenc{latin9}}
\DeclareOption{next}{\gdef\projdoc@inputenc{next}}
\DeclareOption{numbib}{\numbibtrue}
\DeclareOption{nonumbib}{\numbibfalse}
\DeclareOption{toc}{\nottocfalse}
\DeclareOption{nottoc}{\nottoctrue}
\DeclareOption{altsubsec}{\altsubsectrue}
\let\projdoc@lof\relax
\let\projdoc@lot\relax
\DeclareOption{lof}{\gdef\projdoc@lof{\listoffigures}}
\DeclareOption{lot}{\gdef\projdoc@lot{\listoftables}}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{\projdoc@baseclass}}
\ProcessOptions
\LoadClass[a4paper,12pt]{\projdoc@baseclass}
\RequirePackage{palatino}
\RequirePackage[dvips]{graphicx}
\RequirePackage{ifthen}
\RequirePackage{lastpage}
\RequirePackage{fancyhdr}
\RequirePackage{color}
\RequirePackage{everyshi}[1994/12/09]
\RequirePackage{hyperref}
\RequirePackage[\projdoc@inputenc]{inputenc}
\RequirePackage[T1]{fontenc}
\RequirePackage[left=35mm,right=20mm,top=35mm,bottom=35mm,nohead]{geometry}
\RequirePackage[finnish,english]{babel}
\ifnumbib
\ifnottoc
\RequirePackage[numbib,nottoc]{tocbibind}
\else %nottoc
\RequirePackage[numbib]{tocbibind}
\fi %nottoc
\else %numbib
\ifnottoc
\RequirePackage[nottoc]{tocbibind}
\else  %nottoc
\RequirePackage{tocbibind}
\fi %nottoc
\fi %numbib

\def\versionhistoryname{Version history}
\def\appendicesname{Appendices}
\renewcommand{\baselinestretch}{1.2}
\newenvironment{tablist}[2]{%
    \noindent\textbf{#1:}\par%
    %\vspace{0.5truecm}%
    \ifthenelse{\equal{#2}{}}%
      {\begin{tabular}{lll}}%
      {\begin{tabular}{#2}}%
    }%
    {\end{tabular}
     \vspace*{0.5truecm}}
\newcommand{\tabrowx}[3]{~~~$\bullet$~~#1 & #2 & #3 \\}
\newenvironment{Ventry}[1]%
  {\begin{list}{}%
    {\settowidth{\labelwidth}{\hspace{1truecm}\bf{#1}}%
     \setlength{\labelsep}{0.5truecm}%
     \setlength{\leftmargin}{\labelwidth}%
     \renewcommand{\makelabel}[1]{\hspace{1truecm}\bf{##1}\hfil}%
    }%
  }%
  {\end{list}}
\setlength{\parindent}{0pt}
\newcounter{pages}
\def\selectdefaultlanguage{\selectlanguage{english}}
\AtBeginDocument{%
  \selectdefaultlanguage
  \maketitle
  \pagenumbering{roman}
  %% header and footer for infopages
  \pagestyle{fancyplain}
  \fancyhead{}
  \fancyhead[LO,LE]{\small{\projdoc@project~Project}}
  \fancyhead[CO,CE]{\small{\projdoc@title \ \projdoc@version}}
  \fancyhead[RO,RE]{\small{\projdoc@publicity}}
  \fancyfoot[C]{\thepage}
  \renewcommand{\headrulewidth}{0.4pt}
  \renewcommand{\headheight}{14pt}
  \renewcommand{\headsep}{1em}
  \renewcommand{\topmargin}{0pt}
  %\projdoc@docinfo
  %\projdoc@filldocinfo
}
%\newcounter{TotPages}
%\EveryShipout{\stepcounter{TotPages}}
%-------------------------------------
%\def\TotPages@putlabel{%
%
%  \if@filesw
%  \begingroup
%  \ifHy@pageanchor
%  \ifHy@hypertexnames
%  \ifHy@plainpages
%  \def\Hy@temp{\arabic{page}}%
%  \else
%  \let\textlatin\@firstofone
%  \edef\Hy@temp{\thepage}%
%  \fi
%  \else
%  \def\Hy@temp{\the\Hy@pagecounter}%
%  \fi
%  \fi
%  \immediate\write\@mainaux{%
%    \string\newlabel
%    {TotPages}{{\theTotPages}{\thepage}{}{%
%        \ifHy@pageanchor page.\Hy@temp\fi}{}}%
%  }%
%  \endgroup
%  \fi
%  \addtocounter{page}{1}%
%}%
%--------------------------------------------
\AtEndDocument{%
%\addtocounter{page}{-1}%
%\addtocounter{pages}{\thepage}
%\immediate\write\@mainaux{\string\newlabel\string{TotalPages\string}%
%\string{\string{0\string}\string{\thepages\string}\string}}%
%\TotPages@putlabel
}
%\string\def\string\projdoc@numpages\string{\thepage\string}}}
\def\projdoc@muistitko#1{\textit{\projdoc@msg@muistitko} \texttt{\textbackslash #1}\textit{\projdoc@msg@komentoa}}
\def\projdoc@title{\projdoc@muistitko{title}}
\def\projdoc@author{\projdoc@muistitko{author}}
\def\projdoc@project{\projdoc@muistitko{project}}
\def\projdoc@date{{\number\day}.{\number\month}.{\number\year}}
\def\projdoc@paikka{Jyväskylä}
\def\projdoc@place{Jyväskylä}
\def\projdoc@yliopisto{Jyväskylän yliopisto}
\def\projdoc@university{University of Jyväskylä}
\def\projdoc@laitos{Tietotekniikan laitos}
\def\projdoc@department{Department of Mathematical Information Technology}
\def\projdoc@publicity{\projdoc@muistitko{publicity}}
\def\projdoc@version{\projdoc@muistitko{version}}
\def\projdoc@docabstract{\projdoc@muistitko{docabstract}}
\def\projdoc@projectabstract{\projdoc@muistitko{projectabstract}}
\def\projdoc@avainsanat{\projdoc@muistitko{keywords}}
\newcommand{\authorinitials}[4]{%
   \gdef\projdoc@firstinitial{#1}%
   \gdef\projdoc@secondinitial{#2}%
   \gdef\projdoc@thirdinitial{#3}%
   \gdef\projdoc@fourthinitial{#4}}%
\newcommand{\authoremails}[4]{%
   \gdef\projdoc@firstemail{#1}%
   \gdef\projdoc@secondemail{#2}%
   \gdef\projdoc@thirdemail{#3}
   \gdef\projdoc@fourthemail{#4}}
\newcommand{\authorphones}[4]{%
   \gdef\projdoc@firstphone{#1}%
   \gdef\projdoc@secondphone{#2}%
   \gdef\projdoc@thirdphone{#3}
   \gdef\projdoc@fourthphone{#4}}
\newcommand{\project}[1]{\gdef\projdoc@project{#1}}
\renewcommand{\title}[1]{\gdef\projdoc@title{#1}}
% TEKIJÄT ALLEKAIN JA ERIKSEEN
\renewcommand{\author}[4]{%
    \gdef\projdoc@author{#1 \\#2 \\#3 \\#4}%
    \gdef\projdoc@firstauthor{#1}%
    \gdef\projdoc@secondauthor{#2}%
    \gdef\projdoc@thirdauthor{#3}
    \gdef\projdoc@fourthauthor{#4}}
\renewcommand{\date}[1]{%
  \ClassWarning{projdoc}{\textbackslash date is deprecated!}%
  \gdef\projdoc@date{#1}%
}
\newcommand{\setdate}[3]{\gdef\day{#1}\gdef\month{#2}\gdef\year{#3}}
\newcommand{\paikka}[1]{\gdef\projdoc@place{#1}}
\newcommand{\yliopisto}[1]{\gdef\projdoc@university{#1}}
\newcommand{\laitos}[1]{\gdef\projdoc@department{#1}}
\newcommand{\publicity}[1]{\gdef\projdoc@publicity{#1}}
\newcommand{\version}[1]{\gdef\projdoc@version{#1}}
\newcommand{\keywords}[1]{\gdef\projdoc@keywords{#1}}
\long\def\projectabstract#1{\gdef\projdoc@projectabstract{#1}}
\long\def\docabstract#1{\gdef\projdoc@docabstract{#1}}
%\newcommand{\projdoc@toolate}{%
%  \ClassError{projdoc}{%
%    Bibliografiatietoja ei voi antaa enaa dokumentin alun jalkeen.%
%  }{%
%    Komennot kuten \textbackslash title voi antaa vain ennen
%    \textbackslash begin\{document\}-komentoa.
%  }%
%}

%\@ifundefined{\csname projdoc@numpages \endcsname}
%  \def\projdoc@numpages{\textbf{?}}
%\fi

% Toc and first chapter have to start from odd page,
% I didn't manage to force pagebreaks with usual commands
% (clearpage, cleardoublepage, newpage, pagebreak),
% so I just added the '~' -character.

\newcommand{\mainmatter}{
  \ifthenelse{\isodd{\value{page}}}{\newpage~}{}
  \tableofcontents
  \ifthenelse{\isodd{\value{page}}}{\newpage~}{}
  \projdoc@lof
  \projdoc@lot
  \setcounter{pages}{\value{page}}
  \clearpage
  \pagenumbering{arabic}
  \setcounter{chapter}{0}
  % Header and footer from the mainmatter
  \pagestyle{fancyplain}
  \fancyhead{}
  \fancyhead[LO,LE]{\small{\projdoc@project~Project}}
  \fancyhead[CO,CE]{\small{\projdoc@title \ \projdoc@version}}
  \fancyhead[RO,RE]{\small{\projdoc@publicity}}
  \fancyfoot[C]{\thepage(\pageref{LastPage})}
  \renewcommand{\headrulewidth}{0.4pt}
  \renewcommand{\headheight}{14pt}
  \renewcommand{\headsep}{1em}
  \renewcommand{\topmargin}{0pt}
  \setlength{\parskip}{1.5ex plus 0.5ex minus 0.2ex}
}
\newcommand{\projdoc@authorinfo}{%
   \begin{tablist}{Authors}{p{6cm}p{6cm}p{3cm}}
   \tabrowx{\projdoc@firstauthor~(\projdoc@firstinitial)}{\texttt{\projdoc@firstemail}}{\projdoc@firstphone}
   \tabrowx{\projdoc@secondauthor~(\projdoc@secondinitial)}{\texttt{\projdoc@secondemail}}{\projdoc@secondphone}
   \tabrowx{\projdoc@thirdauthor~(\projdoc@thirdinitial)}{\texttt{\projdoc@thirdemail}}{\projdoc@thirdphone}
   \tabrowx{\projdoc@fourthauthor~(\projdoc@fourthinitial)}{\texttt{\projdoc@fourthemail}}{\projdoc@fourthphone}
   \end{tablist}}

\renewcommand{\maketitle}{%
%----------KANSISIVU-ALKAA---------->
\begin{titlepage}%
    \vspace*{5truecm plus 1truecm minus 3truecm}%
    \centerline{\Large\textbf{\parbox[top][\height][c]{\textwidth}{\centering \projdoc@project~Project}}}%
    \vspace{1truecm}%
    \centerline{\Large\textbf{\parbox[top][\height][c]{\textwidth}{\centering \projdoc@title}}}%
    \vspace{1truecm}%
    \center{\textbf{\projdoc@author}}%
    \vspace{1truecm}%
	\center{\scalebox{3.0}{\includegraphics[width=3cm]{logo.eps}}}
    \vspace{1truecm}%
    \par{%
       \centerline{Version \projdoc@version}%
       \centerline{\projdoc@publicity}%
       \par%
       \centerline{\projdoc@date}%
    }%
    \vfill%
    \vspace{1truecm plus 1truecm minus .5truecm}%
    \center{\textbf{\projdoc@university}}%
    \center{\textbf{\projdoc@department}}%
    \center{\textbf{\projdoc@place}}%
\end{titlepage}%
\newpage%

%-------KANSISIVUN--KÄÄNTÖPUOLI-ALKAA------------>

\begin{titlepage}%
\vspace*{20truecm}%

%\renewcommand{\arraystretch}{2}
%\begin{tabular}{|l|r|p{4cm}|p{4cm}|}
%\hline
%\textbf{Acceptor} & \textbf{Date} & \textbf{Signature} & \textbf{Clarification} \\ 
%\hline\hline
%Project manager & \_\_.\_\_.\the\year & &\\
%\hline
%Customer           & \_\_.\_\_.\the\year & &\\
%\hline
%Instructor         & \_\_.\_\_.\the\year & &\\
%\hline
%\end{tabular}
\end{titlepage}%
\newpage%
}
%--------KOMENTO DOKUMENTTITIETOJEN LATOMISEKSI---
%\newcommand{\projdoc@filldocinfo}{%
%   \projdoc@authorinfo\par%
   %\vspace{0.5truecm}\par%
%   \noindent\textbf{Document name:} \projdoc@project project,~\projdoc@title\par%
%   \noindent\textbf{Page count:} \pageref{LastPage}\par%
%   \noindent\textbf{File name:} \texttt{\jobname.tex}\par%
%   \vspace{0.5truecm}
%   \noindent\textbf{Abstract:} \projdoc@docabstract\par%
%   \noindent\textbf{Keywords:} \projdoc@keywords\par%
%}
%-------KOMENTO PROJEKTIN TIETOJEN LATOMISEKSI---
%\newcommand{\projdoc@fillprojinfo}{%
%   \noindent\projdoc@projectabstract\par%
%   \vspace{0.5truecm}
%   \noindent\projdoc@authorinfo}
   

%\newcommand{\preface}{\chapter*{\prefacename}
%   \addcontentsline{toc}{chapter}{\prefacename}}
%\newcommand{\termlist}{\chapter*{\termlistname}
%   \addcontentsline{toc}{chapter}{\termlistname}}
% DOKUMENTTITIEDOT, MUUTOSHISTORIA JA PROJEKTITIEDOT
%\newcommand{\projdoc@docinfo}{\chapter*{\docinfoname}}
%   \addcontentsline{toc}{chapter}{\docinfoname}}
%\newcommand{\projectinfo}{\chapter*{\projectinfoname}\projdoc@fillprojinfo}
%   \addcontentsline{toc}{chapter}{\projectinfoname}}
\newcommand{\versionhistory}{\chapter*{\versionhistoryname}}
%   \addcontentsline{toc}{chapter}{\versionhistoryname}}

\renewcommand\appendix{\par
  \addtocontents{toc}{\protect\contentsline{chapter}{\appendicesname}{}{}}
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@Alph\c@chapter}}

%% redefines chapter and chapter* of report-class:

% commnent out the change to plain pagestyle
\renewcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
                    %\thispagestyle{plain}
                    \global\@topnum\z@
                    \@afterindentfalse
                    \secdef\@chapter\@schapter}

% chapter outlook
\def\@makechapterhead#1{%
  %\vspace*{1em}%
  {\parindent \z@ \raggedright \normalfont
    \Large \bfseries 
    \ifnum \c@secnumdepth >\m@ne
      \thechapter\hskip2ex
    \fi
    #1\par\nobreak
    \vskip 1em
  }}

\def\@makeschapterhead#1{%
  %\vspace*{1em}%
  {\parindent \z@ \raggedright
    \normalfont
    \interlinepenalty\@M
    \Large \bfseries  #1\par\nobreak
    \vskip 1em
  }}

%% end of chapter re-definitions

\renewcommand\section{\@startsection%
  {section}%
  {1}%
  {\z@}%
  {-2em}%
  {1em}%
  {\normalfont\bfseries\large}%
}

\renewcommand\subsection{\@startsection%
  {subsection}%
  {2}%
  {\z@}%
  {-\baselineskip}%
  {\ifaltsubsec
    -2ex
   \else
     2ex
   \fi}% aligns the first line of the following paragraph with the title:
         % bug or feature?
  %{2ex}%
  {\normalfont\bfseries\normalsize}%
}

\renewcommand\subsubsection{\ClassError{projdoc}{Don't use sectioning below subsection}{}}
\renewcommand\paragraph{\ClassError{projdoc}{Don't use sectioning below subsection}{}}
\renewcommand\subparagraph{\ClassError{projdoc}{Don't use sectioning below subsection}{}}

\setcounter{secnumdepth}{2}

\let\projdoc@thebibliography\thebibliography
\def\thebibliography#1{%
  \def\refname{Sources}%
  \def\bibname{Sources}%
  \projdoc@thebibliography{#1}%
}
