#!/bin/bash

# Setup Cron Jobs for Automatic Backup
# This script sets up cron jobs for automatic backup scheduling

BACKUP_SCRIPT="/Applications/XAMPP/xamppfiles/htdocs/hueman/backup.sh"

# Check if backup script exists
if [ ! -f "$BACKUP_SCRIPT" ]; then
    echo "Error: Backup script not found at $BACKUP_SCRIPT"
    exit 1
fi

# Get current crontab
CURRENT_CRON=$(crontab -l 2>/dev/null)

# Remove existing backup cron jobs
NEW_CRON=$(echo "$CURRENT_CRON" | grep -v "backup.sh")

# Add new cron jobs
# Daily backup at 12:00 PM
NEW_CRON="$NEW_CRON
0 12 * * * $BACKUP_SCRIPT daily >> /Applications/XAMPP/xamppfiles/htdocs/hueman/backups/cron.log 2>&1"

# Weekly backup on Wednesday at 12:00 PM
NEW_CRON="$NEW_CRON
0 12 * * 3 $BACKUP_SCRIPT weekly >> /Applications/XAMPP/xamppfiles/htdocs/hueman/backups/cron.log 2>&1"

# Monthly backup on 1st of month at 12:00 PM
NEW_CRON="$NEW_CRON
0 12 1 * * $BACKUP_SCRIPT monthly >> /Applications/XAMPP/xamppfiles/htdocs/hueman/backups/cron.log 2>&1"

# Install new crontab
echo "$NEW_CRON" | crontab -

echo "✓ Cron jobs installed successfully!"
echo ""
echo "Scheduled backups:"
echo "  - Daily: 12:00 PM every day"
echo "  - Weekly: 12:00 PM every Wednesday"
echo "  - Monthly: 12:00 PM on 1st of every month"
echo ""
echo "Log file: /Applications/XAMPP/xamppfiles/htdocs/hueman/backups/cron.log"
echo ""
echo "To view current cron jobs: crontab -l"
echo "To remove all cron jobs: crontab -r"
echo "To edit cron jobs: crontab -e"
