#!/bin/bash
#
# OpenRPT report writer and rendering engine
# Copyright (C) 2001-2008 by OpenMFG, LLC
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Please contact info@openmfg.com with any questions on this license.
#

APPNAME=`awk '/^QMAKE_TARGET/ {print $3}' Makefile`
echo $0 for $APPNAME

if [ -z "$QTDIR" ] ; then
  QTDIR=/opt/qt/4.1.4
fi
if ! [ -d "$QTDIR" ] ; then
  echo "Cannot find QTDIR"
  exit 1
fi

if [ -z "$PGDIR" ] ; then
  PGDIR="$QTDIR"
fi
if ! [ -d $PGDIR/lib ] ; then
  echo "Cannot find PGDIR/lib"
  exit 1
fi
FRAMEWORKSDIR=../../bin/${APPNAME}.app/Contents/Frameworks

if ! [ -d "$FRAMEWORKSDIR" ] ; then
  mkdir "$FRAMEWORKSDIR"
fi
cd "$FRAMEWORKSDIR"

LIBLIST=`otool -L ../MacOS/${APPNAME} | awk '{print $1}' | grep "$QTDIR"`
for ORIGLIB in $LIBLIST "$PGDIR"/lib/libpq.4.dylib ; do
  NEWLIB=`basename "$ORIGLIB"`
  cp "$ORIGLIB" $NEWLIB

  install_name_tool -id @executable_path/../Frameworks/"$NEWLIB" "$NEWLIB"
  install_name_tool -change $ORIGLIB @executable_path/../Frameworks/"$NEWLIB" ../MacOS/${APPNAME}
done

# update lib path for the current library in any others that refer to it
for REPLLIB in *.dylib ; do
  LIBLIST=`otool -L ${REPLLIB} | awk '{print $1}' | grep "$QTDIR"`
  for ORIGLIB in $LIBLIST ; do
    NEWLIB=`basename "$ORIGLIB"`
    install_name_tool -change "$ORIGLIB" @executable_path/../Frameworks/"$NEWLIB" "$REPLLIB"
  done
done

if ! [ -d "../plugins/sqldrivers" ]; then
  mkdir -p ../plugins/sqldrivers
fi
cd ../plugins/sqldrivers
cp $QTDIR/plugins/sqldrivers/libqsqlpsql.dylib .
install_name_tool -change $QTDIR/lib/libQtSql.4.dylib @executable_path/../Frameworks/libQtSql.4.dylib libqsqlpsql.dylib
install_name_tool -change $QTDIR/lib/libQtCore.4.dylib @executable_path/../Frameworks/libQtCore.4.dylib libqsqlpsql.dylib
install_name_tool -change $PGDIR/lib/libpq.4.dylib @executable_path/../Frameworks/libpq.4.dylib libqsqlpsql.dylib

cd ..

if ! [ -d "imageformats" ]; then
  mkdir imageformats
fi
cd imageformats
for IMAGELIB in libqgif.dylib libqjpeg.dylib libqmng.dylib ; do
  cp $QTDIR/plugins/imageformats/$IMAGELIB .
  install_name_tool -change $QTDIR/lib/libQtGui.4.dylib @executable_path/../Frameworks/libQtGui.4.dylib $IMAGELIB
  install_name_tool -change $QTDIR/lib/libQtCore.4.dylib @executable_path/../Frameworks/libQtCore.4.dylib $IMAGELIB
done

cd ../../Resources

echo "[Paths]" > qt.conf
echo "Prefix = .." >> qt.conf
