#!/bin/bash
# ============================================================
# cron_setup.sh — xLoyalty Clover Connector Cron Jobs
#
# USAGE:
#   1. Edit VARIABLES section below
#   2. Upload to server
#   3. chmod +x cron_setup.sh && bash cron_setup.sh
#
# OR set manually via cPanel → Cron Jobs
# ============================================================

# ─── VARIABLES — edit these ──────────────────────────────────────────────────
PHP_BIN="/usr/local/bin/ea-php81"       # find yours: which php8.1 or ls /usr/local/bin/ea-php*
APP_DIR="/home/YOURUSERNAME/clover"     # absolute path to your clover app files
LOG_DIR="/home/YOURUSERNAME/logs"       # log directory
CRON_SECRET="REPLACE_WITH_YOUR_CRON_SECRET"

# ─── Setup ───────────────────────────────────────────────────────────────────
mkdir -p "$LOG_DIR"

# ─── Build crontab entries ───────────────────────────────────────────────────

# Remove old xloyalty-clover cron entries (if any)
crontab -l 2>/dev/null | grep -v "xloyalty-clover" | crontab -

# Add new entries
(
  crontab -l 2>/dev/null
  # Sync customers every 6 hours
  echo "0 */6 * * *  ${PHP_BIN} ${APP_DIR}/cron_sync_all_merchants.php --customers-only >> ${LOG_DIR}/clover_customers.log 2>&1  # xloyalty-clover"
  # Sync orders every 30 minutes
  echo "*/30 * * * *  ${PHP_BIN} ${APP_DIR}/cron_sync_all_merchants.php --orders-only >> ${LOG_DIR}/clover_orders.log 2>&1  # xloyalty-clover"
) | crontab -

echo ""
echo "✓ Cron jobs installed:"
crontab -l | grep xloyalty-clover
echo ""
echo "Log files will be written to: ${LOG_DIR}/"
echo ""
echo "─── Manual cPanel setup ───────────────────────────────────"
echo "If the script doesn't work, add these in cPanel → Cron Jobs:"
echo ""
echo "Every 30 minutes (orders):"
echo "  ${PHP_BIN} ${APP_DIR}/cron_sync_all_merchants.php --orders-only >> ${LOG_DIR}/clover_orders.log 2>&1"
echo ""
echo "Every 6 hours (customers):"
echo "  ${PHP_BIN} ${APP_DIR}/cron_sync_all_merchants.php --customers-only >> ${LOG_DIR}/clover_customers.log 2>&1"
echo ""
echo "─── Find your PHP 8.1 binary ──────────────────────────────"
echo "Run: ls /usr/local/bin/ea-php*"
echo "     or: which php8.1"
