#!/bin/bash

## For each package...
for package in hepnicenames heppennames; do
    echo "Processing package $package"

    ## Make the macro list document fragment
    TMP=macrolist-$$.tmp
    > $TMP
    echo "\begin{multicols}{2}{" >> $TMP
    echo "\begin{itemize}" >> $TMP
    cat ${package}.sty | egrep "^\\\DeclareRobustCommand|^%\\\DeclareRobustCommand" | while read line; do
        item=$( echo $line | sed -e s/'^%'// -e s/"DeclareRobustCommand{\([^}]\+\)}[^%]*"/"\1"/g -e s/'%.*'//g )
        if [[ -z $item ]]; then continue; fi
        description=$( echo $line | sed -e s/'^.*DeclareRobustCommand[^%]*'//g -e s/'%%.*'//g -e s/'^%\ *'//g )
        if [[ -n $description ]]; then description="$description\newline"; fi
        echo "\item ${description} \texcmd{$(echo $item | sed -e s/'\ .*'/''/g)} \$\Rightarrow\$ \\${item}" >> $TMP
    done
    echo "\end{itemize}" >> $TMP
    echo "}\end{multicols}" >> $TMP

    ## Make the LaTeX files
    PKGOUT=${package}-macros.tex
    echo "Making $PKGOUT"
    cat $TMP > $PKGOUT

    for style in it rm; do
        PKGOUT=${package}-${style}.tex
        echo "Making $PKGOUT"
        > $PKGOUT
        if [[ "$style" = "rm" ]]; then pkgopts=""; else pkgopts="[italic]"; fi
        SECTIONCMD="\section"
        echo "\documentclass[12pt]{article}" >> $PKGOUT
        echo "\usepackage${pkgopts}{${package}}" >> $PKGOUT
        echo "\usepackage{a4wide,multicol,setspace,color}" >> $PKGOUT
        echo "\title{Testing \\textsf{${package}}}" >> $PKGOUT
        echo "\author{Generated by \texttt{$USER}}" >> $PKGOUT
        echo "\onehalfspacing" >> $PKGOUT
        echo '\newcommand{\texcmd}[1]{\textcolor{red}{\texttt{\char`\\#1}}}' >> $PKGOUT
        echo "\newcommand{\texenv}[1]{\textcolor{red}{\texttt{#1}}}" >> $PKGOUT
        echo "\newcommand{\texopt}[1]{\textcolor{purple}{\texttt{#1}}}" >> $PKGOUT
        echo "\newcommand{\texopts}[1]{\textcolor{purple}{\texttt{[#1]}}}" >> $PKGOUT
        echo "\newcommand{\texarg}[1]{\textcolor{violet}{\texttt{#1}}}" >> $PKGOUT
        echo "\newcommand{\texargs}[1]{\textcolor{violet}{\texttt{\{#1\}}}}" >> $PKGOUT
        echo "\newcommand{\texpkg}[1]{\texttt{#1}}" >> $PKGOUT
        echo "\newcommand{\texcls}[1]{\texttt{#1}}" >> $PKGOUT
        echo "\newcommand{\gen}[1]{\ensuremath{\braket{#1}}}" >> $PKGOUT
        echo >> $PKGOUT
        echo "\begin{document}" >> $PKGOUT
        echo "\maketitle" >> $PKGOUT
        echo >> $PKGOUT
        echo "$SECTIONCMD{Normal font}" >> $PKGOUT
        cat $TMP >> $PKGOUT
        echo "\clearpage" >> $PKGOUT
        echo >> $PKGOUT
        echo "$SECTIONCMD{Bold font}" >> $PKGOUT
        echo "\textbf{" >> $PKGOUT
        cat $TMP >> $PKGOUT
        echo "}" >> $PKGOUT
        echo "\clearpage" >> $PKGOUT
        echo >> $PKGOUT
        echo "$SECTIONCMD{Italic font}" >> $PKGOUT
        echo "\textit{" >> $PKGOUT
        cat $TMP >> $PKGOUT
        echo "}" >> $PKGOUT
        echo "\clearpage" >> $PKGOUT
        echo >> $PKGOUT
        echo "$SECTIONCMD{Bold italic font}" >> $PKGOUT
        echo "\textbf{\textit{" >> $PKGOUT
        cat $TMP >> $PKGOUT
        echo "}}" >> $PKGOUT
        echo >> $PKGOUT
        echo "$SECTIONCMD{Sans font}" >> $PKGOUT
        echo "\textsf{" >> $PKGOUT
        cat $TMP >> $PKGOUT
        echo "}" >> $PKGOUT
        echo "\clearpage" >> $PKGOUT
        echo >> $PKGOUT
        echo "$SECTIONCMD{Bold sans font}" >> $PKGOUT
        echo "\textsf{\textbf{" >> $PKGOUT
        cat $TMP >> $PKGOUT
        echo "}}" >> $PKGOUT
        echo "\clearpage" >> $PKGOUT
        echo >> $PKGOUT
        echo "$SECTIONCMD{Italic sans font}" >> $PKGOUT
        echo "\textsf{\textit{" >> $PKGOUT
        cat $TMP >> $PKGOUT
        echo "}}" >> $PKGOUT
        echo "\clearpage" >> $PKGOUT
        echo >> $PKGOUT
        echo "$SECTIONCMD{Bold italic sans font}" >> $PKGOUT
        echo "\textsf{\textbf{\textit{" >> $PKGOUT
        cat $TMP >> $PKGOUT
        echo "}}}" >> $PKGOUT
        echo >> $PKGOUT
        echo "\end{document}" >> $PKGOUT
    done
    rm -f $TMP

done
