#!/bin/bash

# Setup script for Sender ID API cron jobs
# This script helps set up cron jobs for checking sender ID status changes

# Get the absolute path to the script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Define log directory and create it if it doesn't exist
LOG_DIR="$SCRIPT_DIR/logs"
mkdir -p "$LOG_DIR"

# Define the cron job command
CRON_JOB="*/5 * * * * php $SCRIPT_DIR/crons/status_check.php >> $LOG_DIR/cron.log 2>&1"

# Check if crontab exists for current user
if ! command -v crontab &> /dev/null; then
    echo "Error: crontab command not found. Please make sure it's installed."
    exit 1
fi

# Check if the cron job already exists
if crontab -l | grep -q "$SCRIPT_DIR/crons/status_check.php"; then
    echo "Cron job already exists. Skipping addition."
else
    # Add the cron job
    (crontab -l 2>/dev/null; echo "$CRON_JOB") | crontab -
    echo "Cron job added successfully."
fi

echo "Cron setup completed."
echo "Cron job: $CRON_JOB"

# Set proper permissions for the cron script
chmod +x "$SCRIPT_DIR/crons/status_check.php"
echo "Permissions set for cron script."

echo "Setup complete!" 