#!/bin/bash
set -e
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
export HOME="/root"
LOG="/opt/task-team/deploy.log"
echo "=== Deploy started at $(date -Is) ===" >> $LOG

cd /opt/task-team
git pull origin master >> $LOG 2>&1

cd /opt/task-team/api
npm install --production >> $LOG 2>&1

pm2 reload taskteam-api --force >> $LOG 2>&1

if [ -d /opt/task-team/apps/tasks ] && [ -f /opt/task-team/apps/tasks/package.json ]; then
    cd /opt/task-team/apps/tasks
    NEXT_PUBLIC_API_URL=http://localhost:3000 npm run build >> $LOG 2>&1
    pm2 reload taskteam-web --force >> $LOG 2>&1 || true
fi

echo "=== Deploy completed at $(date -Is) ===" >> $LOG
