#!/bin/sh -e

export CARGO="${CARGO:-$_defaultCARGO}"

#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_PROFILE="--release"
if [ -n "${CARGO_USE_DEV}" ]; then
    CARGO_PROFILE=""
    if [ -n "${CARGO_USE_CLIPPY}" ]; then
        $CARGO clippy --manifest-path $srcdir/Cargo.toml
    fi
fi

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

if [ -n "${CARGO_USE_DEV}" ]; then
    cp -p target/$RUSTC_TARGET_ARCH/debug/librecrust.a $builddir/rec-rust-lib/rust/librecrust.a
else
    cp -p target/$RUSTC_TARGET_ARCH/release/librecrust.a $builddir/rec-rust-lib/rust/librecrust.a
fi

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
