#!/bin/bash

# Build libraries using Turbo instead of direct commands
# This leverages Turbo's caching and dependency management

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

echo -e "${CYAN}Building libraries using Turborepo...${NC}"

# Store original location
ORIGINAL_DIR=$(pwd)

# Find git root directory
GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
if [ $? -ne 0 ]; then
    echo -e "${RED}Error: Not in a git repository. Please run this script from within the stock-bot git repository.${NC}"
    exit 1
fi

cd "$GIT_ROOT"

cleanup() {
    cd "$ORIGINAL_DIR"
}

# Set up cleanup on exit
trap cleanup EXIT

# Build all libraries using turbo
echo -e "${GREEN}Building all libraries at once with Turborepo...${NC}"
npx turbo build --filter="./libs/*"

if [ $? -ne 0 ]; then
    echo -e "${RED}Failed to build libraries with Turborepo. Exiting.${NC}"
    exit 1
else
    echo -e "${GREEN}All libraries built successfully!${NC}"
fi

cd "$GIT_ROOT"
