#!/bin/sh

# This script generates rust compatible makefiles from libuv. When upgrading
# libuv, do:
# cd $RUST_DIR
# rm -r mk/libuv
# cd $LIBUV_DIR
# git archive --prefix libuv/ $REVISION > libuv.tar.gz
# cd $RUST_DIR/src/rt
# rm -r libuv
# tar -xvf $LIBUV_DIR/libuv.tar.gz
# mkdir -p libuv/build
# svn co http://gyp.googlecode.com/svn/trunk libuv/build/gyp
# cd ../..
# ./src/etc/gyp_uv

set -e

cd `dirname $0`
cd ../..

for ARCH in ia32 x86_64
do
    args="--depth . -Dlibrary=static_library -Dtarget_arch=$ARCH"
    
    ./src/libuv/build/gyp/gyp src/libuv/uv.gyp $args \
        -f make-mac \
        --generator-output mk/libuv/$ARCH/mac \
        -DOS=mac

    ./src/libuv/build/gyp/gyp src/libuv/uv.gyp $args \
        -f make-linux \
        --generator-output mk/libuv/$ARCH/unix \
        -DOS=linux

    ./src/libuv/build/gyp/gyp src/libuv/uv.gyp $args \
        -f make-linux \
        --generator-output mk/libuv/$ARCH/win \
        -DOS=win

    # Comment out the gyp auto regeneration
    for os in mac unix win; do
        sed -i \
            -e 's/^\(Makefile: $(srcdir)\/src\/rt\/libuv\/uv\.gyp\)/#\1/' \
            mk/libuv/$ARCH/$os/Makefile

        sed -i \
            -e 's/^\(	$(call do_cmd,regen_makefile)\)/#\1/' \
            mk/libuv/$ARCH/$os/Makefile
    done
done

# On Mac, GYP hardcodes a -arch i386 into the output. Fix that.
sed -i \
    -e 's/-arch i386/-arch x86_64/' \
    mk/libuv/x86_64/mac/src/libuv/*.mk
