#!/bin/bash

#################
#set variables MINGW_HOST 

HOST=${MINGW_HOST:-"i586-mingw32msvc"}
export CC=${HOST}"-gcc"

#################

#we move in the current script directory
script_dir=$(readlink -f $0) || exit 1
script_dir=${script_dir%\/*.sh}
cd $script_dir/../..

. ./mp3splt-gtk/include_variables.sh || exit 1

put_package "cross_windows"

#we run autoconf and automake..
cd mp3splt-gtk && ./autogen.sh win || exit 1 && cd ..

#untar and copy the required libraries
cd ../libs
tar jxf mp3splt-gtk_mingw_required_libs.tar.bz2 || exit 1
tar jxf mp3splt-gtk_runtime.tar.bz2 || exit 1
cp -a mp3splt-gtk_runtime/*.dll ./bin || exit 1
cp lib/libmad.a lib/libvorbis.a lib/libogg.a lib/libid3tag.a lib/libz.a \
    lib/libvorbisfile.a ../trunk/mp3splt-gtk/ || exit 1
cp lib/libmad.a lib/libvorbis.a lib/libogg.a lib/libid3tag.a lib/libz.a \
    lib/libvorbisfile.a ../trunk/mp3splt-gtk/src || exit 1
cp MP3SPLT-GTK_LIBS_README_LICENSES_SOURCES.txt ../trunk || exit 1

sed -i 's/\/mingw\/lib\/libltdl.la/-lltdl/g' lib/libmp3splt.la || exit 1

for a in lib/*.la;do 
 sed -i 's/\/opt\/lib\///g' $a;
 sed -i 's/libcairo.la/-lcairo/g' $a;
 sed -i 's/libpangowin32-1.0.la/-lpangowin32-1.0/g' $a;
 sed -i 's/libpangoft2-1.0.la/-lpangoft2-1.0/g' $a;
 sed -i 's/libgio-2.0.la/-lgio-2.0/g' $a;
 sed -i 's/libpixman-1.la/-lpixman-1/g' $a;
 sed -i 's/libpng15.la/-lpng15/g' $a;
 sed -i 's/libfontconfig.la/-lfontconfig/g' $a;
 sed -i 's/libpango-1.0.la/-lpango-1.0/g' $a;
 sed -i 's/libfreetype.la/-lfreetype/g' $a;
 sed -i 's/\/mingw\/lib\/libexpat.la/-lexpat/g' $a;
 sed -i 's/libgobject-2.0.la/-lgobject-2.0/g' $a;
 sed -i 's/libgmodule-2.0.la/-lgmodule-2.0/g' $a;
 sed -i 's/libgthread-2.0.la/-lgthread-2.0/g' $a;
 sed -i 's/libffi.la/-lffi/g' $a;
 sed -i 's/libglib-2.0.la/-lglib-2.0/g' $a;
 sed -i 's/\/mingw\/lib\/libintl.la/-lintl/g' $a;
 sed -i 's/\/mingw\/lib\/libiconv.la/-liconv/g' $a;
 sed -i 's/libgdk-3.la/-lgdk-3/g' $a;
 sed -i 's/libatk-1.0.la/-latk-1.0/g' $a;
 sed -i 's/libpangocairo-1.0.la/-lpangocairo-1.0/g' $a;
 sed -i 's/libgdk_pixbuf-2.0.la/-lgdk_pixbuf-2.0/g' $a;
 sed -i 's/libcairo-gobject.la/-lcairo-gobject/g' $a;
 sed -i 's/\/usr\/lib\/libintl.la/-lintl/g' $a;
done

sed -i '1i # Generated by ltmain.sh - GNU libtool 1.5.25a (1.1220.2.458 2007/06/30 09:32:00)' lib/libiconv.la

cd ..

#cross compile flags
export CFLAGS=" -mms-bitfields -enable-stdcall-fixup -I`pwd`/libs/include -D_WIN32 -D__MINGW32__ $CFLAGS"
export LDFLAGS="-L`pwd`/libs/lib $LDFLAGS"
export PKG_CONFIG_PATH="`pwd`/libs/lib/pkgconfig"
export PKG_CONFIG=/usr/bin/pkg-config
export PATH="`pwd`/libs/bin:$PATH"

#modify pkg-config files path
TARGET=`pwd`/libs
for f in `pwd`/libs/lib/pkgconfig/*.pc ; do
  cat $f | sed s+^prefix=.*+prefix=$TARGET+ > $f.tmp
  mv $f.tmp $f
done  

make -C trunk/mp3splt-gtk/libltdl

#we compile mp3splt-gtk
cd trunk/mp3splt-gtk &&\
cp ../../libs/lib/libmp3splt.a . && cp ../../libs/lib/libmp3splt.a ./src &&\
./configure --enable-optimise --disable-cutter --disable-audacious --disable-gnome --prefix=`pwd`/../../libs --host=$HOST --disable-gtktest &&\
make clean && make && make install &&\
${HOST}-strip ./src/mp3splt-gtk.exe || exit 1

