#!/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 /gtkmm_debug and /gtkmm_release, "
echo "respectively and to have successfully built everything with both "
echo "MSVC 2005 and MSVC 2008 (both Debug and Release builds). Make also "
echo "sure that the official GTK+ bundle has been unzipped to /bundle, as "
echo "well as libxml2 and iconv runtime and development files. Install "
echo "libglade into /libglade. Finally, make sure that makensis is in your PATH"

echo "Copying mingw files..."
cp -R /gtkmm_debug/bin . || exit -1
cp -R /gtkmm_debug/lib . || exit -1
cp -R /gtkmm_debug/share . || exit -1
cp -R /gtkmm_debug/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

# libglade
cp -R /libglade/bin . || exit -1
cp -R /libglade/include . || exit -1
cp -R /libglade/lib . || exit -1
cp -R /libglade/share . || 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
# Use the gtkmm release binaries (they have been compiled with -O2 instead of -O0)
cp -R /gtkmm_release/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?
# TODO: Strip .dll.a files with --strip-unneeded

# 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
