#!/perl
####     ###     ###     DOCUMENTATION

###     STEP 1
#You're supposed to have a tex file, whose name is given to $f:
#$f=mytexfile.tex
$f="/Users/account/folder/yourfile.tex";
###     STEP 2
#In this file, just write 2 commentaries:
#C1
# %END PRELUDE, after the description of packages, etc.
# which are useful for tex files but not for beamer files
# C2
# %memoencoding: MyEncoding to precise which encoding to choose for beamer
# eg: %memoencoding: mac

###     STEP 3
# Make 2 choices for the following parameters 
# $fontsize= ??? #small font for each slide? Medium? May be 12, 15...
$fontsize=10; 
# $pausewanted=??? 1 if you want a pause between 2 items; 0 if not
$pausewanted=1; 

###     STEP 4
#Build your slides: pdflatex mytexfile.beam.tex
# Make modifications if necessary
# ENJOY !
####     ###     ###     END OF DOCUMENTATION

$g= $f;
$g=~s/tex$/beam.tex/g;

$beam="\\documentclass[$fontsize"."pt]{beamer}
\\usepackage[ZZZZZZ]{inputenc}
\\usepackage{hyperref,url}
\\usepackage{color}
\\usetheme{Warsaw}
";
$firstframe="
\\begin{frame}
\\titlepage
\\end{frame}
";

$debutframe=0;
open (F,$f);
open (G,">$g");

while (<F>)
	{
	#chop; 
	$ok=1 if /^%END PRELUDE/;
	$code=$1 ;
	if (/^%memoencoding: *(.*)/)
		{
		$code=$1; 
		$beam =~s/ZZZZZZ/$code/g;
		$_= $beam;
		}
	else
		{
		if (/(^ *\\.*subsection{)([^}]+)}/)
			{
			$_.="\\begin{frame}{$2}\n";
			$_="\\end{frame}\n\n".$_ if ($debutframe);			
			$debutframe=1;
			#print $_,"\n"; sleep 1;
			}
		if (/(^ *\\section{)([^}]+)}/ && $debutframe)
			{$_="\\end{frame}\n\n".$_;
			$debutframe=0;
			}
			
		s/\\item /\\pause \\item /g if $pausewanted;
		s/\\\maketitle/$firstframe/g;
#old		s/(\\href{([^}]+)}{([^}]+)})/{\\color{blue}$1}/g;			
		s/(\\href{[^}]+}{[^}]+})/{\\color{blue}$1}/g;	
		s/(\\url{[^}]+})/{\\color{blue}$1}/g;	

		s/\\end{document}/\\end{frame}\n\\end{document}/g;
		}
	
	print G if $ok;
	}
close (F);
close (G);
