#! /usr/bin/env bash

# Create ODB compiler Darwin distribution.
#
# -rebuild
# -arch
#
trap 'exit 1' ERR

function error ()
{
  echo "$*" 1>&2
}

rebuild=n

arch=i686-apple-darwin8
rebuild=n

while [ $# -gt 0 ]; do
  case $1 in
    -rebuild)
      rebuild=y
      shift
      ;;
    -arch)
      shift
      arch=$1
      shift
      ;;
    *)
      error "unknown option: $1"
      exit 1
      ;;
  esac
done

wd=`pwd`
cpu=`echo $arch | sed -e 's/^\([^-]*\)-.*$/\1/'`

cver=`echo libcutl-?.*.tar.gz | sed -e "s%libcutl-\(.*\).tar.gz%\1%"`
over=`echo odb-?.*.tar.gz | sed -e "s%odb-\(.*\).tar.gz%\1%"`
mver=`echo $over | sed -e 's%\([0-9]*\.[0-9]*\).*%\1%'`

install_root="/tmp/odb-$over-$cpu-macosx"

# Using older version of GCC (newer one have trouble with old as).
#
export PATH=/$arch-4.5.1/bin:$PATH

# Clean everything up if we are rebuilding.
#
if [ $rebuild = y ]; then
  rm -rf libcutl
  rm -rf odb odb-build-d odb-build-p
  rm -rf $install_root
  rm -rf libodb
  rm -rf libodb-boost
  rm -rf libodb-qt

  if [ ! -d ../boost-binary ]; then
    error "no boost distribution in ../boost-binary"
    exit 1
  fi

  tar xfz libcutl-$cver.tar.gz
  mv libcutl-$cver libcutl

  tar xfz odb-$over.tar.gz
  mv odb-$over odb

  libodb=`echo libodb-$mver.*.tar.gz | sed -e 's%\(.*\)\.tar\.gz%\1%'`
  tar xfz $libodb.tar.gz
  mv $libodb libodb

  libodb_boost=`echo libodb-boost-$mver.*.tar.gz | sed -e 's%\(.*\)\.tar\.gz%\1%'`
  tar xfz $libodb_boost.tar.gz
  mv $libodb_boost libodb-boost

  libodb_qt=`echo libodb-qt-$mver.*.tar.gz | sed -e 's%\(.*\)\.tar\.gz%\1%'`
  tar xfz $libodb_qt.tar.gz
  mv $libodb_qt libodb-qt
fi

rm -f /tmp/odb-$over-$cpu-macosx.tar.bz2

mkdir -p odb-build-d
mkdir -p odb-build-p

# Build libcutl
#
cd libcutl

if [ $rebuild = y ]; then
  ../libcutl-configure
fi

make
cd ..

# Build odb plugin
#
cd odb-build-p

if [ $rebuild = y ]; then
  ../odb-configure p $arch $install_root
fi

make
make install-strip
cd ..

# Build odb driver
#
cd odb-build-d

if [ $rebuild = y ]; then
  ../odb-configure d $arch $install_root
fi

make -C odb odb
cp odb/odb $install_root/bin/
cd ..

# Build libodb
#
cd libodb

if [ $rebuild = y ]; then
  ../libodb-configure $install_root/lib/odb/$arch
fi

make
cd odb
make install-data
cd ../..

# Build libodb-boost
#
cd libodb-boost

if [ $rebuild = y ]; then
  ../libodb-boost-configure $install_root/lib/odb/$arch
fi

make
make install-data
cd ..

# Build libodb-qt
#
cd libodb-qt

if [ $rebuild = y ]; then
  ../libodb-qt-configure $install_root/lib/odb/$arch
fi

make
make install-data
cd ..

# Copy /$arch over to installation
#
cp -r /$arch $install_root/lib/odb/

# Copy the default options file.
#
mkdir -p $install_root/etc/odb
cp ./default.options $install_root/etc/odb/

# Copy manifest and README.
#
cp manifest $install_root/lib/odb/$arch/
cp README $install_root/


# Move doc and man out of share/
#
rm -rf $install_root/doc $install_root/man
mv $install_root/share/man $install_root/
mv $install_root/share/doc/odb $install_root/doc
rm -r $install_root/share

# Clean some things up.
#
rm -f `find $install_root -name '*.la'`

# Pack it up.
#
cd /tmp
tar cfj $wd/test/odb-$over-$cpu-macosx.tar.bz2 odb-$over-$cpu-macosx
cd $wd