#!/bin/bash
# Setup FreeTDS ODBC driver for macOS

echo "🔧 Setting up FreeTDS ODBC driver on macOS..."

# Check if FreeTDS is installed
if ! brew list freetds &>/dev/null; then
    echo "❌ FreeTDS not installed. Installing..."
    brew install freetds unixodbc
else
    echo "✅ FreeTDS already installed"
fi

# Create odbcinst.ini if it doesn't exist
ODBCINST_FILE="/usr/local/etc/odbcinst.ini"

if [ ! -f "$ODBCINST_FILE" ]; then
    echo "📝 Creating $ODBCINST_FILE..."
    sudo tee "$ODBCINST_FILE" > /dev/null <<'EOF'
[FreeTDS]
Description = FreeTDS Driver for SQL Server
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsodbc.so
UsageCount = 1
FileUsage = 1

[ODBC Driver 17 for SQL Server]
Description = FreeTDS Driver for SQL Server (alias)
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsodbc.so
UsageCount = 1
FileUsage = 1
EOF
    echo "✅ Created $ODBCINST_FILE"
else
    echo "✅ $ODBCINST_FILE already exists"
fi

# Check if FreeTDS driver is registered
echo ""
echo "🔍 Checking registered ODBC drivers:"
odbcinst -q -d 2>/dev/null || echo "Note: odbcinst might show errors, but FreeTDS should work"

echo ""
echo "✅ Setup complete!"
echo ""
echo "📋 Next steps:"
echo "1. Update password in aumentum_browser_service.py (line 472)"
echo "2. Restart the API server: ./STOP_API.sh && ./START_API.sh"
echo "3. Test connection: python test_db_connection.py"

