% This should be compiled with plain TeX
\vsize = 25cm
\magnification=\magstephalf
\parskip=6pt plus 1pt minus 1pt
\overfullrule=0pt

\newif\ifsecondpass
\newread\std
\openin\std \jobname.std
\ifeof\std
  \secondpassfalse
   \message{FIRST PASS}
\else
  \secondpasstrue
   \message{SECOND PASS}
\fi

\newcount\chapno
\newcount\secno
\newcount\subsecno
\newcount\subsubsecno

\ifsecondpass
 \let\immediate\relax
 \def\write#1#2{}
\else
  \csname newwrite\endcsname\toc
  \csname newwrite\endcsname\index

  \openout\toc=\jobname.toc
  \openout\index=\jobname.idx
\fi
\chapno=0

\def\sref#1{{\write\index{!#1!\the\pageno!}}}
\def\ref#1{\sref{#1}#1}
\def\hwrite#1#2{{\let\the=0\edef\next{\write#1{#2}}\next}} %cf TeXBook Exercice 21.10

\def\title#1{\ifodd\pageno\else\hbox{}\vfill\eject\fi
             \hwrite\toc{#1\string\dotfill\the\pageno}
             \centerline{#1}\vskip1cm}
\def\sectitle#1{\hwrite\toc{\hskip0.5cm#1\string\dotfill\the\pageno}
             \medskip\medskip\noindent{\bf{#1}}\medskip\nobreak}
\def\subsectitle#1{\hwrite\toc{{\sevenrm\hskip1cm#1\string\dotfill\the\pageno}}
             \smallskip{\bf{#1}}\nobreak}
\def\subsubsectitle#1{\hwrite\toc{{\sevenrm\hskip1.5cm#1\string\dotfill\the\pageno}}
             \smallskip{\bf{#1}}\nobreak}

\def\chapter#1{
    \advance\chapno by1\secno=0
    \title{Chapter \number\chapno: #1}}
\def\section#1{
    \advance\secno by1\subsecno=0
    \sectitle{\number\chapno.\number\secno\ #1}}
\def\subsec#1{
    \advance\subsecno by 1\subsubsecno=0
    \subsectitle{\number\chapno.\number\secno.\number\subsecno\ #1}}
\def\subsubsec#1{
    \advance\subsubsecno by1
    \subsubsectitle{\number\chapno.\number\secno.\number
    \subsecno.\number\subsubsecno\ #1}}

\def\subsecref#1{\sref{#1}\subsec{#1}}
\def\subsubsecref#1{\sref{#1}\subsubsec{#1}}

\def\bs{\char'134}
\def\quo{\char'042}
\def\obr{\char'173}
\def\cbr{\char'175}
\def\Bbb#1{{\bf #1}}
\def\curl{\cal}
\def\mod{\,{\rm mod}\,}
\def\varprojlim{{\displaystyle\lim_{\textstyle\longleftarrow}}}
\def\text#1{{\rm#1}}
\def\teb#1{{\bf\ref{#1}}}
\def\tec#1#2{{\bf\ref{#1}}{\tt #2}}
\def\dfrac#1#2{{{#1}\over{#2}}}
\def\binom#1#2{\pmatrix{{#1}\cr{#2}}}
\def\a{{\bf a}}
\def\Z{\Bbb Z}
\def\Q{\Bbb Q}
\def\F{\Bbb F}
\def\R{\Bbb R}
\def\C{\Bbb C}
\def\vers{1.39.03{}}
{
\nopagenumbers
\input titlepage
}

\pageno=-1
\def\dotfill{\leaders\hbox to 3truemm{\hfil.\hfil}\hfill}
{\def\write#1#2{}\title {Table of Contents}}

\ifsecondpass
  \begingroup
  \parskip=0pt plus 1pt
  \parindent=0pt
  \obeylines
  \input\jobname.toc
  \endgroup
\fi

\vfil\eject

\pageno=1

\input usersch1
\input usersch2
\input usersch3
\input usersch4
\input usersch5
\input tutorial
\input appa
\input appb
\input appc
\input appd

\write\index{The End}

\ifsecondpass\else
  \immediate\write\toc{Index\string\dotfill\the\pageno}
  \expandafter\end
\fi


\vfill\eject

\newdimen\fullhsize
\fullhsize=\hsize
\advance\hsize by -20pt
\divide\hsize by 2

\def\fullline{\hbox to\fullhsize}
\let\lr=L\newbox\leftcolumn

\headline={\hfil Index\hfil\global\headline={\hfil}}

\def\makeheadline{\vbox to 0pt{\vskip-22.5pt
    \fullline{\vbox to8.5pt{}\the\headline}\vss}
    \nointerlineskip}

\def\makefootline{\baselineskip=24pt\fullline{\the\footline}}


\output={\if L\lr   %pique dans le TeXbook, p257
    \global\setbox\leftcolumn=\columnbox\global\let\lr=R
    \else\doubleformat\global\let\lr=L\fi
    \ifnum\outputpenalty>-20000\else\dosupereject\fi}
\def\doubleformat{\shipout\vbox
    {\makeheadline
    \fullline{\box\leftcolumn\hfil\columnbox}
    \makefootline}
    \advancepageno}
\def\columnbox{\leftline{\pagebody}}
\def\parse !#1#2!#3! {%
     \uppercase{\def\theletter{#1}}\def\theword{#1#2}\def\thepage{#3}}
\def\theoldword{}
\def\theend{The End }
\newif\ifencore

\parskip=0pt plus 1pt
\parindent=0pt



\def\dotfill{\leaders\hbox to 3truemm{\hfil.\hfil}\hfill}
\loop
  \read\std to\theline
  \ifx\theline\theend\encorefalse\else\encoretrue\fi
\ifencore
  \expandafter\parse\theline
  \ifx\theletter\theoldletter\else
    \endgraf\vskip 10pt plus 10pt\centerline{\theletter}\vskip 10pt plus 10pt\fi
  \let\theoldletter\theletter
  \ifx\theword\theoldword
    \ifx\thepage\theoldpage\else, \thepage\fi
  \else
    \relax\endgraf\theword\dotfill\thepage\let\theoldword=\theword
  \fi
  \let\theoldpage=\thepage
\repeat


\end

