#!/bin/sh
[ -r $WMII_CONFDIR/rc.conf ] || exit 1
WMII_RCCONF="$WMII_CONFDIR/rc.conf"
[ -r $HOME/.wmii/rc.conf ] && WMII_RCCONF="$HOME/.wmii/rc.conf"

export WMII_RCCONF
. $WMII_RCCONF

FILE="wmii-refcard"

cat > $FILE.tex << __EOF__
\documentclass{article}
\pagestyle{empty}
\begin{document}
__EOF__


echo "\section*{wmii reference card}" >> $FILE.tex
echo "Version: \small{\texttt{`wmii -v|sed 's/wmiiwm - //;s/(C).*$//'`}}" >> $FILE.tex

cat >> $FILE.tex << __EOF__
\subsection*{Actions}
\vspace{3pt}
\begin{tabular}{ll}
__EOF__

for i in `ls $WMII_CONFDIR/action`; do
    echo "\texttt{$i} & `$WMII_CONFDIR/action/$i -h|sed 's/_/\\_/g'` \\\\" >> $FILE.tex
done

cat >> $FILE.tex << __EOF__
\end{tabular}
\subsection*{Shortcuts}
__EOF__

for i in `$READ /keys/mode`; do
    echo "\subsubsection*{`echo $i|sed 's/\///g'` mode}" >> $FILE.tex
    echo "\begin{tabular}{ll}" >> $FILE.tex
    for j in `$READ /keys/mode/$i`; do
        while expr "$j" : '.*/$' >/dev/null; do
            # handle sequential shortcuts
            j="$j`$READ /keys/mode/$i/$j`"
        done
        echo "\texttt{$j} & `$READ /keys/mode/$i/$j|sed 's/wmira //;s/ .*$//g'` \\\\" >> $FILE.tex
    done
    echo "\end{tabular}" >> $FILE.tex
done

cat >> $FILE.tex << __EOF__
\subsection*{Configuration}
\small{wmii is configured through basic sh-compliant scripts.
Global wmii configuration is located in \texttt{/usr/local/etc/wmii},
within wmii the \texttt{WMII\_CONFDIR} variable points to that location.
User specific settings can be created in \texttt{HOME/.wmii}: each file
found there overrides (i.e., is read instead of) its global counterpart.}
\end{document}
__EOF__

latex $FILE.tex
xdvi $FILE.dvi
