#!/bin/bash

# Build a standalone distributable package for DICOM MCP Server (Linux)
# Uses python-build-standalone to bundle Python 3.12 + dependencies

set -e

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

echo -e "${GREEN}=== Building Standalone DICOM MCP Package (Linux) ===${NC}\n"

# Detect architecture
ARCH=$(uname -m)
RELEASE="20260203"
if [ "$ARCH" = "x86_64" ]; then
    PYTHON_BUILD="cpython-3.12.12+${RELEASE}-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz"
    DIST_ARCH="x86_64"
elif [ "$ARCH" = "aarch64" ]; then
    PYTHON_BUILD="cpython-3.12.12+${RELEASE}-aarch64-unknown-linux-gnu-install_only_stripped.tar.gz"
    DIST_ARCH="aarch64"
else
    echo -e "${RED}Unsupported architecture: $ARCH${NC}"
    exit 1
fi

TEMP_BUILD_DIR="build/temp"
DIST_DIR="build/dicom_mcp_standalone_linux_${DIST_ARCH}"
PYTHON_URL="https://github.com/indygreg/python-build-standalone/releases/download/${RELEASE}/$PYTHON_BUILD"

# Clean previous builds
rm -rf "build" "dist"
mkdir -p "$TEMP_BUILD_DIR" "$DIST_DIR"

# Download standalone Python
echo -e "${YELLOW}Downloading Python 3.12 standalone build for Linux ${ARCH}...${NC}"
curl -L "$PYTHON_URL" -o "$TEMP_BUILD_DIR/python.tar.gz"

# Extract Python
echo -e "${YELLOW}Extracting Python...${NC}"
tar -xzf "$TEMP_BUILD_DIR/python.tar.gz" -C "$TEMP_BUILD_DIR"
mv "$TEMP_BUILD_DIR/python" "$DIST_DIR/python"

# Set up Python environment
PYTHON_BIN="$DIST_DIR/python/bin/python3"

# Install dependencies
echo -e "${YELLOW}Installing dependencies...${NC}"
"$PYTHON_BIN" -m pip install --upgrade pip
"$PYTHON_BIN" -m pip install fastmcp==2.0.0 pydicom==3.0.1 numpy==2.4.2 Pillow==12.1.1

# Copy server code
echo -e "${YELLOW}Copying server code...${NC}"
cp -r dicom_mcp/ "$DIST_DIR/dicom_mcp/"
cp dicom_mcp.py "$DIST_DIR/"

# Create launcher script
echo -e "${YELLOW}Creating launcher script...${NC}"
cat > "$DIST_DIR/run_dicom_mcp.sh" << 'EOF'
#!/bin/bash
# DICOM MCP Server Launcher
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"
exec "$SCRIPT_DIR/python/bin/python3" -m dicom_mcp
EOF
chmod +x "$DIST_DIR/run_dicom_mcp.sh"

# Create installation instructions
cat > "$DIST_DIR/INSTALL.txt" << EOF
DICOM MCP Server - Installation Instructions (Linux)

1. Move this entire folder to your preferred location, e.g.:
   /opt/dicom_mcp_standalone_linux_${DIST_ARCH}

2. Edit your MCP client configuration file to add this server.

   For Claude Desktop on Linux (~/.config/Claude/claude_desktop_config.json):

   {
     "mcpServers": {
       "dicom_mcp": {
         "command": "/path/to/dicom_mcp_standalone_linux_${DIST_ARCH}/run_dicom_mcp.sh"
       }
     }
   }

   (Replace /path/to/ with the actual location where you placed this folder)

3. Restart your MCP client

4. Verify the server is working by asking Claude:
   "List available MCP tools"

You should see 12 DICOM tools available.

No Python installation required - everything is bundled!
EOF

# Create a simple installer script for end users
cat > "$DIST_DIR/install_to_claude.sh" << 'EOF'
#!/bin/bash

set -e

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
CONFIG_FILE="$HOME/.config/Claude/claude_desktop_config.json"

echo -e "${GREEN}=== DICOM MCP Server - Claude Desktop Setup (Linux) ===${NC}\n"

# Create config directory if needed
mkdir -p "$HOME/.config/Claude"

# Check if config exists
if [ -f "$CONFIG_FILE" ]; then
    echo -e "${YELLOW}Existing configuration found.${NC}"
    echo -e "Please add the following to your mcpServers section:\n"
    echo '{
  "dicom_mcp": {
    "command": "'"$SCRIPT_DIR/run_dicom_mcp.sh"'"
  }
}'
    echo -e "\n${YELLOW}Configuration file location:${NC}"
    echo "$CONFIG_FILE"
else
    echo -e "${YELLOW}Creating new configuration...${NC}"
    cat > "$CONFIG_FILE" << CONFIGEOF
{
  "mcpServers": {
    "dicom_mcp": {
      "command": "$SCRIPT_DIR/run_dicom_mcp.sh"
    }
  }
}
CONFIGEOF
    echo -e "${GREEN}✓ Configuration created${NC}"
fi

echo -e "\n${GREEN}Next step: Restart Claude Desktop${NC}\n"
EOF
chmod +x "$DIST_DIR/install_to_claude.sh"

# Create tarball for distribution
echo -e "${YELLOW}Creating distribution package...${NC}"
mkdir -p dist
tar -czf "dist/dicom_mcp_standalone_linux_${DIST_ARCH}.tar.gz" -C build "dicom_mcp_standalone_linux_${DIST_ARCH}"

# Clean up build directory
rm -rf build

echo -e "\n${GREEN}=== Build Complete ===${NC}\n"
echo -e "Distribution package created at:"
echo -e "${YELLOW}dist/dicom_mcp_standalone_linux_${DIST_ARCH}.tar.gz${NC}\n"
echo -e "Package size: $(du -h "dist/dicom_mcp_standalone_linux_${DIST_ARCH}.tar.gz" | cut -f1)\n"
echo -e "To distribute:"
echo -e "1. Send the .tar.gz file to users"
echo -e "2. Users extract it: ${YELLOW}tar -xzf dicom_mcp_standalone_linux_${DIST_ARCH}.tar.gz${NC}"
echo -e "3. Users run: ${YELLOW}cd dicom_mcp_standalone_linux_${DIST_ARCH} && ./install_to_claude.sh${NC}\n"
