#!/bin/sh
# script to generate the MPlayer HTML documentation from the XML sources,
# convert the manual page to text and HTML versions
# and generate the codecs status table

export TMPDIR=`pwd`

TEMPDIR=$(mktemp -td $(basename $0).XXXXXX)
DOCSDIR=$TEMPDIR/DOCS
DSTDIR=`pwd`/MPlayerDOCSnew

REPO=svn://svn.mplayerhq.hu/mplayer/trunk

cleanup(){
    rm -rf $TEMPDIR
    exit $1
}

cd $TEMPDIR || cleanup 1

svn --force export $REPO .

# Skip getting a git clone of FFmpeg.
mkdir -p ffmpeg/libavcodec || cleanup 1
mkdir -p ffmpeg/libavutil/x86 || cleanup 1
touch ffmpeg/libavutil/x86/asm.h

./configure --language=all --yasm='' || cleanup 1

# Do not generate the program automatically, it is a security risk.
# Instead, use a version that we compiled ourselves manually.
make codecs2html
./codecs2html || cleanup 1
#codecs2html > /dev/null 2>&1 || cleanup 1
mkdir -p $DSTDIR || cleanup 1
mv $DOCSDIR/codecs-status.html $DSTDIR

nice -10 make doc || cleanup 1

rm -rf $DSTDIR/HTML
mv $DOCSDIR/HTML $DSTDIR

rm -rf $DSTDIR/tech
mv $DOCSDIR/tech $DSTDIR


# groff does not set the encoding in generated HTML ...
cat <<EOF > $TEMPDIR/encoding.sed
6i\\
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
EOF

fixencoding() {
    cd $DOCSDIR/man/$1
    iconv -f $2 -t UTF-8 < mplayer.1 > mplayer.1.utf8
    mv mplayer.1.utf8 mplayer.1
}

# Convert man pages with legacy encodings
fixencoding ru KOI8-R

fixencoding hu ISO8859-2
fixencoding pl ISO8859-2

for lang in en de es fr it; do
    fixencoding $lang ISO8859-1
done

cd $DOCSDIR/man/
for lang in $DOCSDIR/man/*; do
    cd $lang
    groff -m man -Thtml mplayer.1 > mplayer.1.html || cleanup 1
    groff -m man -Tutf8 -rLL=78n mplayer.1 | col -bxp > mplayer.1.txt
    sed -i -f $TEMPDIR/encoding.sed mplayer.1.html
    cd ..
done

rm -rf $DSTDIR/man
mv $DOCSDIR/man/ $DSTDIR

rm -f $DSTDIR/README
mv $TEMPDIR/README $DSTDIR

cleanup 0
