#!/bin/sh

echo "This builds the development and runtime installers for gtkmm. Make "
echo "sure to have everything (libsigc++, glibmm, cairomm, pangomm, gtkmm, "
echo "libglademm, libxml++) installed to /usr/local, and to have "
echo "successfully built everything with both MSVC 2005 and MSVC 2008 (both "
echo "Debug and Release builds). Make also sure that the official GTK+ "
echo "bundle has been unzipped to echo /bundle, as well as libxml2 and iconv "
echo "runtime and development files. Finally, make sure that makensis is in "
echo "your PATH"

# Copy all of /usr/local into here. It's probably more than we need, but we
# can be sure we have the required stuff this way.
# TODO: Maybe it would be enough to create Windows Shortcuts here.
echo "Copying mingw files..."
cp -R /usr/local/bin . || exit -1
cp -R /usr/local/lib . || exit -1
cp -R /usr/local/share . || exit -1
cp -R /usr/local/include . || exit -1

# Same with /bundle, for GTK+
cp -R /bundle/bin . || exit -1
cp -R /bundle/lib . || exit -1
cp -R /bundle/share . || exit -1
cp -R /bundle/include . || exit -1
cp -R /bundle/etc . || exit -1
cp -R /bundle/contrib . || exit -1

# The license text in the installer
cp ../COPYING lgpl.txt

echo "Removing non-GTK locales..."
# Remove all locales we don't want, so the installer can simply copy share/locale recursively, in case there is more than just the GTK+ bundle in /bundle.
find share/locale -type f | grep -v libiconv.mo | grep -v glib20.mo | grep -v gtk20.mo | grep -v gtk20-properties.mo | xargs -r rm || exit -1
find share/locale -type d | xargs rmdir -p --ignore-fail-on-non-empty || exit -1

# Change prefix in .pc files to be /target instead of /usr/local. pkg-config
# then looks relative to the .pc file for the actual package.

# TODO: If someone knows how to do this more elegant, please tell me
# (armin@arbur.net). Note that perl -i does _not_ work on Windows (at least
# not with the msys one).
echo "Fixing pkg-config prefixes..."
mkdir -p temp || exit -1
for file in lib/pkgconfig/*.pc; do
	perl -pe 's/prefix=\/usr\/local/prefix=\/target/' $file > temp/`basename $file` || exit -1
done

for file in temp/*.pc; do
	mv $file lib/pkgconfig/`basename $file` || exit -1
done
rmdir temp

# Use the MS-Windows theme by default
echo "Setting GTK theme..."
echo "gtk-theme-name = \"MS-Windows\"" > etc/gtk-2.0/gtkrc || exit -1

# Strip DLLs for the runtime version
echo "Stripping binaries..."
mkdir -p bin_stripped || exit -1
cp -R bin/* bin_stripped || exit -1
strip bin_stripped/*.dll || exit -1
strip bin_stripped/*.exe || exit -1;

# TODO: Strip modules in lib/gtk-2.0?

# Get the MSVC files into here
echo "Copying MSVC files..."
for module in libsigc++ glibmm giomm cairomm pangomm atkmm gdkmm gtkmm libglademm libxml++; do
	for toolset in vc80 vc90; do
		./install-msvc-module $module $toolset || exit -1
	done
done

cp gtkmm-vc80-2_4.vsprops MSVC || exit -1
cp gtkmm-vc90-2_4.vsprops MSVC || exit -1
cp gtkmm-vc80-d-2_4.vsprops MSVC || exit -1
cp gtkmm-vc90-d-2_4.vsprops MSVC || exit -1
# Compatibility with old naming scheme
cp gtkmm-vc80-2_4.vsprops MSVC/gtkmm-2.4.vsprops || exit -1
cp gtkmm-vc80-d-2_4.vsprops MSVC/gtkmm-2.4d.vsprops || exit -1

echo "Copying gtk-demo..."
mkdir -p demo || exit -1
cp -R ../demos/gtk-demo/* demo/ || exit -1
cp ../demos/gtk-demo/.libs/gtkmm-demo.exe demo/demo.exe || exit -1
strip demo/demo.exe || exit -1

echo "Creating installer..."
makensis gtkmm-installer.nsi || exit -1

echo "Creating runtime installer..."
perl -pe 's/; !define RUNTIME_ONLY 1/!define RUNTIME_ONLY 1/' gtkmm-installer.nsi > gtkmm-runtime-installer.nsi || exit -1
makensis gtkmm-runtime-installer.nsi || exit -1
