#!/bin/bash

# Define the path to your project
PROJECT_DIR="/var/www/html/deepbiodata.com/deepbiodata-website"

echo "=== Starting deployment update: $(date) ==="

# Navigate to project directory
cd $PROJECT_DIR || { echo "Directory not found"; exit 1; }


# 2. Actualizar código de GitHub
git fetch origin main
LOCAL=$(git rev-parse HEAD)
REMOTE=$(git rev-parse @{u})

if [ "$LOCAL" = "$REMOTE" ]; then
    echo "$(date): El código ya está actualizado."
else
    echo "$(date): Cambios detectados. Iniciando despliegue..."

# Fetch latest changes from Git
echo "Pulling latest code from Git..."
git pull origin main

# Install dependencies (using 'npm ci' is cleaner for automated scripts than 'npm install')
echo "Installing dependencies..."
npm ci

# Build the static files
echo "Compiling React application..."
npm run build

# Reset permissions so Apache can read the new files cleanly
echo "Fixing permissions for Apache..."
sudo chown -R www-data:www-data $PROJECT_DIR/build
sudo find $PROJECT_DIR/build -type d -exec chmod 755 {} \;
sudo find $PROJECT_DIR/build -type f -exec chmod 644 {} \;

echo "=== Deployment finished successfully: $(date) ==="

fi
