#!/bin/sh
# pysol-compile.sh
#
# $Id: pysol-compile.sh,v 1.1 2003/07/01 02:07:42 das-cvs Exp $
#
# 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

# First BUNDLE mark in mfxutil shouldn't be there
sed '/^#%ifndef BUNDLE/{d;q}' < mfxutil.py > mfxutil.py.tmp
sed '/^#%endif/{d;q}' < mfxutil.py.tmp > mfxutil.py
rm mfxutil.py.tmp

# Remove BUNDLE blocks, comment out imports
find . -name '*.py' | while read file ; do
   sed '/^#%ifndef BUNDLE/,/^#%endif/d
       /^# 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

# 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

# make sure it doesn't try to load the non-existant freecellsolver
sed '/^class FreeCellSolverWrapper:/s/.*/pysolfreecellsolver = None\
&/' < hint.py > hint.py.tmp
mv hint.py.tmp hint.py


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 pysoltk.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 > pysol_compile.py

cat games/yukon.py >> pysol_compile.py
cat games/gypsy.py >> pysol_compile.py
cat games/fortythieves.py >> pysol_compile.py
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 >> pysol_compile.py
cat games/contrib/*.py >> pysol_compile.py
cat games/special/*.py >> pysol_compile.py
cat pysol.py >> pysol_compile.py

python -c "from py_compile import * ; compile('pysol_compile.py')"

