#!/bin/sh
echo "You need to execute this on a Windows machine within msys (http://www.mingw.org)"
echo "You also need InnoSetup (http://www.innosetup.org) with iscc in your PATH"
echo "You need to have python, pygobject, pycairo and pygtk installed into C:\\Python25"
echo "Make sure glom and all its dependencies have been installed correctly to /local"

revision=$1
if test "$revision" = ''; then
  echo "Installer revision not provided, assuming 1"
  revision=1
fi

# TODO: Let configure replace @pyexecdir@ to get the correct python path?
PYTHONDIR=/c/Python25
POSTGRESDIR=/c/postgres

echo "Cleanup..."
if test -e installer; then
  rm installer -Rf || exit;
fi

mkdir -p installer || exit

echo "Copying DLL files..."

mkdir -p installer/bin

#cp /local/bin/libintl-8.dll installer/bin || exit
cp /bin/libgettextpo-0.dll installer/bin || exit
# TODO: There are no usable libxslt Windows binaries, therefore I built this myself:
cp /local/bin/libxslt-1.dll installer/bin || exit

cp /bundle/bin/libglib-2.0-0.dll installer/bin || exit
cp /bundle/bin/libgio-2.0-0.dll installer/bin || exit
cp /bundle/bin/libgmodule-2.0-0.dll installer/bin || exit
cp /bundle/bin/libgobject-2.0-0.dll installer/bin || exit
cp /bundle/bin/libgthread-2.0-0.dll installer/bin || exit

# TODO: We can probably omit these, as we do not use g_spawn on Windows anymore
cp /bundle/bin/gspawn-win32-helper.exe installer/bin || exit
cp /bundle/bin/gspawn-win32-helper-console.exe installer/bin || exit

cp /bundle/bin/libatk-1.0-0.dll installer/bin || exit
cp /bundle/bin/libxml2-2.dll installer/bin || exit
# TODO: Remove as soon as libgda is built against recent libxml2
cp libxml2.dll installer/bin || exit
cp /bundle/bin/libfontconfig-1.dll installer/bin || exit
cp /bundle/bin/libexpat-1.dll installer/bin || exit
cp /bundle/bin/freetype6.dll installer/bin || exit
cp /bundle/bin/libcairo-2.dll installer/bin || exit
cp /bundle/bin/libpng14-14.dll installer/bin || exit
#cp /bundle/bin/libjpeg-7.dll installer/bin || exit
#cp /bundle/bin/libtiff-3.dll installer/bin || exit

cp /bundle/bin/libpango-1.0-0.dll installer/bin || exit
cp /bundle/bin/libpangoft2-1.0-0.dll installer/bin || exit
cp /bundle/bin/libpangocairo-1.0-0.dll installer/bin || exit
cp /bundle/bin/libpangowin32-1.0-0.dll installer/bin || exit

cp /bundle/bin/libgdk-win32-2.0-0.dll installer/bin || exit
cp /bundle/bin/libgdk_pixbuf-2.0-0.dll installer/bin || exit

cp /bundle/bin/libgtk-win32-2.0-0.dll installer/bin || exit
cp /gtksourceview/bin/libgtksourceview-2.0-0.dll installer/bin || exit

#cp /bin/libORBit-2-0.dll installer/bin || exit
#cp /bin/libORBitCosNaming-2-0.dll installer/bin || exit
#cp /bin/libORBit-imodule-2-0.dll installer/bin || exit

#cp /bin/libgconf-2-4.dll installer/bin || exit
#cp /libglade/bin/libglade-2.0-0.dll installer/bin || exit
cp /goocanvas/bin/libgoocanvas-3.dll installer/bin || exit

cp /libgda/bin/libgda-4.0-4.dll installer/bin || exit
#cp /libgda/bin/libgdasql-3.0-2.dll installer/bin || exit
cp /libgda/bin/libgda-report-4.0-4.dll installer/bin || exit

cp /gtkmm_release/bin/libsigc-2.0-0.dll installer/bin || exit
cp /gtkmm_release/bin/libglibmm-2.4-1.dll installer/bin || exit
cp /gtkmm_release/bin/libgiomm-2.4-1.dll installer/bin || exit
cp /gtkmm_release/bin/libatkmm-1.6-1.dll installer/bin || exit
cp /gtkmm_release/bin/libcairomm-1.0-1.dll installer/bin || exit
cp /gtkmm_release/bin/libpangomm-1.4-1.dll installer/bin || exit
cp /gtkmm_release/bin/libgdkmm-2.4-1.dll installer/bin || exit
cp /gtkmm_release/bin/libxml++-2.6-2.dll installer/bin || exit
cp /gtkmm_release/bin/libgtkmm-2.4-1.dll installer/bin || exit
cp /local/bin/libgtksourceviewmm-2.0-2.dll installer/bin || exit
cp /local/bin/libgoocanvasmm-1.0-5.dll installer/bin || exit
#cp /local/bin/libgconfmm-2.6-1.dll installer/bin || exit
cp /local/bin/libgdamm-4.0-12.dll installer/bin || exit
#cp /gtkmm_release/bin/libglademm-2.4-1.dll installer/bin || exit

# cp /local/bin/libbakery-2.6--6-1.dll installer/bin || exit
# TODO: Can we build boost so that mingwm10.dll is not required? Maybe try static C runtime linkage
cp /mingw/bin/mingwm10.dll installer/bin || exit
cp /boost/lib/boost_python-mgw34-mt-1_42.dll installer/bin || exit
cp /local/bin/libglom-1.16-0.dll installer/bin || exit

echo "Stripping DLL files..."
strip installer/bin/*.dll || exit
strip installer/bin/*.exe || exit

# stripping these render them unusable (although no changing in size).
# We therefore copy them after having stripped the rest. Same with the other
# Perhaps those were built with MSVC.
cp /bundle/bin/intl.dll installer/bin || exit
# TODO: iconv no longer required with new libxml2-2.dll
cp iconv.dll installer/bin || exit
cp /bundle/bin/zlib1.dll installer/bin || exit

echo "Copying Python..."

# TODO: Find out Windows directory somehow, perhaps by looking at PATH?
# There is $WINDIR already.
cp /c/WINDOWS/system32/python25.dll installer/bin || exit

# We through all python modules into python/. Glom sets PYTHONPATH accordingly.
mkdir -p installer/python || exit

# TODO: Perhaps some scripts need more python modules.
cp $PYTHONDIR/Lib/*.py installer/python || exit

mkdir -p installer/python/encodings || exit
cp $PYTHONDIR/Lib/encodings/*.py installer/python/encodings || exit

cp $PYTHONDIR/Lib/site-packages/glom_1_14.pyd installer/python || exit
strip installer/python/glom_1_14.pyd || exit
cp $PYTHONDIR/Lib/site-packages/gtk-2.0/gda.pyd installer/python || exit
strip installer/python/gda.pyd || exit

cp $PYTHONDIR/Lib/site-packages/pygtk.py installer/python || exit
cp $PYTHONDIR/Lib/site-packages/gtk-2.0/*.pyd installer/python || exit

mkdir -p installer/python/gobject || exit
cp $PYTHONDIR/Lib/site-packages/gtk-2.0/gobject/*.py installer/python/gobject || exit
cp $PYTHONDIR/Lib/site-packages/gtk-2.0/gobject/*.pyd installer/python/gobject || exit

mkdir -p installer/python/cairo || exit
cp $PYTHONDIR/Lib/site-packages/cairo/*.py installer/python/cairo || exit
cp $PYTHONDIR/Lib/site-packages/cairo/*.pyd installer/python/cairo || exit

mkdir -p installer/python/gtk || exit
cp $PYTHONDIR/Lib/site-packages/gtk-2.0/gtk/*.py installer/python/gtk || exit
cp $PYTHONDIR/Lib/site-packages/gtk-2.0/gtk/*.pyd installer/python/gtk || exit

echo "Copying modules..."

#cp $PYTHONDIR/Lib/site-packages/glom_1_12.pyd installer/bin || exit
#cp $PYTHONDIR/Lib/site-packages/gtk-2.0/gda.pyd installer/bin || exit
#strip installer/bin/*.pyd || exit

#mkdir -p installer/lib/gtk-2.0/2.10.0/immodules || exit
#cp /lib/gtk-2.0/2.10.0/immodules/*.dll installer/lib/gtk-2.0/2.10.0/immodules || exit
#strip installer/lib/gtk-2.0/2.10.0/immodules/*.dll || exit

mkdir -p installer/lib/gtk-2.0/2.10.0/engines || exit
cp /bundle/lib/gtk-2.0/2.10.0/engines/libwimp.dll installer/lib/gtk-2.0/2.10.0/engines || exit
strip installer/lib/gtk-2.0/2.10.0/engines/libwimp.dll || exit

#mkdir -p installer/lib/gtk-2.0/2.10.0/loaders || exit
#cp /bundle/lib/gtk-2.0/2.10.0/loaders/*.dll installer/lib/gtk-2.0/2.10.0/loaders || exit
#strip installer/lib/gtk-2.0/2.10.0/loaders/*.dll || exit

mkdir -p installer/etc/gtk-2.0
cp /bundle/etc/gtk-2.0/gtk.immodules installer/etc/gtk-2.0 || exit -1
cp /bundle/etc/gtk-2.0/gdk-pixbuf.loaders installer/etc/gtk-2.0 || exit -1

mkdir -p installer/share/themes || exit
cp -R /bundle/share/themes/MS-Windows installer/share/themes || exit
mkdir -p installer/etc/gtk-2.0 || exit
echo "gtk-theme-name = \"MS-Windows\"" > installer/etc/gtk-2.0/gtkrc || exit

mkdir -p installer/lib/libgda-4.0/providers || exit
cp /libgda/lib/libgda-4.0/providers/libgda-postgres.dll installer/lib/libgda-4.0/providers || exit
cp /libgda/lib/libgda-4.0/providers/libgda-sqlite.dll installer/lib/libgda-4.0/providers || exit
strip installer/lib/libgda-4.0/providers/*.dll || exit

mkdir -p installer/etc/libgda-4.0 || exit
cp /libgda/etc/libgda-4.0/config installer/etc/libgda-4.0 || exit

echo "Copying locales..."

# We need to keep the locale files from share/locale in share/locale and those
# from lib/locale in lib/locale:
cp /bundle/share/locale installer/share -R || exit # GTK+ + deps
cp /share/locale installer/share -R || exit # GConf2
cp /local/share/locale installer/share -R || exit # bakery, glom
cp /libgda/share/locale installer/share -R || exit # libgda-4.0
cp /gtksourceview/share/locale installer/share -R || exit # gtksourceview
# TODO: Bail if a translation file is missing

TRANSLATIONS="glib20.mo gtk20-properties.mo atk10.mo gtk20.mo gtksourceview-2.0.mo GConf2.mo libgda-4.0.mo bakery.mo glom.mo"

#find installer/lib/locale/ -type f | grep -v atk10.mo | grep -v gtk20.mo | grep -v bakery.mo | grep -v GConf2.mo | grep -v glib20.mo | grep -v glom.mo | grep -v gtk20.mo | grep -v gtk20-properties.mo | grep -v gtksourceview-2.0.mo |  grep -v libgda-4.0.mo | xargs rm
#find installer/lib/locale -type d | xargs rmdir -p --ignore-fail-on-non-empty

find installer/share/locale/ -type f | grep -v atk10.mo | grep -v gtk20.mo | grep -v bakery.mo | grep -v GConf2.mo | grep -v glib20.mo | grep -v glom.mo | grep -v gtk20.mo | grep -v gtk20-properties.mo | grep -v gtksourceview-2.0.mo |  grep -v libgda-4.0.mo | xargs rm
find installer/share/locale -type d | xargs rmdir -p --ignore-fail-on-non-empty

echo "Copying executable..."
cp /local/bin/glom.exe installer/bin || exit
strip installer/bin/glom.exe || exit

echo "Copying postgres..."
mkdir -p installer/bin || exit
cp $POSTGRESDIR/bin/postgres.exe installer/bin || exit
cp $POSTGRESDIR/bin/initdb.exe installer/bin || exit
cp $POSTGRESDIR/bin/pg_ctl.exe installer/bin || exit

cp $POSTGRESDIR/lib/libpq.dll installer/bin || exit
strip installer/bin/postgres.exe installer/bin/initdb.exe installer/bin/pg_ctl.exe installer/bin/libpq.dll || exit
# This is required by libpq.dll in the official installer
#cp $POSTGRESDIR/bin/libintl3.dll installer/bin || exit
#cp $POSTGRESDIR/bin/libiconv2.dll installer/bin || exit
#cp $POSTGRESDIR/bin/krb5_32.dll installer/bin || exit
#cp $POSTGRESDIR/bin/k5sprt32.dll installer/bin || exit
#cp $POSTGRESDIR/bin/gssapi32.dll installer/bin || exit
#cp $POSTGRESDIR/bin/comerr32.dll installer/bin || exit
#cp $POSTGRESDIR/bin/libeay32.dll installer/bin || exit
#cp $POSTGRESDIR/bin/ssleay32.dll installer/bin || exit

mkdir -p installer/share/postgresql || exit
cp $POSTGRESDIR/share/postgres.bki installer/share/postgresql || exit
cp $POSTGRESDIR/share/postgres.description installer/share/postgresql || exit
cp $POSTGRESDIR/share/postgres.shdescription installer/share/postgresql || exit
cp $POSTGRESDIR/share/conversion_create.sql installer/share/postgresql || exit
cp $POSTGRESDIR/share/information_schema.sql installer/share/postgresql || exit
cp $POSTGRESDIR/share/snowball_create.sql installer/share/postgresql || exit
cp $POSTGRESDIR/share/system_views.sql installer/share/postgresql || exit
cp $POSTGRESDIR/share/pg_hba.conf.sample installer/share/postgresql || exit
cp $POSTGRESDIR/share/pg_ident.conf.sample installer/share/postgresql || exit
cp $POSTGRESDIR/share/postgresql.conf.sample installer/share/postgresql || exit
cp $POSTGRESDIR/share/sql_features.txt installer/share/postgresql || exit
cp -R $POSTGRESDIR/share/timezone installer/share/postgresql || exit
cp -R $POSTGRESDIR/share/timezonesets installer/share/postgresql || exit

mkdir -p installer/lib/postgresql || exit
cp $POSTGRESDIR/lib/*.dll installer/lib/postgresql || exit
# These are not conversion modules
rm installer/lib/postgresql/libecpg.dll installer/lib/postgresql/libecpg_compat.dll installer/lib/postgresql/libpgtypes.dll installer/lib/postgresql/libpq.dll installer/lib/postgresql/pgevent.dll installer/lib/postgresql/plpgsql.dll || exit
strip installer/lib/postgresql/*.dll

echo "Copying shared data (glade files, icons, etc.)..."

mkdir -p installer/share/gtksourceview-2.0 || exit
cp -R /gtksourceview/share/gtksourceview-2.0/language-specs installer/share/gtksourceview-2.0 || exit
cp -R /gtksourceview/share/gtksourceview-2.0/styles installer/share/gtksourceview-2.0 || exit

mkdir -p installer/share/libgda-4.0/dtd
cp /libgda/share/libgda-4.0/information_schema.xml installer/share/libgda-4.0/
cp /libgda/share/libgda-4.0/postgres_*.xml installer/share/libgda-4.0/
cp /libgda/share/libgda-4.0/sqlite_*.xml installer/share/libgda-4.0/
cp /libgda/share/libgda-4.0/dtd/*.dtd installer/share/libgda-4.0/dtd

mkdir -p installer/share/glom/glade || exit
cp /local/share/glom/glade/glom.glade installer/share/glom/glade || exit
cp /local/share/glom/glade/glom_developer.glade installer/share/glom/glade || exit
cp -R /local/share/glom/doc installer/share/glom/ || exit
cp -R /local/share/glom/pixmaps installer/share/glom/ || exit
cp -R /local/share/glom/xslt installer/share/glom || exit

mkdir -p installer/share/icons/hicolor/48x48/apps || exit
cp /local/share/icons/hicolor/48x48/apps/glom.png installer/share/icons/hicolor/48x48/apps || exit

# This is used for the glom icon in the initial dialog. I'm not sure why GTK
# does not already get it from the icons/ directory, but it took me already
# long enough to figure out that this works:
# TODO: Maybe we'll need to use gtk_icon_theme_append_search_path to tell GTK+
# where we store the icon.
mkdir -p installer/share/pixmaps
cp /local/share/icons/hicolor/16x16/apps/glom.png installer/share/pixmaps || exit

echo "Creating installer..."

perl -pe "s/INSTALLERREVISION/$revision/" glom.iss > installer/glom.iss || exit
#cp installer || exit
iscc installer/glom.iss || exit

echo "Done"
