#!/bin/bash set -e echo "===================================" echo "Web Scraper Installation Script" echo "===================================" echo "" # Check if running as root if [ "$EUID" -ne 0 ]; then echo "Error: Please run as root (use sudo)" exit 1 fi # Get the actual user who ran sudo ACTUAL_USER=${SUDO_USER:-$USER} ACTUAL_HOME=$(eval echo ~$ACTUAL_USER) # Installation directory INSTALL_DIR="/opt/webshop-scraper" SERVICE_NAME="webshop-scraper" # Get the script directory and project directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_DIR="$(dirname "$SCRIPT_DIR")" SOURCE_ENV_FILE="$PROJECT_DIR/.env" echo "Installation directory: $INSTALL_DIR" echo "Service name: $SERVICE_NAME" echo "Source directory: $PROJECT_DIR" echo "" # Check for source .env file first if [ -f "$SOURCE_ENV_FILE" ]; then echo "✓ Found .env file in source directory: $SOURCE_ENV_FILE" echo " Using configuration from source .env file" echo " All environment variables will be automatically configured" echo "" # Load the source .env to show summary source "$SOURCE_ENV_FILE" 2>/dev/null || true echo "Configuration preview:" [ -n "$API_KEY" ] && echo " ✓ API_KEY: ${API_KEY:0:8}... (configured)" [ -n "$HOST" ] && echo " ✓ HOST: $HOST" [ -n "$PORT" ] && echo " ✓ PORT: $PORT" [ -n "$QDRANT_ENABLED" ] && echo " ✓ QDRANT_ENABLED: $QDRANT_ENABLED" [ -n "$QDRANT_API_URL" ] && echo " ✓ QDRANT_API_URL: $QDRANT_API_URL" [ -n "$OPENROUTER_API_KEY" ] && echo " ✓ OPENROUTER_API_KEY: ${OPENROUTER_API_KEY:0:15}... (configured)" [ -n "$MCP_ENABLED" ] && echo " ✓ MCP_ENABLED: $MCP_ENABLED" echo "" USE_SOURCE_ENV=true SKIP_CONFIG_PROMPTS=true else echo "⚠ No .env file found in source directory" echo " Expected location: $SOURCE_ENV_FILE" echo "" echo "Please create a .env file in the project directory before running the installer." echo "You can copy .env.example and configure it:" echo " cp .env.example .env" echo " nano .env" echo "" exit 1 fi # Create installation directory echo "Creating installation directory..." mkdir -p "$INSTALL_DIR" # Check if Node.js is installed if ! command -v node &> /dev/null; then echo "Error: Node.js is not installed. Please install Node.js first." exit 1 fi # Check if npm is installed if ! command -v npm &> /dev/null; then echo "Error: npm is not installed. Please install npm first." exit 1 fi # Build application in the source directory first echo "Building application..." cd "$PROJECT_DIR" # Install all dependencies (including dev dependencies for build) echo "Installing build dependencies..." npm install # Build the application echo "Compiling TypeScript and building web assets..." npm run build # Check if build was successful if [ ! -f "$PROJECT_DIR/dist/index.js" ]; then echo "Error: Build failed - dist/index.js not found" exit 1 fi # Copy application files echo "Copying application files..." cp -r package.json "$INSTALL_DIR/" cp -r dist "$INSTALL_DIR/" # Change to installation directory cd "$INSTALL_DIR" # Install only production dependencies echo "Installing production dependencies..." npm install --production # Copy environment file from source echo "Copying environment file..." cp "$SOURCE_ENV_FILE" "$INSTALL_DIR/.env" echo "✓ Environment file copied from source to $INSTALL_DIR/.env" # Set permissions chmod 600 "$INSTALL_DIR/.env" chown -R $ACTUAL_USER:$ACTUAL_USER "$INSTALL_DIR" # Create systemd service file echo "Creating systemd service..." cat > "/etc/systemd/system/$SERVICE_NAME.service" </dev/null || true # Display appropriate URL based on host configuration DISPLAY_HOST="${HOST:-localhost}" if [ "$DISPLAY_HOST" = "0.0.0.0" ]; then DISPLAY_HOST="localhost" fi echo "Configuration loaded from: $SOURCE_ENV_FILE" echo "API is available at: http://$DISPLAY_HOST:${PORT:-3000}" echo "Health check: http://$DISPLAY_HOST:${PORT:-3000}/health" # Show Qdrant status if enabled if [ "$QDRANT_ENABLED" = "true" ]; then echo "" echo "Qdrant Integration: ENABLED" echo " Qdrant URL: $QDRANT_API_URL" echo " OpenRouter: ${OPENROUTER_API_KEY:+CONFIGURED}" echo " MCP Server: ${MCP_ENABLED}" fi echo ""