#!/bin/bash

set -e -x
PATH=/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin
PATH=/tmp/cpnt/CinePaint.app/Contents/Resources/bin:$PATH
export PATH

ARCH=i386

#ARCH=x86_64
#export CFLAGS="-arch x86_64"
#export CXXFLAGS="-arch x86_64"
#export LDFLAGS="-arch x86_64"

rm -Rf /tmp/cpnt              
mkdir -p /tmp/cpnt
chmod a+w /tmp/cpnt

cd /tmp/cpnt
tar xvfj $OLDPWD/CinePaint.app.tar.bz2
cd -

mkdir build
cd build
tar xfz ../cinepaint-0.22-1.tar.gz

tar xfz ../pkg-config-0.23.tar.gz
cd pkg-config-0.23
./configure --prefix=/tmp/cpnt/CinePaint.app/Contents/Resources
make
make install
cd ..

tar xfz ../glib-1.2.10.tar.gz
cd glib-1.2.10
patch -p1 -s < ../../glib-1.2.10.patch
cp ../cinepaint-0.22-1/config.guess .
cp ../cinepaint-0.22-1/config.sub .
./configure --prefix=/tmp/cpnt/CinePaint.app/Contents/Resources
make 
make install 
cd .. 

tar xfz ../gtk+-1.2.10.tar.gz
cd gtk+-1.2.10
cp ../cinepaint-0.22-1/config.guess .
cp ../cinepaint-0.22-1/config.sub .
./configure --prefix=/tmp/cpnt/CinePaint.app/Contents/Resources
make 
make install 
cd .. 

tar xfz ../zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure --prefix=/tmp/cpnt/CinePaint.app/Contents/Resources
make 
make install 
cd .. 

tar xfz ../libpng-1.2.40.tar.gz
cd libpng-1.2.40
./configure --prefix=/tmp/cpnt/CinePaint.app/Contents/Resources
make 
make install 
cd .. 

tar xfz ../jpegsrc.v7.tar.gz
cd jpeg-7
./configure --prefix=/tmp/cpnt/CinePaint.app/Contents/Resources \
  --enable-shared
make 
make install 
cd .. 

tar xfz ../tiff-3.8.2.tar.gz
cd tiff-3.8.2
./configure --prefix=/tmp/cpnt/CinePaint.app/Contents/Resources \
  --enable-shared \
  --with-jpeg-lib-dir=/tmp/cpnt/CinePaint.app/Contents/Resources/lib \
  --with-jpeg-include-dir=/tmp/cpnt/CinePaint.app/Contents/Resources/include
make 
make install 
cd .. 

tar xfz ../lcms-1.19.tar.gz
cd lcms-1.19
./configure --prefix=/tmp/cpnt/CinePaint.app/Contents/Resources
make 
make install 
cd .. 

tar xfj ../fltk-1.1.9-source.tar.bz2
cd fltk-1.1.9
./configure --prefix=/tmp/cpnt/CinePaint.app/Contents/Resources \
  --enable-threads
make 
make install 
cd .. 

tar xfz ../ilmbase-1.0.1.tar.gz
cd ilmbase-1.0.1
./configure --prefix=/tmp/cpnt/CinePaint.app/Contents/Resources
make 
make install 
cd .. 

tar xfz ../openexr-1.6.1.tar.gz
cd openexr-1.6.1
./configure --prefix=/tmp/cpnt/CinePaint.app/Contents/Resources
make 
make install 
cd .. 

tar xfz ../gettext-0.17.tar.gz
cd gettext-0.17
./configure --prefix=/tmp/cpnt/CinePaint.app/Contents/Resources
make 
make install 
cd .. 

cd cinepaint-0.22-1
patch -p1 -s < ../../cinepaint-0.22-1.patch
aclocal -I /tmp/cpnt/CinePaint.app/Contents/Resources/share/aclocal
automake
autoconf
./configure --prefix=/tmp/cpnt/CinePaint.app/Contents/Resources
cp ../libpng-1.2.40/libtool .
make 
chmod a+x install-sh
make install 
cd .. 

cp ../README.txt /tmp/cpnt
cd /tmp
hdiutil create -volname CinePaint -srcfolder cpnt \
  $OLDPWD/../CinePaint-$ARCH.dmg
