%%
%% Default dblatex DocBook style
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{docbook}[2007/04/04 DocBook Style]

% The defined options
\DeclareOption{hyperlink}{ \def\DBKhyperlink{yes} }
\DeclareOption{nohyperlink}{ \def\DBKhyperlink{no} }

% Default values
\ExecuteOptions{nohyperlink}

% If defined, process the options
\ProcessOptions\relax

% ##############################################################################
% ### DocBook and local commands
% ##############################################################################
% Default DocBook Parameters
\newcommand{\DBKreference}{}
\newcommand{\DBKtitle}{Main Title}
\let\DBKsubtitle\relax
\newcommand{\DBKdate}{\today}
\newcommand{\DBKsite}{}
\newcommand{\DBKedition}{}
\newcommand{\DBKreleaseinfo}{Working Paper}
\newcommand{\DBKpubdate}{}
\let\DBKlegalblock\relax
\newcommand{\DBKlegaltitle}{}
\let\DBKcopyright\relax

% Local commands
\newcommand{\DBKtoclof}{true}
\newcommand{\DBKauthor}{}

% Adaptation...
\setcounter{secnumdepth}{4}
\setcounter{tocdepth}{4}

\setlength\parskip{\medskipamount}
\setlength\parindent{0pt}

% Don't want paragraph indentation in abstract
\@ifundefined{abstract}{
\newenvironment{abstract}{
  \setlength\parskip{\medskipamount}%
  \setlength\parindent{0pt}%
}{
}
}{
% Redefine abstract in avoid page count reset
\def\nocount#1#2{}
\let\stdsetcounter\setcounter
\let\stdabstract=\abstract
\let\endstdabstract=\endabstract
\renewenvironment{abstract}{%
  \let\setcounter\nocount%
  \begin{stdabstract}%
  \noindent%
  \setlength\parskip{\medskipamount}%
  \setlength\parindent{0pt}%
}{
  \end{stdabstract}
  \let\setcounter\stdsetcounter
}
}

% We want paragraph + subparagraph headers like the others
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {1ex \@plus .2ex}%
                                     {\normalfont\normalsize\bfseries}}

\renewcommand\subparagraph{\@startsection{subparagraph}{5}{\z@}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {1ex \@plus .2ex}%
                                     {\normalfont\normalsize\bfseries}}

% To have title rendering changed
\usepackage[sf,bf]{titlesec}


% Display ``DRAFT'' as a water mark:
\usepackage{ifpdf}

\newif\ifpdfengine
\ifpdf\pdfenginetrue
\else\ifxetex\pdfenginetrue
\fi\fi

\ifpdfengine
\usepackage{pdfpages}
\usepackage{color}
\def\showwatermark#1{%
  \AddToShipoutPicture{
  \AtTextCenter{%
  \makebox(0,0)[c]{\resizebox{\textwidth}{!}{%
  \rotatebox{45}{\textsf{\textbf{\color[gray]{0.8}#1}}}}}%
  }}
}
\else
\def\showwatermark#1{}%
\fi

% ##############################################################################
% ### Page Parameters if not done by geometry
% ##############################################################################
\@ifpackageloaded{geometry}{}{
\setlength{\paperwidth}{21cm}
\setlength{\paperheight}{29.7cm}
\setlength{\hoffset}{-1in}
\setlength{\voffset}{-1in}
\setlength{\oddsidemargin}{2cm}
\setlength{\marginparsep}{0cm}
\setlength{\marginparwidth}{0cm}
\setlength{\textwidth}{18cm}
%\setlength{\topmargin}{0.5cm}
\setlength{\topmargin}{1.3cm}
%\setlength{\headsep}{2cm}
\setlength{\headsep}{1.1cm}
\setlength{\textheight}{24cm}
\setlength{\footskip}{1.5cm}
}

% ##############################################################################
% ### Used Packages
% ##############################################################################
\makeatletter
% Postscript Graphics
\usepackage{graphicx}

% Miscellaneous
\usepackage{calc}
\usepackage{multirow}
\usepackage{subfigure}
\usepackage{ifthen}
\usepackage{tabularx}
% Before hyperlink to have consistent links to footnotes
\usepackage{footmisc}
% To be able to have sections afters appendices
\usepackage{appendix}

% Workaround to avoid troubles with accents in \appendixname used as hyperref
% label: force the label prefix to 'appendix' whatever the lang is
\let\orgresetsapp=\@resets@pp
\let\orgrestoreapp=\restoreapp
\let\restoreapp\relax
\def\@resets@pp{%
  \orgresetsapp
  \if@pphyper
    \def\Hy@chapapp{appendix}%
  \fi
  \orgrestoreapp
}

\usepackage{dbk_fonts}
\usepackage{dbk_hyper}

\usepackage{fancyhdr}

% To have fancy <programlisting>
\usepackage{fancyvrb}

% ##############################################################################
% ### Some DocBook floats and environments
% ##############################################################################

%% Inclusion of float package after hyperref to have its definitions!
\usepackage{float}

\@ifundefined{chapter}{

\floatstyle{ruled}
\newfloat{example}{htb}{loe}[section]
\floatname{example}{\examplename}

\floatstyle{plain}
\newfloat{dbequation}{htb}{lom}[section]
\floatname{dbequation}{{\scshape \dbequationname }}

% Redefinition of tableofcontent, to have it in separate pages
\let\stdtoc=\tableofcontents
\def\tableofcontents{ \newpage \stdtoc{} \newpage} 
}{

\floatstyle{ruled}
\newfloat{example}{htb}{loe}[chapter]
\floatname{example}{\examplename}

\floatstyle{plain}
\newfloat{dbequation}{htb}{lom}[chapter]
\floatname{dbequation}{{\scshape \dbequationname }}
}

% Sidebar is a boxed minipage that can contain verbatim
\newenvironment{sidebar}[1][]{
  \hspace{0mm}\newline%
  \noindent\begin{Sbox}\begin{minipage}{0.95\linewidth}%
  \setlength\parskip{\medskipamount}%
}{
  \end{minipage}\end{Sbox}\shadowbox{\TheSbox}%
}


% ##############################################################################
% ### Title and Bookinfo display
% ##############################################################################
\def\title#1{\renewcommand{\DBKtitle}{#1}}

% Redefine author so that it can be used by the domitete
\def\author#1{\renewcommand{\DBKauthor}{#1}}

% Redefine how to print the cover page and book infos
\def\maketitle{
  \DBKcover
%  \if\DBKcopyright\@empty\else
  \ifx\DBKcopyright\relax\else
    \DBKcopyright \\
  \fi
  \ifx\DBKlegalblock\relax\else
    \DBKlegalblock
  \fi
  \newpage
  \DBKdomitete
}

% How to render a <legalnotice>
\newenvironment{DBKlegalnotice}{
  \if@twocolumn
    \noindent\small\textit{\DBKlegaltitle}
    \/\bfseries---$\!$
  \else
    \ifx\DBKlegaltitle\relax\else
      \noindent\begin{center}\small\bfseries\DBKlegaltitle\end{center}%
    \fi
    \begin{quote}\small
  \fi
}{
  \vspace{0.6em}\par\if@twocolumn\else\end{quote}\fi
}

% ##############################################################################
% ### Index redefinition
% ##############################################################################

%
% Index is redefined to have the section numbered.
% 
\renewenvironment{theindex}
{ 
  \if@twocolumn
    \@restonecolfalse
  \else
    \@restonecoltrue
  \fi
  \columnseprule \z@
  \columnsep 35\p@
  \pagebreak
  \@ifundefined{chapter}{
    \twocolumn[\section{\indexname}]%
  }{
    % To not have the number, replace makechapterhead by
    % makeschapterhead
    % \chapter{\indexname}{%
    % \@mkboth{\MakeUppercase\indexname}%
    %        {\MakeUppercase\indexname}
    % }%
    \refstepcounter{chapter}
    \twocolumn[\@makechapterhead{\indexname}]%
  }
  \thispagestyle{plain}\parindent\z@
  \parskip\z@ \@plus .3\p@\relax
  \let\item\@idxitem
}{\if@restonecol\onecolumn\else\clearpage\fi}

\let\stdindex=\theindex
\let\endstdindex=\endtheindex
\renewenvironment{theindex}{
  \begin{stdindex}
  \@ifundefined{chapter}{}{
    % To not have the number, replace by:
    % \addcontentsline{toc}{chapter}{\numberline{}\indexname}
    \ifnum \c@secnumdepth >\z@
      \ifnum \value{part} >\z@
        \addcontentsline{toc}{part}{\numberline{\thechapter}\hspace{-1em}\indexname}
      \else
        \addcontentsline{toc}{chapter}{\numberline{\thechapter}\indexname}
      \fi
    \else
      \addcontentsline{toc}{chapter}{\indexname}
    \fi
  }
}{
  \end{stdindex}
}


% ##############################################################################
% ### Commands and environments
% ##############################################################################
\newcommand{\draft}{\special{!userdict begin /bop-hook{gsave 200 30 translate %
         65 rotate /Times-Roman findfont 216 scalefont setfont %
         0 0 moveto 0.8 setgray (DRAFT) show grestore}def end}}

\usepackage{refcount}
\usepackage{lastpage}

%\usepackage{dbk_url}
\usepackage{dbk_locale}
\usepackage{dbk_title}
\usepackage{dbk_headfoot}
\usepackage{dbk_revision}
\usepackage{dbk_admonition}
\usepackage{dbk_changebar}
\usepackage{dbk_biblio}
\usepackage{dbk_table}
\usepackage{dbk_item}
\usepackage{dbk_graphic}
\usepackage{dbk_cover}
\usepackage{dbk_verbatim}
\usepackage{dbk_misc}
\usepackage{dbk_callout}
\usepackage{dbk_annot}

\makeatother

