%  flipbook.sty --- style file for flip books
%% Copyright 2011 Olivier Buffet
%
% This material is subject to the LaTeX Project Public License. See http://www.ctan.org/tex-archive/help/Catalogue/licenses.lppl.html for the details of that license.
%
% This program consists of the files flipbook.sty and documentation.tex (and some less important files).
%  
%  Report bugs and comments to:
%  buffet-AT-AT-loria-DOT-DOT-fr
%
%  $Id: flipbook.sty,v 0.1 2011/12/05 16:26:53 buffet Exp $
%
%  PACKAGES REQUIRED:
%
%  - fancyhdr
%  - graphicx
%  - scalefnt
%  - verbatim
%  - everypage
%  - ifthen

\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{flipbook}[2011/12/05 Standard LaTeX class]

% The type of headings that allows for customization:
\RequirePackage{fancyhdr}
\RequirePackage{graphicx}
\RequirePackage{scalefnt}
\RequirePackage{verbatim}
\RequirePackage{everypage}
\RequirePackage{ifthen}
\pagestyle{fancyplain}

\newcommand{\fbDebug}[1]{}
\DeclareOption{debug}{%
  \renewcommand{\fbDebug}[1]{{\tiny #1} \@latex@warning{#1}}
}
\ProcessOptions

\newcounter{fbpage}
\setcounter{fbpage}{0}
\AddEverypageHook{
  \addtocounter{fbpage}{1}
}

% How to get the total number of pages. Inspired from the files
% lastpage.sty and beamerbasemisc.sty .

\newcounter{numpages}

\AtEndDocument{%
   \clearpage \setcounter{numpages}{\thefbpage}
   \if@filesw
   \immediate\write\@auxout{\noexpand\setcounter{numpages}{\the\c@page}}
   \fi
}%

% The important command (but which can probably be improved):
% 3 parameters:
% - prefix of your images
% - suffix (extension) of your images
% - displaying options of your images
\makeatletter
\newcommand\fbImageF[3]{ % flip-book \includegraphics{...} forward
  \@tempcnta 0
  \advance\@tempcnta+\thefbpage
  \if@twoside
  \divide\@tempcnta2
  \fi
  \IfFileExists{#1\number\@tempcnta .#2}
               {
                 \includegraphics[#3]{#1\number\@tempcnta .#2}
               }{
                 \fbDebug{``#1\number\@tempcnta .#2'' NOT FOUND}
               }
}
\newcommand\fbImageB[3]{ % flip-book \includegraphics{...} backward
  \@tempcnta\value{numpages}
  \advance\@tempcnta-\thefbpage
  \if@twoside
  \divide\@tempcnta2
  \fi
  \IfFileExists{#1\number\@tempcnta .#2}
               {
                 \includegraphics[#3]{#1\number\@tempcnta .#2}
               }{
                 \fbDebug{``#1\number\@tempcnta .#2'' NOT FOUND}
               }
}
\makeatother

\makeatletter
\newcommand\fbInputF[3]{ % flip-book \input{...} forward
  \@tempcnta 0
  \advance\@tempcnta+\thefbpage
  \if@twoside
  \divide\@tempcnta2
  \fi
  \IfFileExists{#1\number\@tempcnta .#2}
               {
                 \scalefont{#3}
                 \input{#1\number\@tempcnta .#2}
               }{
                 \fbDebug{``#1\number\@tempcnta .#2'' NOT FOUND}
               }
}
\newcommand\fbInputB[3]{ % flip-book \input{...} backward
  \@tempcnta\value{numpages}
  \advance\@tempcnta-\thefbpage
  \if@twoside
  \divide\@tempcnta2
  \fi
  \IfFileExists{#1\number\@tempcnta .#2}
               {
                 \scalefont{#3}
                 \input{#1\number\@tempcnta .#2}
               }{
                 \fbDebug{``#1\number\@tempcnta .#2'' NOT FOUND}
               }
}
\makeatother

\makeatletter
\newcommand\fbVerbF[3]{ % flip-book \verbatiminput{...} forward
  \@tempcnta 0
  \advance\@tempcnta+\thefbpage
  \if@twoside
  \divide\@tempcnta2
  \fi
  \IfFileExists{#1\number\@tempcnta .#2}
               {
                 \scalefont{#3}
                 \verbatiminput{#1\number\@tempcnta .#2}
               }{
                 \fbDebug{``#1\number\@tempcnta .#2'' NOT FOUND}
               }
}
\newcommand\fbVerbB[3]{ % flip-book \verbatiminput{...} backward
  \@tempcnta\value{numpages}
  \advance\@tempcnta-\thefbpage
  \if@twoside
  \divide\@tempcnta2
  \fi
  \IfFileExists{#1\number\@tempcnta .#2}
               {
                 \scalefont{#3}
                 \verbatiminput{#1\number\@tempcnta .#2}
               }{
                 \fbDebug{``#1\number\@tempcnta .#2'' NOT FOUND}
               }
}
\makeatother

% Fancyhdr moves the center of pages.
% To compensate this I have found the following piece of code.
% But there must be a better solution.
\makeatletter
\@tempdima=\paperwidth
\advance\@tempdima by -\textwidth
\divide\@tempdima by 2
\advance\@tempdima by -1in
\oddsidemargin=\@tempdima
\let\evensidemargin=\oddsidemargin
\makeatother