← back to Watches

setup-pwa.sh

217 lines

#!/bin/bash

##############################################################################
# Omega Watches PWA Setup Script
# Sets up all PWA features: service worker, push notifications, etc.
##############################################################################

set -e

echo "========================================="
echo "Omega Watches PWA Setup"
echo "========================================="
echo ""

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

# Project directory
PROJECT_DIR="/root/Projects/watches"
cd "$PROJECT_DIR"

# Step 1: Install dependencies
echo -e "${YELLOW}[1/7] Installing dependencies...${NC}"
if npm install; then
    echo -e "${GREEN}✓ Dependencies installed${NC}"
else
    echo -e "${RED}✗ Failed to install dependencies${NC}"
    exit 1
fi
echo ""

# Step 2: Generate VAPID keys
echo -e "${YELLOW}[2/7] Generating VAPID keys for push notifications...${NC}"

if ! command -v npx &> /dev/null; then
    echo -e "${RED}✗ npx not found${NC}"
    exit 1
fi

# Generate keys and save to file
VAPID_OUTPUT=$(npx web-push generate-vapid-keys)

PUBLIC_KEY=$(echo "$VAPID_OUTPUT" | grep "Public Key:" | cut -d':' -f2 | tr -d ' ')
PRIVATE_KEY=$(echo "$VAPID_OUTPUT" | grep "Private Key:" | cut -d':' -f2 | tr -d ' ')

echo "Public Key: $PUBLIC_KEY"
echo "Private Key: [HIDDEN]"

# Create or update .env file
if [ ! -f .env ]; then
    echo "Creating .env file..."
    touch .env
fi

# Add VAPID keys to .env
if grep -q "VAPID_PUBLIC_KEY" .env; then
    sed -i "s|VAPID_PUBLIC_KEY=.*|VAPID_PUBLIC_KEY=$PUBLIC_KEY|" .env
else
    echo "VAPID_PUBLIC_KEY=$PUBLIC_KEY" >> .env
fi

if grep -q "VAPID_PRIVATE_KEY" .env; then
    sed -i "s|VAPID_PRIVATE_KEY=.*|VAPID_PRIVATE_KEY=$PRIVATE_KEY|" .env
else
    echo "VAPID_PRIVATE_KEY=$PRIVATE_KEY" >> .env
fi

if grep -q "VAPID_SUBJECT" .env; then
    echo "VAPID_SUBJECT already set"
else
    echo "VAPID_SUBJECT=mailto:admin@omegawatches.example.com" >> .env
fi

echo -e "${GREEN}✓ VAPID keys generated and saved to .env${NC}"
echo ""

# Step 3: Create screenshots directory
echo -e "${YELLOW}[3/7] Creating screenshots directory...${NC}"
mkdir -p public/screenshots
echo -e "${GREEN}✓ Screenshots directory created${NC}"
echo ""

# Step 4: Create models directory for AR
echo -e "${YELLOW}[4/7] Creating AR models directory...${NC}"
mkdir -p public/models/watches
mkdir -p public/textures/watches
echo -e "${GREEN}✓ AR directories created${NC}"
echo ""

# Step 5: Update server.js with PWA routes
echo -e "${YELLOW}[5/7] Checking server.js for PWA routes...${NC}"

if grep -q "pwa-routes" server.js; then
    echo -e "${GREEN}✓ PWA routes already added to server.js${NC}"
else
    echo -e "${YELLOW}Adding PWA routes to server.js...${NC}"
    # Backup server.js
    cp server.js server.js.backup

    # Add import at top
    sed -i "/import express from 'express';/a import pwaRoutes from './pwa-routes.js';" server.js

    # Add route before catch-all
    sed -i "/app.get('\*',/i app.use('/api/pwa', pwaRoutes);\n" server.js

    echo -e "${GREEN}✓ PWA routes added (backup saved as server.js.backup)${NC}"
fi
echo ""

# Step 6: Test service worker accessibility
echo -e "${YELLOW}[6/7] Verifying PWA files...${NC}"

PWA_FILES=(
    "public/sw-advanced.js"
    "public/pwa-app.js"
    "public/camera-scanner.js"
    "public/biometric-auth.js"
    "public/ar-viewer.js"
    "public/pwa-index.html"
    "public/pwa-styles.css"
    "public/manifest.json"
    "pwa-routes.js"
    "push-notifications.js"
)

ALL_EXIST=true
for file in "${PWA_FILES[@]}"; do
    if [ -f "$file" ]; then
        echo -e "${GREEN}✓ $file${NC}"
    else
        echo -e "${RED}✗ $file - MISSING${NC}"
        ALL_EXIST=false
    fi
done

if [ "$ALL_EXIST" = false ]; then
    echo -e "${RED}Some PWA files are missing!${NC}"
    exit 1
fi
echo ""

# Step 7: Start/restart server
echo -e "${YELLOW}[7/7] Starting PWA server...${NC}"

# Check if PM2 is available
if command -v pm2 &> /dev/null; then
    echo "Using PM2..."

    # Stop existing process if running
    pm2 stop omega-watches 2>/dev/null || true
    pm2 delete omega-watches 2>/dev/null || true

    # Start with PM2
    pm2 start server.js --name omega-watches-pwa
    pm2 save

    echo -e "${GREEN}✓ Server started with PM2${NC}"
else
    echo "PM2 not found. Starting with Node.js..."
    echo "Consider installing PM2: npm install -g pm2"

    # Kill existing process on port 7600
    lsof -ti:7600 | xargs kill -9 2>/dev/null || true

    # Start server in background
    nohup node server.js > logs/pwa-server.log 2>&1 &

    echo -e "${GREEN}✓ Server started${NC}"
fi
echo ""

# Summary
echo "========================================="
echo -e "${GREEN}PWA Setup Complete!${NC}"
echo "========================================="
echo ""
echo "Access your PWA at:"
echo "  http://45.61.58.125:7600/pwa-index.html"
echo ""
echo "Features enabled:"
echo "  ✓ Service Worker (offline mode)"
echo "  ✓ Push Notifications"
echo "  ✓ Camera Scanner"
echo "  ✓ Biometric Authentication"
echo "  ✓ AR Try-On"
echo "  ✓ Add to Home Screen"
echo "  ✓ Background Sync"
echo ""
echo "Next steps:"
echo "  1. Open PWA in browser"
echo "  2. Test Add to Home Screen"
echo "  3. Enable notifications"
echo "  4. Try camera scanner"
echo "  5. Test AR try-on"
echo ""
echo "For app store deployment, see PWA_GUIDE.md"
echo ""
echo "VAPID Public Key (for frontend):"
echo "$PUBLIC_KEY"
echo ""
echo -e "${YELLOW}Important: Update pwa-app.js line 218 with your VAPID public key!${NC}"
echo ""

# Optional: Open firewall port
read -p "Open firewall port 7600? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    sudo ufw allow 7600/tcp
    echo -e "${GREEN}✓ Firewall port 7600 opened${NC}"
fi

echo ""
echo "Setup complete!"