#!/bin/sh
# pysol-compile.sh
#
# $Id: pysol-compile.sh 1162 2003-09-08 02:04:11Z das-cvs $
#
# Pysol is distributed as byte-compiled binaries, and since these are
# architecture-independent, this is usually sufficient.  However, these
# binaries are to some extent dependent on the version of python with which
# they were compiled, so keeping up with the latest python can be somewhat
# difficult.
#
# The python source for pysol is available, but lacks clear instructions on how
# to use it.  Making the single byte-code file from the pysol source requires
# that certain sections specific to the multi-file layout be removed, and a few
# patches are necessary to avoid dependency on the pysolsoundserver module.
# Finally, the source files need to be concatenated in an order that ensures
# that functions and classes are available before the point where they would
# have been imported.
#
# This script should be run from inside the pysol source directory, and
# will create two files: pysol_compile.py and pysol_compile.pyc
# An optimized version can be created by adding '-O' to the python invocation

if [ ! -z "$1" ] ; then
   OUTPUT="$1"
else
   OUTPUT=pysol_compile.py
fi

# Remove BUNDLE blocks, comment out imports
find . -name '*.py' | while read file ; do
   sed '/^# PySol imports/,/^$/s/^[[:space:]]*[[:alpha:]].*/#&/
       /^# [Tt]oolkit imports/,/^$/s/^from.*/#&/
       /^# Toolkit imports/,/^$/s/^import.*/#&/
       /^# stats imports/,/^$/s/^from.*/#&/' < ${file} > ${file}.tmp
   mv ${file}.tmp ${file}
done

# get the rest of the imports
for file in move.py mfxutil.py pysolaudio.py ; do
   sed '/^from mfxtools/s/.*/#&/' < ${file} > ${file}.tmp
   mv ${file}.tmp ${file}
done

sed '/^# import the games/,/^del /s/.*/# &/' < pysol.py > pysol.py.tmp
sed '/^# We need this so that saved games/,/^del /s/.*/# &/' < pysol.py.tmp \
   > pysol.py
rm pysol.py.tmp

# these things are just silly and dumb and tend to make things crash
for file in mfxtools.py random.py tk/tkwidget.py tk/toolbar.py \
      tk/progressbar.py tk/statusbar.py tk/edittextdialog.py \
      tk/tkwidget.py tk/demooptionsdialog.py tk/playeroptionsdialog.py \
      tk/tkhtml.py tk/soundoptionsdialog.py ; do
   sed "/^def `basename $file .py`_main(/,\$s/.*/# &/" < $file > ${file}.tmp
   mv ${file}.tmp $file
done

sed '/^if __name__/,$s/.*/# &/' < tk/tktree.py > tk/tktree.py.tmp
mv tk/tktree.py.tmp tk/tktree.py

# make failure to load the soundserver non-fatal
# whitespace is important!
sed '/^import pysolsoundserver/s/.*/try:\
    import pysolsoundserver\
except ImportError:\
    pysolsoundserver = None/' < pysolaudio.py > pysolaudio.py.tmp
mv pysolaudio.py.tmp pysolaudio.py

echo "# coding: iso-8859-1" | 
cat - mfxtools.py mfxutil.py version.py util.py resource.py gamedb.py \
   tk/tkinit.py tk/tkconst.py tk/tkfont.py tk/tkutil.py tk/tkcanvas.py \
   tk/tkwrap.py tk/tkwidget.py tk/tkhtml.py tk/edittextdialog.py stats.py \
   tk/tkstats.py tk/playeroptionsdialog.py pysolaudio.py \
   tk/soundoptionsdialog.py tk/demooptionsdialog.py random.py \
   help.py actions.py tk/toolbar.py tk/statusbar.py tk/progressbar.py \
   tk/tktree.py tk/selecttree.py tk/selectgame.py tk/selectcardset.py \
   tk/selecttile.py tk/menubar.py acard.py tk/card.py images.py move.py \
   hint.py game.py app.py main.py stack.py layout.py > "$OUTPUT"

cat games/yukon.py >> "$OUTPUT"
cat games/gypsy.py >> "$OUTPUT"
cat games/fortythieves.py >> "$OUTPUT"
find games ! -name games -type d -prune -o -type f -name '*.py' ! -name 'yukon.py' ! -name 'gypsy.py' ! -name 'fortythieves.py' -print | sort | xargs cat >> "$OUTPUT"
cat games/contrib/*.py >> "$OUTPUT"
cat games/special/*.py >> "$OUTPUT"
cat pysol.py >> "$OUTPUT"

python -c "from py_compile import * ; compile('${OUTPUT}')"

