#!/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"

WINE_VER=`wine --version`

# py2exe scripts produced by wine-1.4 don't work on Windows 7 for
# some reason, while wine-1.2 works fine in our testing, so we
# check for this
if ! echo ${WINE_VER} | grep -q ^wine-1.2; then
   echo "Wine version ${WINE_VER} is not wine 1.2"
   echo "This may cause problems on some versions of windows"
   read -p "Break to abort, Enter to continue : " JUNK
fi

# 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}"`
PY_DLL=`wine python -u -c "import sys; print 'python%d%d.dll' % (sys.version_info[0], sys.version_info[1])"`

# 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 -f "${BUILD_FOLDER}/etc/gtk-2.0/gdk-pixbuf.loaders.orig"
echo "Fixing loaders.cache (also very fragile)"
sed -i -e 's#^"C:/Python2./Lib/site-packages/gtk-2.0/runtime/#"#' "${BUILD_FOLDER}/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache"


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

if [ -f "${WINE_PYTHON}/${PY_DLL}" ]; then
   cp -v "${WINE_PYTHON}/${PY_DLL}" "${BUILD_FOLDER}"
elif [ -f "${WINE_SYS32}/${PY_DLL}" ]; then
   cp -v "${WINE_SYS32}/${PY_DLL}" "${BUILD_FOLDER}"
else
   echo "Unable to find python dll - ${PY_DLL}"
   echo "Aborting"
   exit
fi
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"
