#!/bin/sh -e

#echo "PWD=$PWD"
#echo "srcdir=$srcdir"
#echo "builddir=$builddir"
# cxx/cxxbridge generate the files lib.rs.h and cxx.h inside $CARGO_TARGET_DIR if it is set
# otherwise it uses target
#echo "CARGO_TARGET_DIR=${CARGO_TARGET_DIR}"
mytarget=${CARGO_TARGET_DIR-target}
#echo "mytarget=${mytarget}"

$CARGO build --release $RUST_TARGET --target-dir=$builddir/target --manifest-path $srcdir/Cargo.toml

cp -p target/$RUSTC_TARGET_ARCH/release/librecrust.a $builddir/rec-rust-lib/rust/librecrust.a

cp -p $mytarget/$RUSTC_TARGET_ARCH/cxxbridge/recrust/src/lib.rs.h $srcdir/lib.rs.h
cp -p $mytarget/$RUSTC_TARGET_ARCH/cxxbridge/recrust/src/lib.rs.h $builddir/rec-rust-lib/rust/lib.rs.h
cp -p $mytarget/$RUSTC_TARGET_ARCH/cxxbridge/rust/cxx.h $srcdir/cxx.h
cp -p $mytarget/$RUSTC_TARGET_ARCH/cxxbridge/rust/cxx.h $builddir/rec-rust-lib/rust/cxx.h
cp -p $mytarget/$RUSTC_TARGET_ARCH/cxxbridge/recrust/src/web.rs.h $srcdir/web.rs.h
cp -p $mytarget/$RUSTC_TARGET_ARCH/cxxbridge/recrust/src/web.rs.h $builddir/rec-rust-lib/rust/web.rs.h
cp -p $mytarget/$RUSTC_TARGET_ARCH/cxxbridge/recrust/src/misc.rs.h $srcdir/misc.rs.h
cp -p $mytarget/$RUSTC_TARGET_ARCH/cxxbridge/recrust/src/misc.rs.h $builddir/rec-rust-lib/rust/misc.rs.h
