% directories.sty
%
% Richard Hughey 10/3/90
% modify \include to work w/ subdirs of main directory.

% This file will be read in for any section.
% and contains section specific macros.
\def\defaultmacros{macros.tex}
\def\@inputifexists#1{\openin1 #1\ifeof1\relax\else\closein1\@input{#1}\fi}

% Input now checks the current directory then the normal search
% path for the file.  Note that a .tex must not be present if
% the current directory is used (this is because latex does not
% add extensions when directories are specified.
\let\@originput=\input
\def\@myinput#1{\openin1 \thedirectory#1
   \ifeof1\openin1 \thedirectory#1.tex
          \ifeof\edef\@ifile{#1}
	  \else\closein1\edef\@ifile{thedirectory#1.tex}
	  \fi
   \else\closein1\edef\@ifile{\thedirectory#1}
   \fi\@input\@ifile}
\def\pushd#1{\def\thedirectory{#1/}\let\input=\@myinput}
\def\popd{\let\input=\@originput}

% Include:  we need to place the aux file in the
% directory included.  The arg to include is now a directory, optional
% arg is a main file in that directory (default is directory.tex).
\def\include{\@ifnextchar[{\@include}{\@@include}}
\def\@@include#1{\@include[#1]{#1}}
\def\@include[#1]#2{\clearpage\pushd{#2}
\if@filesw \immediate\write\@mainaux{\string\@input{#2/#1.aux}}\fi
\@tempswatrue\if@partsw \@tempswafalse\edef\@tempb{#2}\@for
\@tempa:=\@partlist\do{\ifx\@tempa\@tempb\@tempswatrue\fi}\fi
\if@tempswa \if@filesw \let\@auxout=\@partaux
\immediate\openout\@partaux #2/#1.aux
\immediate\write\@partaux{\relax}\fi%
{\input{macros.tex}\input{#1.tex}}\clearpage
\@writeckpt{#2}\if@filesw \immediate\closeout\@partaux \fi
\let\@auxout=\@mainaux\else\@nameuse{cp@#2}\fi\popd}
% Modified from kgk to use \include w/o directory names rph
% Chapters 
\def\dirn@me{ch}
\def\filn@me{ch}

\newcount\@chcnt \@chcnt\z@
\newif\if@chapter \@chapterfalse

\def\chp@me{\dirn@me\number\@chcnt/\filn@me\number\@chcnt.tex}
\def\chd@me{\dirn@me\number\@chcnt}
\def\chf@me{\filn@me\number\@chcnt}


\def\test@ch{\advance\@chcnt by \@ne\test@@ch{\chp@me}}
\def\test@@ch#1{\openin1 #1
  \ifeof1 \@chapterfalse\else\closein1 \@chaptertrue\fi}

\def\chapters{\@chcnt\z@ \test@ch
  \if@chapter\@whilesw\if@chapter \fi{\include[\chf@me]{\chd@me}\test@ch}\fi}



