#!/bin/bash

# Setup script for cron job to auto-publish icons crate
# This script helps configure the cron job for automated publishing

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${GREEN}Icons Crate Auto-Publish Cron Setup${NC}"
echo "======================================"
echo

# Get the absolute path to the auto-publish script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
AUTOPUBLISH_SCRIPT="$SCRIPT_DIR/auto-publish.sh"

if [[ ! -f "$AUTOPUBLISH_SCRIPT" ]]; then
    echo -e "${RED}Error: auto-publish.sh not found at $AUTOPUBLISH_SCRIPT${NC}"
    exit 1
fi

echo -e "${BLUE}Auto-publish script location: $AUTOPUBLISH_SCRIPT${NC}"
echo

# Check if script is executable
if [[ ! -x "$AUTOPUBLISH_SCRIPT" ]]; then
    echo -e "${YELLOW}Making auto-publish.sh executable...${NC}"
    chmod +x "$AUTOPUBLISH_SCRIPT"
fi

# Show current crontab
echo -e "${BLUE}Current crontab entries:${NC}"
if crontab -l 2>/dev/null; then
    echo
else
    echo "No current crontab entries found."
    echo
fi

# Cron schedule options
echo -e "${BLUE}Cron Schedule Options:${NC}"
echo "1) Daily at 9:00 AM        (0 9 * * *)"
echo "2) Weekly on Sunday 9:00 AM (0 9 * * 0)"
echo "3) Monthly on 1st at 9:00 AM (0 9 1 * *)"
echo "4) Every 2 weeks on Monday 9:00 AM (0 9 * * 1)"
echo "5) Custom schedule"
echo "6) Just show cron entry (don't install)"
echo

read -p "Select an option (1-6): " choice

case $choice in
    1)
        CRON_SCHEDULE="0 9 * * *"
        SCHEDULE_DESC="Daily at 9:00 AM"
        ;;
    2)
        CRON_SCHEDULE="0 9 * * 0"
        SCHEDULE_DESC="Weekly on Sunday at 9:00 AM"
        ;;
    3)
        CRON_SCHEDULE="0 9 1 * *"
        SCHEDULE_DESC="Monthly on the 1st at 9:00 AM"
        ;;
    4)
        CRON_SCHEDULE="0 9 * * 1/2"
        SCHEDULE_DESC="Every 2 weeks on Monday at 9:00 AM"
        ;;
    5)
        read -p "Enter custom cron schedule (e.g., '0 9 * * *'): " CRON_SCHEDULE
        SCHEDULE_DESC="Custom: $CRON_SCHEDULE"
        ;;
    6)
        CRON_SCHEDULE="0 9 * * *"
        SCHEDULE_DESC="Daily at 9:00 AM (example)"
        SHOW_ONLY=true
        ;;
    *)
        echo -e "${RED}Invalid option. Exiting.${NC}"
        exit 1
        ;;
esac

# Create the cron entry
CRON_ENTRY="$CRON_SCHEDULE cd \"$SCRIPT_DIR\" && ./auto-publish.sh >> /tmp/auto-publish-cron.log 2>&1"

echo
echo -e "${BLUE}Generated cron entry:${NC}"
echo "$CRON_ENTRY"
echo
echo -e "${BLUE}Schedule: $SCHEDULE_DESC${NC}"
echo

if [[ "$SHOW_ONLY" == "true" ]]; then
    echo -e "${YELLOW}Cron entry generated but not installed.${NC}"
    echo "To manually add this to your crontab, run:"
    echo "  crontab -e"
    echo "Then add the following line:"
    echo "  $CRON_ENTRY"
    exit 0
fi

# Confirm installation
echo -e "${YELLOW}This will add the cron job to your crontab.${NC}"
read -p "Do you want to continue? (y/N): " confirm

if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
    echo "Installation cancelled."
    exit 0
fi

# Add to crontab
echo "Adding cron job..."
(crontab -l 2>/dev/null || true; echo "$CRON_ENTRY") | crontab -

if [[ $? -eq 0 ]]; then
    echo -e "${GREEN}✓ Cron job added successfully!${NC}"
    echo
    echo -e "${BLUE}Setup complete. The cron job will:${NC}"
    echo "1. Run $SCHEDULE_DESC"
    echo "2. Increment the patch version (e.g., 0.2.8 → 0.2.9)"
    echo "3. Run cargo check --features ssr"
    echo "4. Commit and push changes to master"
    echo "5. Publish to crates.io"
    echo "6. Log all output to /tmp/auto-publish-cron.log"
    echo
    echo -e "${YELLOW}Important notes:${NC}"
    echo "• Make sure you're logged into cargo (run 'cargo login' if needed)"
    echo "• Ensure git credentials are configured for push access"
    echo "• Check the log file /tmp/auto-publish-cron.log for status"
    echo "• To remove this cron job later, run 'crontab -e' and delete the line"
    echo
    echo -e "${BLUE}To view your current crontab: crontab -l${NC}"
else
    echo -e "${RED}Failed to add cron job.${NC}"
    exit 1
fi