#!/bin/bash

#Print shell input lines as they are read
set -v

diff vk-vlc-trunk/Makefile.am vlc-trunk/Makefile.am > /tmp/diff1.diff
if [[ "$?" == "1" ]]; then
  cmp -s trunk-Makefile.am.diff /tmp/diff1.diff
  if [[ "$?" == "1" ]]; then
    echo 'There is difference between VLC Makefile.am and VK Makefile.am !'
    exit 1
  fi
fi

diff vk-vlc-trunk/bindings/java/src/Makefile.am vlc-trunk/bindings/java/src/Makefile.am > /tmp/diff1.diff
if [[ "$?" == "1" ]]; then
  cmp -s trunk-bindings-java-src-Makefile.am.diff /tmp/diff1.diff
  if [[ "$?" == "1" ]]; then
    echo 'There is difference between VLC bindings/java/src/Makefile.am and VK bindings/java/src/Makefile.am !'
    exit 1
  fi
fi

diff vk-vlc-trunk/vlc.win32.nsi.in vlc-trunk/vlc.win32.nsi.in > /tmp/diff1.diff
if [[ "$?" == "1" ]]; then
  cmp -s trunk-vlc.win32.nsi.in.diff /tmp/diff1.diff
  if [[ "$?" == "1" ]]; then
    echo 'There is difference between VLC vlc.win32.nsi.in and VK vlc.win32.nsi.in !'
    exit 1
  fi
fi

#Exit immediately if a simple command exits with a non-zero status
set -e

#
# My Linux is Kubuntu 7.10 (Gutsy)
#
# My vlc-svn dir:
#
# -rwxr-xr-x  1 root root       69 2006-09-13 14:34 checkout.sh
# -rw-r--r--  1 root root     5120 2007-07-12 04:00 jawt.dll
# -rw-r--r--  1 root root      497 2007-10-12 19:35 jawt.dll.def
# -rw-r--r--  1 root root     2232 2007-10-12 19:35 libjawt.dll.a
# -rwxr-xr-x  1 root root    19456 2002-10-30 09:50 pexports.exe
# -rw-r--r--  1 root root     2099 2007-10-21 21:29 trunk-bindings-java-src-Makefile.am.diff
# -rw-r--r--  1 root root      921 2007-10-19 22:57 trunk-Makefile.am.diff
# -rw-r--r--  1 root root       90 2007-10-21 21:29 trunk-vlc.win32.nsi.in.diff
# -rwxr-xr-x  1 root root     3792 2007-10-21 21:33 vk-build-java-mingw32.sh
# -rwxr-xr-x  1 root root     1950 2007-10-19 23:07 vk-build-java-mozilla.sh
# drwxr-xr-x  3 root root      136 2007-10-13 00:16 vk-vlc-trunk
# drwxr-xr-x 18 root root     1280 2007-10-21 14:16 vlc-trunk
# drwxr-xr-x 14 root root      792 2007-10-21 21:29 vlc-trunk-linux
# drwxr-xr-x 23 root root     2120 2007-10-13 06:55 vlc-trunk-mingw32

BUILD_DIR=vlc-trunk-mingw32
CONTRIB_DIR=/usr/win32
JAVA_HOME=/usr/lib/jvm/java-6-sun;export JAVA_HOME

if ! [ -f $CONTRIB_DIR/lib/libjawt.dll.a ]; then
  # jawt.dll is copied from windows jdk1.6.0_02
  wine pexports.exe jawt.dll | sed "s/^_//" > jawt.dll.def
  /usr/i586-mingw32msvc/bin/dlltool --add-stdcall-underscore --output-lib libjawt.dll.a --input-def jawt.dll.def --verbose
  cp libjawt.dll.a $CONTRIB_DIR/lib
fi

if ! [ -f $CONTRIB_DIR/include/jawt_md.h ]; then
  # jawt_md.h is copied from windows jdk1.6.0_02
  cp jawt_md.h $CONTRIB_DIR/include
fi

if ! [ -f $CONTRIB_DIR/include/jni_md.h ]; then
  # jni_md.h is copied from windows jdk1.6.0_02
  cp jni_md.h $CONTRIB_DIR/include
fi

if ! [ -h $JAVA_HOME/bin/java.exe ]; then
  ln -s $JAVA_HOME/bin/java $JAVA_HOME/bin/java.exe
fi

if ! [ -h $JAVA_HOME/bin/javac.exe ]; then
  ln -s $JAVA_HOME/bin/javac $JAVA_HOME/bin/javac.exe
fi

/bin/rm -r -f $BUILD_DIR
/bin/cp -pr vlc-trunk/ $BUILD_DIR
cd $BUILD_DIR
cp ../vk-vlc-trunk/Makefile.am .
cp ../vk-vlc-trunk/vlc.win32.nsi.in .
cp ../vk-vlc-trunk/bindings/java/src/Makefile.am bindings/java/src

./bootstrap > build-out0.log 2>&1 && \
PKG_CONFIG_LIBDIR=$CONTRIB_DIR/lib/pkgconfig \
CPPFLAGS="-I${CONTRIB_DIR}/include -I${CONTRIB_DIR}/include/ebml" \
LDFLAGS=-L${CONTRIB_DIR}/lib \
JAVA_HOME=/usr/lib/jvm/java-6-sun \
JAVAC=$JAVA_HOME/bin/javac.exe \
JAVA=$JAVA_HOME/bin/java.exe \
CC=i586-mingw32msvc-gcc CXX=i586-mingw32msvc-g++ \
./configure --host=i586-mingw32msvc --build=i386-linux --disable-gtk --enable-nls \
  --enable-sdl --with-sdl-config-path=$CONTRIB_DIR/bin \
  --enable-mozilla --with-mozilla-sdk-path=$CONTRIB_DIR/gecko-sdk \
  --enable-ffmpeg --with-ffmpeg-config-path=$CONTRIB_DIR/bin --with-ffmpeg-mp3lame --with-ffmpeg-faac \
  --with-ffmpeg-zlib --enable-faad --enable-flac --enable-theora \
  --with-freetype-config-path=$CONTRIB_DIR/bin --with-fribidi-config-path=$CONTRIB_DIR/bin \
  --enable-live555 --with-live555-tree=$CONTRIB_DIR/live.com --enable-caca --with-caca-config-path=$CONTRIB_DIR/bin \
  --with-xml2-config-path=$CONTRIB_DIR/bin --enable-dvdnav=no \
  --disable-cddax --disable-vcdx --enable-goom --enable-twolame --enable-dvdread \
  --enable-wxwidgets=no --enable-debug --enable-java-bindings --enable-shared-libvlc >> build-out0.log 2>&1 && \
make all >> build-out0.log 2>&1

make package-win32 > build-out1.log 2>&1
