#!/bin/sh

# in your home directory, download and extract:
#   http://synfig.org/code/synfig-svn-checkout.tar.gz
# that makes a new directory called 'synfig'
# cd into it, and put this script in there
# also, put openexr-1.4.0a.tar.gz in there
# then make a new directory called 'patches' inside 'synfig'
# put these 3 files in there:
#   openexr-1.4.0-mingw32.patch
#   openexr-1.4.0-pkgconfig.patch
#   synfig-core-hack-libtool.patch

banner() {
    printf "\n  .------------------------------------------------------------------------\n  |  %s\n  \`------------------------------------------------------------------------\n\n" "$1"
    true
}

config() {
    SRC_DIR=$(pwd)
    INSTALL=$SRC_DIR/install
    PATCHES=$SRC_DIR/patches

    SYN_OPENEXR_VERSION=1.4.0
    SYN_OPENEXR_SRC=$SRC_DIR/openexr-${SYN_OPENEXR_VERSION}a.tar.gz

    SYN_IMAGEMAGICK_PATH="/C/Program Files/ImageMagick"
    SYN_SUBVERSION_PATH="/C/Program Files/Subversion/bin"
    SYN_NSIS_PATH="/C/Program Files/NSIS"

    MINGW_HOST=mingw32
    SYN_CORE_DEBUG_BUILD=yes
    SYN_STUDIO_DEBUG_BUILD=yes
    CPUS=2

    if [ "$GTK_BASEPATH" ];     then CPPFLAGS="-I${GTK_BASEPATH}/include $CPPFLAGS"; fi
    if [ "$SYN_IMAGEMAGICK_PATH" ]; then PATH="${SYN_IMAGEMAGICK_PATH}:${PATH}"    ; fi
    if [ "$SYN_SUBVERSION_PATH" ];  then PATH="${SYN_SUBVERSION_PATH}:${PATH}"     ; fi
    if [ "$SYN_NSIS_PATH" ];        then PATH="${SYN_NSIS_PATH}:${PATH}"           ; fi
    export CPPFLAGS

    export PATH="$INSTALL/bin:$PATH"
    export PKG_CONFIG_PATH="$INSTALL/lib/pkgconfig:/C/GTK/lib/pkgconfig"
}

config

openexr() {
    if [ -f $INSTALL/lib/pkgconfig/OpenEXR.pc ]; then
	banner "OpenEXR has already been made"
	return
    fi

    banner "Making OpenEXR" &&
    cd $SRC_DIR &&
    banner "Extracting sources" &&
    tar xfz $SYN_OPENEXR_SRC &&
    cd  ./openexr-$SYN_OPENEXR_VERSION &&
    banner "Applying patches..." &&
    patch -p1 < $PATCHES/openexr-1.4.0-pkgconfig.patch &&
    patch -p1 < $PATCHES/openexr-1.4.0-mingw32.patch &&
    banner "Configuring" &&
    ./configure --host=$MINGW_HOST --prefix=$INSTALL --disable-static --disable-threading --disable-posix-sem &&
    banner "Making" &&
    make -j $CPUS install &&
    banner "Done: OpenEXR"
}

etl() {
    if [ -f $INSTALL/lib/pkgconfig/ETL.pc ]; then
	banner "ETL has already been made"
	return
    fi

    banner "Making ETL" &&
    cd $SRC_DIR/ETL &&
    sed -e "s!^includedir=.*!includedir=$(pwd)!" ETL.pc.in > ETL.pc.in.tmp &&
    mv ETL.pc.in.tmp ETL.pc.in &&
    banner "Autoreconfing ETL" &&
    autoreconf -if &&
    banner "Configuring ETL" &&
    ./configure --host=$MINGW_HOST --prefix=$INSTALL &&
    banner "Making ETL" &&
    make -j $CPUS install &&
    banner "Done: ETL"
}

core_config() {
    if [ "$configure" != "yes" ]; then
	banner "Skipping Configure for Core"
	return
    fi

    if [ "$SYN_CORE_DEBUG_BUILD" == "yes" ]; then
	flags="--disable-optimization  --enable-debug"
    else
	flags="--enable-optimization=1 --disable-debug"
    fi

    cd $SRC_DIR/synfig-core &&
    banner "Making Core" &&
    sed -e "s!^libdir=.*!libdir=$(pwd)/src/synfig!" \
	-e "s!^includedir=.*!includedir=$(pwd)/src!" synfig.pc.in > synfig.pc.in.tmp &&
    mv synfig.pc.in.tmp synfig.pc.in &&
    banner "Libtoolizing Core" &&
    libtoolize --ltdl --copy -f &&
    banner "Autoreconfing Core" &&
    autoreconf -if
    banner "Configuring Core" &&
    ./configure --host=$MINGW_HOST --prefix=$INSTALL $flags &&
    banner "Patching Libtool for Core" &&
    patch -p2 < $PATCHES/synfig-core-hack-libtool.patch
}

core_make_package() {
    cd $SRC_DIR/synfig-core &&
    banner "Making Package for Core" &&
    make -j $CPUS package &&
    mv ./synfig-*.exe ${SRC_DIR}/..
}

core_make_install() {
    cd $SRC_DIR/synfig-core &&
    banner "Making Install for Core" &&
    make -j $CPUS install
}

studio_config() {
    if [ "$configure" != "yes" ]; then
	banner "Skipping Configure for Studio"
	return
    fi

    if [ "$SYN_STUDIO_DEBUG_BUILD" == "yes" ]; then
	flags="--disable-optimization  --enable-debug"
    else
	flags="--enable-optimization=1 --disable-debug"
    fi

    cd $SRC_DIR/synfig-studio &&
    banner "Making Studio" &&
    banner "Autoreconfing Studio" &&
    autoreconf -if &&
    banner "Configuring Studio" &&
    ./configure --host=$MINGW_HOST --prefix=$INSTALL $flags
}

studio_make_package() {
    cd $SRC_DIR/synfig-studio &&
    banner "Making Package for Studio" &&
    make -j $CPUS package &&
    mv ./synfigstudio-*.exe ${SRC_DIR}/..
}

studio_make_install() {
    cd $SRC_DIR/synfig-studio &&
    banner "Making Install for Studio" &&
    make -j $CPUS install
}

core() {
    core_config &&
    core_make_install &&
    banner "Done: Core"
}

studio() {
    studio_config &&
    studio_make_install &&
    banner "Done: Studio"
}

package() {
    core_make_package &&
    studio_make_package
}

main() {
    # configure=yes
    openexr && etl && core && studio
    # package
}

main
