#!/usr/bin/env bash
# yam2beamer - convert yam output to latex beamer format

# previous:
# - yam2html seds out the embedded script in the talk and runs it
# - html2slidy takes the .html and creates a -slidy.html
# - any necessary munging is also done by the script from the talk
#
# new:
# - as above but via latex and targeting beamer

# parameters
P=`basename $0`				
USAGE="${P} [-h(elp)] -t 'Talk title' [-i header-image] [-w image-width]
  file.tex (convert yam latex output to beamer format)"
OPTIONSTRING=ht:i:w:
TITLE=
IMAGEWIDTH=100

# message & exit if exit num present
usage() { echo Usage: $USAGE; [ ! -z "$1" ] && exit $1; }

# process options
while getopts $OPTIONSTRING OPTION
do
  case $OPTION in
    h)	usage 0 ;;
    t)  TITLE="${OPTARG}" ;;
    i)  IMAGE="${OPTARG}" ;;
    w)  IMAGEWIDTH="${OPTARG}" ;;
    *)	usage 1 ;;
  esac   
done 
shift `expr $OPTIND - 1`
case $# in
  1)
    INPUT=$1
    OUTPUT=`echo $1 |sed 's,\.tex,-beamer.tex,'`
    ;;
  *) usage 2 ;;
esac
[ -z "$TITLE" ] && { echo no title; usage 3; }
case $INPUT in
  *.tex) ;;
  *) echo "input must be a .tex"; usage 4; ;;
esac
[ -f $INPUT ] || { echo cannot open $INPUT; usage 5; }

# dates etc.
DATE=`date '+%A %d %B %Y' |sed 's,:,,g'`
YEAR=`date '+%Y'`
MYNAME=`grep $USER /etc/passwd |sed -e 's,.*[0-9]:,,' -e 's,\,.*$,,'`
SUBTITLE=${MYNAME}

# the header
HEAD=${P}-head.tex
cat << EOF | sed \
  -e "s,__TITLE__,${TITLE}," \
  -e "s,__SUBTITLE__,${SUBTITLE}," \
  -e "s,__DATE__,${DATE}," \
  -e "s,__YEAR__,${YEAR}," \
  > ${HEAD}
%% LaTeX Beamer presentation template (requires beamer package)
%% see http://latex-beamer.sourceforge.net/
\documentclass[c]{beamer}

\mode<presentation>

\newif\ifsolutions

% Swap this to solutionstrue to include solution slides for the hands-on
% exercises
\solutionstrue
%\solutionsfalse

% Set up an \omitfromhandout command to specify slides that should be omitted
% when generating the handout.
\makeatletter
\newwrite\tf@ofh
\immediate\openout\tf@ofh\jobname.ofh
\makeatother

\newcommand{\omitfromhandout}{\addtocontents{ofh}{\thepage}}

\renewcommand<>{\omitfromhandout}[0]{{\only#1{\beameroriginal{\omitfromhandout}}}}

%\ Make sure the GATE theme is used
\usetheme{GATE}
\input{slides-preamble}
\setbeamerfont{alerted text}{series=\bfseries}

\title{__TITLE__}
\subtitle{__SUBTITLE__}

% - Use the \inst{?} command only if the authors have different
%   affiliation.
%\author{F.~Author\inst{1} \and S.~Another\inst{2}}
%\author{\inst{1}}

% - Use the \inst command only if there are several affiliations.
% - Keep it simple, no one is interested in your street address.
%\institute{University of Sheffield}

% This is only inserted into the PDF information catalog. Can be left
% out.
\subject{__TITLE__}

% If you have a file called "university-logo-filename.xxx", where xxx
% is a graphic format that can be processed by latex or pdflatex,
% resp., then you can add a logo as follows:

%\pgfdeclareimage[height=2cm]{university-logo}{../../images/logo.png}
%\logo{\pgfuseimage{university-logo}}

% Delete this, if you do not want the table of contents to pop up at
% the beginning of each subsection:
\AtBeginSubsection[]
{
\begin{frame}<beamer>
\frametitle{Outline}
\tableofcontents[currentsection, currentsubsection]
\end{frame}
}

% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command:
%\beamerdefaultoverlayspecification{<+->}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% GATE preamble stuff
\newcommand{\delete}[1]{}

\begin{document}

\begin{frame}
\omitfromhandout
\titlepage
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Outline}
\tableofcontents
\end{frame}
EOF

# the preamble
PREAMBLE=slides-preamble.tex
cat << EOF | sed \
  -e "s,__TITLE__,${TITLE}," \
  -e "s,__DATE__,${DATE}," \
  -e "s,__YEAR__,${YEAR}," \
  > ${PREAMBLE}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{array}

% font definitions, try \usepackage{ae} instead of the following
% three lines if you don't like this look
\usepackage{mathptmx}
\usepackage[scaled=.90]{helvet}
\usepackage{courier}
 
\usepackage[T1]{fontenc}
\usepackage{graphicx}

% code listings
\usepackage{listings}
\lstset{
  language=Java,
  basicstyle=\footnotesize\ttfamily,
  keywordstyle=\color{blue}\bfseries,
  commentstyle=\color[rgb]{0,0.5,0}\it,
  stringstyle=\color[rgb]{1,0,1},
  showstringspaces=false,
  numbers=none,
  numbers=left,   
  numberstyle=\tiny, 
% stepnumber=1,
  numbersep=5pt,
  xleftmargin=10pt,
% frame=L,
% frameround=ffff,
% framesep=6pt,
% framexleftmargin=5mm,
% rulesepcolor=\color[rgb]{0.8,0.8,0.8},
  captionpos=b,
  backgroundcolor=\color[RGB]{230,230,240}
}

% define a larger style, used for e.g. inline listsings
\lstdefinestyle{large}{basicstyle = \normalsize\ttfamily\bfseries}

% common title page bits
\date{%
% __TITLE__\\\\%
  __DATE__\\\\%
  \vspace{2em}%
  {\scriptsize \copyright{} __YEAR__ The University of Sheffield\\\\%
    This material is licenced under the Creative Commons \\\\%
      Attribution-NonCommercial-ShareAlike Licence \\\\%
      (\texttt{\tiny http://creativecommons.org/licenses/by-nc-sa/3.0/})}%
}
EOF

# the sty files
STY1=beamerthemeGATE.sty
cat << EOF > ${STY1}
\mode<presentation>

\useoutertheme{split}
\useinnertheme{rectangles}
\usecolortheme{GATE}

\setbeamercolor*{titlelike}{parent=structure}

% make frame numbers optional (but enabled by default)
\newif\ifframenumbers
\framenumberstrue

% Change the footline to also include the frame number if enabled
\expandafter\def\expandafter\insertshorttitle\expandafter{%
  \insertshorttitle \ifframenumbers\hfill \insertframenumber\,/\,\inserttotalframenumber\fi%
}

\mode
<all>
EOF
STY2=beamercolorthemeGATE.sty
cat << EOF > ${STY2}
\mode<presentation>

\definecolor{gate-light-red}{RGB}{255,0,0}
\definecolor{gate-dark-red}{RGB}{140,0,0}
\definecolor{gate-light-green}{RGB}{0,155,0}
\definecolor{gate-dark-green}{RGB}{6,142,6}

\setbeamercolor{section in toc}{fg=black,bg=white}
\setbeamercolor{alerted text}{fg=gate-dark-red!80!gray}
\setbeamercolor*{palette primary}{fg=gate-dark-red!60!black,bg=gray!30!white}
\setbeamercolor*{palette secondary}{fg=gate-dark-red!70!black,bg=gray!15!white}
\setbeamercolor*{palette tertiary}{bg=gate-dark-red!80!black,fg=gray!10!white}
\setbeamercolor*{palette quaternary}{fg=gate-dark-red,bg=gray!5!white}

\setbeamercolor*{sidebar}{fg=gate-dark-red,bg=gray!15!white}

\setbeamercolor*{palette sidebar primary}{fg=gate-dark-red!10!black}
\setbeamercolor*{palette sidebar secondary}{fg=white}
\setbeamercolor*{palette sidebar tertiary}{fg=gate-dark-red!50!black}
\setbeamercolor*{palette sidebar quaternary}{fg=gray!10!white}

\setbeamercolor{title}{fg=gate-dark-red}
\setbeamercolor{subtitle}{fg=gate-light-red}
\setbeamercolor{titlelike}{parent=palette primary,fg=gate-dark-red}
\setbeamercolor{frametitle}{bg=gray!10!white, fg=gate-dark-red}
\setbeamercolor{frametitle right}{bg=gray!60!white}

\setbeamercolor*{separation line}{}
\setbeamercolor*{fine separation line}{}

\setbeamercolor{item}{fg=gate-dark-red}
\setbeamercolor{subitem}{fg=gate-dark-green}
\setbeamercolor{subsubitem}{fg=gate-dark-red}

\setbeamercolor{separation line}{fg=gate-dark-red}

\mode
<all>
EOF

# slide divisions
DIV='\\hrulefill'
MIDDLEDIV="${DIV}"
LASTDIV='\\end{document}'

# vars = 
# INPUT         the input file
# OUTPUT        the output file
# TITLE         title of the slideshow
# HEAD          the file containing the slides file header
# PREAMBLE      the file containing the preamble for input
# STY1          the file containing the style file
# STY2          the file containing the color style file
# DIV           division start
# MIDDLEDIV     middle division start
# LASTDIV       last division end
# IMAGE         header image
# IMAGEWIDTH    header image width

# debugging
#echo in: $INPUT out: $OUTPUT title: $TITLE head: $HEAD
#set -x

# seed the output
sed \
    -e "s|___XXX___|XXX|" \
  $HEAD >$OUTPUT

# the rest of the slides
sed -n '/\\maketitle/,$p' $INPUT | sed -n '2,$p' \
  | sed -e '0,/\\section/{s/\\section\*/XXX__FIRSTSEC__XXX/}' \
  | sed -e 's,\\hrulefill,,' \
      -e 's,\\section\*{\([^}]*\)},\\end{frame}\n\\begin{frame}\[fragile]{\1},' \
      -e 's,\(\\let\\oldThebib.*\)$,\\end{frame}\n\1,' \
  | sed -e 's,XXX__FIRSTSEC__XXX{\([^}]*\)},\\begin{frame}\[fragile]{\1},' \
  >>$OUTPUT

# clean up
#rm $HEAD $PREAMBLE $STY1 $STY2
