\ProvidesPackage{rthmenviro}
\RequirePackage{ifthen}
\RequirePackage{calc}
\RequirePackage{amsmath}
\RequirePackage{hyperref}
\RequirePackage{verbatim}


\begin{comment}
%Ruairi's theorem environment!%
This package defines a new thm environment which allows you to create small sections, such as theorems and examples, that will appear in a list along with page number and name. It creates a new file in your working directory called rthm.tmp

usage:	\listoftheorems{<heading>}
			<heading> is the title that will be given to the contents list, this will vary depending on what you want!

	 	\begin{thm}{<type>}{<name>}
		\end{thm}
			<type> here is what will be displayed as the heading
			<name> is the name of the theorem or example (leaving this blank results in newline characters you don't expect)

		\closefile
			should be inserted at the end of your program for cleanliness. Since this package opens a file for writing to. It will still work if you don't close it but it is good practice to anyway.

known bugs:
		leaving the 3rd argument of \begin{thm} blank results in incorrect newlines in the contents

		This doesn't work if you try and put it in your texmf folder. It only works if it is in the same folder as your current project.

I will try and fix the bugs and publish an updated version sometime soon
please report any more bugs or improvement suggestions to rshort@tcd.ie
\end{comment}

\newcommand{\forloop}[5][1]%
{%
\setcounter{#2}{#3}%
\ifthenelse{#4}%
	{%
	#5%
	\addtocounter{#2}{#1}%
	\forloop[#1]{#2}{\value{#2}}{#4}{#5}%
	}%
% Else
	{%
	}%
}%

\newcounter{FileLines}
\newboolean{RestFile}
\newcommand{\FileLine}{}
\newread\File

\newcommand{\CountLinesInFile}[2]
{
  \setboolean{RestFile}{true}
  \setcounter{FileLines}{0}

  \openin\File=#1
  \whiledo{\boolean{RestFile}}
  {
    \ReadNextLine{\File}
    \ifthenelse{\boolean{RestFile}}{
      \stepcounter{FileLines}
    }{}
  }
  \closein\File
}

\newcommand{\ReadNextLine}[1]{
  \ifthenelse{\boolean{RestFile}}{
    \read#1 to \FileLine
    \ifeof#1\setboolean{RestFile}{false}
    \else % if last line already is read, EOF appears here
    \fi
  }{}
}


%\newtheorem{theorem}{Theorem}[section]


\newcounter{thmnum}

\newenvironment{thm}[2]
	{%what happens before you can write in the theorem
		\addtocounter{thmnum}{1}%
		\vspace{10pt}	 \par
		\noindent \hypertarget{#1 \arabic{section}.\arabic{thmnum}}{\textbf{#1 \arabic{section}.\arabic{thmnum} \hspace{5mm} #2 }}\\
		\immediate\write\outputstream{\string\hyperlink{#1 \arabic{section}.\arabic{thmnum}}{\string\textbf{#1 \arabic{section}.\arabic{thmnum}}} \string\hspace{3mm} #2 \string\dotfill  \arabic{page} \string\newline}
	}	
	
	{%what happens after
		\vspace{10pt} \par
	}

\newcommand{\listoftheorems}[1]
	{%	

		\vspace{10pt}
		\IfFileExists{thmlist.tot}
		{
		\CountLinesInFile{thmlist.tot}

		\ifthenelse{\value{FileLines}>1}		
		{
			%\newboolean{check}{true}
			\noindent\large\textbf{#1}\\
			\vspace{5pt}
			\normalsize
			\newcounter{counter}
			\newread\inputstream
			\immediate\openin\inputstream=thmlist.tot
			\addtocounter{FileLines}{1}
			\forloop{counter}{1}{\value{counter}<\value{FileLines}}
			{
				\immediate\read\inputstream to \auxcommand
				\auxcommand			
			}
	
			\immediate\closein\inputstream
			\newread\outputstream
		}{}
		}{}
		\newwrite\outputstream
		\immediate\openout\outputstream=thmlist.tot
	}

\AtEndDocument{\immediate\closeout\outputstream}

\let\Oldsection\section
\renewcommand{\section}{\setcounter{thmnum}{0}\Oldsection}


