#!/bin/bash
# 
# Select another version of a LaTeX beamer file.
# (c) Jan Bredereke, 2009, 2012, 2016, 2018, 2023
# 
# Version: 2.0
# 
# This program may be redistributed under the GPLv2.
#
# usage:
#     select-beamer-version [ prefix [filePrefix] ] < infile.tex > outfile.tex
#
# This program does some text subsitutions on the LaTeX source. The
# idea is as follows: the LaTeX beamer package allows some overlay
# mode specifications, as follows:
# - beamer (default)
# - handout
# - transparancy
# - article
# - all (all of the above)
# - presentation (the first three of the above)
# These may be followed by a colon and a number, to select pages.
# They also may be combined by "| ". If one mode is named more than
# once, the last one wins. If the name of the mode is unknown, it is
# ignored.
#
# We therefore introduce additional modes, for example:
# - wdh-beamer
# - wdh-handout
# - wdh-all
# These are ignored by the LaTeX beamer package. However, if we
# remove the prefix "wdh-" by this filter here, and if the
# additional mode specification comes last, it will determine the
# result.
# 
# Example:
#   \begin{frame}<wdh-all:0> % don't show frame in wdh
#   ...
#   \end{frame}
#   \begin{frame}<handout| wdh-beamer> % normally handout only,
#                                      % but also beamer in wdh
#   ...
#   \end{frame}
# 
# Another example:
#   ...
#   This is some text. \only<all:0| wdh-all>{More text only in Wdh.}
#   \only<all| wdh-all:0>{This text is omitted in Wdh.}
#
# The prefix (e.g., "wdh-") is a parameter, in order to allow for
# more versions. The default prefix is "wdh-".
#
# The text substitution occurs everywhere in the LaTeX source, 
# except in comments. In order to prevent surprising side effects,
# the prefix must be followed by one of the five above standard
# mode specifications. Furthermore, it must follow either "<" or
# "| ".
# 
# In order to be able to apply the same scheme to sub-files which
# are read in by \input{}, the file names of such files are modified
# by a further text substitution. Any such file name is prepended
# with the prefix. However, this applies only to file names ending
# in ".tex". If a file name contains a path (which contains
# directory names separated by one or more slashes and ends with
# another slash), then the file name with path is replaced by a
# "mangled" file name with prefix, but without any path. The
# "mangled" file name consists of the original file name prefixed by
# the last directory name in the path plus an underscore. For
# example, "../foo/bar/baz.tex" with prefix "std-" becomes
# "std-bar_baz.tex". You must take care that the named file will
# exist in the current directory, not in the directory described by
# the path.
# 
# In some special cases you might want to use a different prefix
# for file name substitition than for selecting the beamer mode.
# Then, you can specify a filePrefix as a second parameter. If
# omitted, it defaults to the prefix used for the beamer mode.

function usage() \
{
    progName=`basename $0`
    echo >&2 'usage:'
    echo >&2 "${progName} [ prefix [ filePrefix ] ] < infile.tex > outfile.tex"
    echo >&2 ''
    echo >&2 'See also the longer explanation at the start of the source code.'
    echo >&2 ''
}

case "$1" in
--help|-h) \
    usage
    exit 1
    ;;
esac

prefix='wdh-'
filePrefix="${prefix}"

if [ "$#" -ge 1 ] ; then
    prefix="$1"
    shift
fi

if [ "$#" -eq 1 ] ; then
    filePrefix="$1"
    shift
fi

if [ "$#" -ne 0 ] ; then
    usage
    exit 1
fi

sed -e "s/\(>\|\\| \)${prefix}\(beamer\|handout\|transparency\|presentation\|article\|all\)/\1\2/g" \
    -e "/^[ \t]*\\\\input{[^}]*\.tex}/s/^\([ \t]*\\\\input{\)[^}]*\/\([^/}]*\)\/\([^/}]*\.tex}.*\)$/\1\2_\3/" \
    -e "s/^\([ \t]*\\\\input{\)\([^}]*\.tex}.*\)$/\1${filePrefix}\2/"
