#!/bin/sh
# Copyright 2008 Simon Cross <hodgestar@gmail.com>
# GPL - see COPYING for details
#
# Usage: sutekh-wine-py2exe

SUTEKH_VERSION=`PYTHONPATH=. python -c "from sutekh import SutekhInfo; print SutekhInfo.SutekhInfo.VERSION_STR"`
BUILD_NAME="sutekh-${SUTEKH_VERSION}"
BUILD_FOLDER="build/${BUILD_NAME}-py2exe"
ZIP_NAME="dist/${BUILD_NAME}.zip"
PY2EXE_LOG="build/${BUILD_NAME}-py2exe.log"

# Calculate paths.

WINE_WIN_PYTHON=`wine python -u -c "import os, sys; print os.path.dirname(sys.executable)"`
WINE_WIN_SYS32="C:/windows/system32"
WINE_WIN_GTK="C:/GTK"
WINE_PYTHON=`winepath "${WINE_WIN_PYTHON}"`
WINE_SYS32=`winepath "${WINE_WIN_SYS32}"`
WINE_GTK=`winepath "${WINE_WIN_GTK}"`

# Sanitize build environment

rm -rf "${BUILD_FOLDER}"
rm -rf "${ZIP_NAME}"
rm -f "${PY2EXE_LOG}"
mkdir -p "${BUILD_FOLDER}"
mkdir -p `dirname "${ZIP_NAME}"`

#
# Report vairables 
#

echo "=== Building Sutekh for Windows using Wine ==="
echo ""
echo "  Sutekh version: ${SUTEKH_VERSION}"
echo "  Build folder: ${BUILD_FOLDER}"
echo "  Dist file: ${ZIP_NAME}"
echo "  Wine Python: ${WINE_PYTHON}"
echo "  Wine System32: ${WINE_SYS32}"
echo "  Wine GTK: ${WINE_GTK}"
echo ""

#
# Run py2exe build under wine
#

echo "=== Running wine python setup.py ==="
echo ""
echo "  Writing log to ${PY2EXE_LOG}."
echo "  ---"
echo "  Please make sure you have patched your py2exe run.exe"
echo "  and run_w.exe with PETools (they need to have their"
echo "  executable size correctly set)."
echo ""

wine python setup.py py2exe >${PY2EXE_LOG} 2>&1

# Copy /etc, /lib and /share/themes from GTK Wine installation
# Fix gdk-pixbuf.loaders
#  TODO: this fix is rather fragile
# Copy /bin/*.dll from the GTK Wine installation
# Copy python dll that py2exe misses   
# Note that this doesn't currently handle the msvcr90 dll juggling that's
# required - the NSIS installer should handle this by installing the
# correct vcredist_x86.exe 
# Copy sqlite dll that py2exe misses

echo "=== Copying in GTK and Python dependencies that py2exe missed ==="
echo ""

echo "'${WINE_GTK}/etc' -> '${BUILD_FOLDER}'"
cp -R "${WINE_GTK}/etc" "${BUILD_FOLDER}"

echo "'${WINE_GTK}/lib' -> '${BUILD_FOLDER}'"
cp -R "${WINE_GTK}/lib" "${BUILD_FOLDER}"

echo "'${WINE_GTK}/share/themes' -> '${BUILD_FOLDER}/share'"
mkdir -p "${BUILD_FOLDER}/share"
cp -R "${WINE_GTK}/share/themes" "${BUILD_FOLDER}/share"

echo "Fixing gdk-pixbuf.loaders (this fix is rather hacky) ..."
sed -i -e 's/^"C:\/GTK\//"/' "${BUILD_FOLDER}/etc/gtk-2.0/gdk-pixbuf.loaders"
rm "${BUILD_FOLDER}/etc/gtk-2.0/gdk-pixbuf.loaders.orig"

echo "'${WINE_GTK}/bin/*.dll' -> '${BUILD_FOLDER}'"
cp "${WINE_GTK}/bin/"*.dll "${BUILD_FOLDER}"

cp -v "${WINE_PYTHON}/python26.dll" "${BUILD_FOLDER}"
cp -v "${WINE_PYTHON}/DLLs/sqlite3.dll" "${BUILD_FOLDER}"

echo ""

#
# create zip file
#

echo "=== Creating zip of patched py2exe dist folder ==="
echo ""
echo "  Zip file: ${ZIP_NAME}"
echo "  Build folder: ${BUILD_FOLDER}"
echo ""

rm -f "$ZIP_NAME"
cd `dirname "${BUILD_FOLDER}"`
zip -r -q "$OLDPWD/$ZIP_NAME" `basename "${BUILD_FOLDER}"`
cd "$OLDPWD"
