#!/bin/bash

# Script để build và chạy C# example

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
BUILD_DIR="$PROJECT_ROOT/build"
LIB_DIR="$BUILD_DIR"
EXAMPLE_DIR="$SCRIPT_DIR"

echo "=========================================="
echo "Building and Running C# Navigation Example"
echo "=========================================="
echo ""
echo "📁 Paths:"
echo "   SCRIPT_DIR:    $SCRIPT_DIR"
echo "   PROJECT_ROOT:   $PROJECT_ROOT"
echo "   BUILD_DIR:     $BUILD_DIR"
echo "   LIB_DIR:       $LIB_DIR"
echo "   EXAMPLE_DIR:   $EXAMPLE_DIR"
echo ""


echo "Building C API library..."
cd "$BUILD_DIR"
cmake ..
make 
sudo make install
echo "Library built successfully!"


# Kiểm tra dotnet có được cài đặt không
if ! command -v dotnet &> /dev/null; then
    echo "=========================================="
    echo "WARNING: .NET SDK not found!"
    echo "=========================================="
    echo "To run the C# example, please install .NET SDK:"
    echo ""
    echo "  Ubuntu/Debian:"
    echo "    wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh"
    echo "    chmod +x dotnet-install.sh"
    echo "    ./dotnet-install.sh --channel 6.0"
    echo "    export PATH=\$PATH:\$HOME/.dotnet"
    echo ""
    echo "  Or visit: https://dotnet.microsoft.com/download"
    echo ""
    echo "Skipping C# example. Library is available at:"
    echo "  $LIB_DIR/libnav_c_api.so"
    echo "=========================================="
    exit 0
fi

# Bước 2: Tạo C# project nếu chưa có
if [ ! -d "$EXAMPLE_DIR/NavigationExample" ]; then
    echo "Creating C# project..."
    cd "$EXAMPLE_DIR"
    dotnet new console -n NavigationExample
    cd NavigationExample
    
    # Tạo .csproj file
    cat > NavigationExample.csproj << 'EOF'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>
  <ItemGroup>
    <None Include="libnav_c_api.so">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>
EOF
    
    echo "C# project created!"
else
    echo "C# project already exists"
fi

# Luôn copy source code mới nhất (cập nhật file nếu đã có)
cd "$EXAMPLE_DIR/NavigationExample"

# # Bước 3: Copy library
# echo "Copying library..."
# cp "$LIB_DIR/libnav_c_api.so" .

# Bước 4: Set LD_LIBRARY_PATH để tìm được tất cả dependencies
# Main build directory (contains libtf3.so, librobot_cpp.so, etc.)
export LD_LIBRARY_PATH="$BUILD_DIR:$LD_LIBRARY_PATH"
# Library directories for dependencies
export LD_LIBRARY_PATH="$BUILD_DIR/src/Libraries/robot_time:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="$BUILD_DIR/src/Libraries/xmlrpcpp:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="$BUILD_DIR/src/Libraries/robot_costmap_2d:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="$BUILD_DIR/src/Libraries/geometry2/robot_tf3_geometry_msgs:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="$BUILD_DIR/src/Libraries/geometry2/robot_tf3_sensor_msgs:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="$BUILD_DIR/src/Navigations/Packages/move_base:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="$BUILD_DIR/src/Navigations/Cores/move_base_core:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="$BUILD_DIR/src/Navigations/Libraries/tf3:$LD_LIBRARY_PATH"
# Also add the example directory where the library is copied
export LD_LIBRARY_PATH="$EXAMPLE_DIR/NavigationExample:$LD_LIBRARY_PATH"
# Add the output directory where .NET places the library
export LD_LIBRARY_PATH="$EXAMPLE_DIR/NavigationExample/bin/Debug/net6.0/linux-x64:$LD_LIBRARY_PATH"

# Bước 5: Build và chạy
echo "Building C# project..."
dotnet build

echo ""
echo "Running example..."
echo "=========================================="
echo "LD_LIBRARY_PATH includes:"
echo "$LD_LIBRARY_PATH" | tr ':' '\n' | head -5
echo "..."
echo "=========================================="
dotnet run

echo "=========================================="
echo "Done!"

