#!/bin/bash

# Function to output text in color
color_echo() {
  local color_code=$1
  local text=$2
  echo -e "\e[${color_code}m${text}\e[0m"
}

# Ask the user for the number of days to keep logs
color_echo "36" "How many days of logs would you like to keep?"
read -p "Enter the number of days (e.g., 10): " days_to_keep

# Validate input with default value and range check
if [[ -z "$days_to_keep" ]]; then
  days_to_keep=10
  color_echo "33" "No input provided. Defaulting to 10 days."
elif ! [[ "$days_to_keep" =~ ^[0-9]+$ ]]; then
  color_echo "31" "Invalid input. Please enter a positive integer."
  exit 1
elif [[ "$days_to_keep" -lt 1 || "$days_to_keep" -gt 365 ]]; then
  color_echo "31" "Please enter a value between 1 and 365 days."
  exit 1
fi

# Confirm the operation
color_echo "36" "You chose to keep logs for $days_to_keep days. A cron job will be created to clean logs older than this."
read -p "Are you sure you want to proceed? (y/n): " confirm

if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
  color_echo "33" "Operation canceled."
  exit 0
fi

# Define the cron job with logging
log_directory="/var/log/proxmox-backup"
backup_directory="/var/log/proxmox-backup-backup"
mkdir -p "$backup_directory"
cron_command_base="/usr/bin/find $log_directory -mtime +"
cron_command_suffix=" -type f -exec mv {} $backup_directory/ \; && /usr/bin/find $backup_directory -mtime +30 -type f -exec rm {} +"
cron_command="$cron_command_base$days_to_keep$cron_command_suffix"
cron_job="0 3 * * * $cron_command"

# Check if a similar cron job exists
existing_cron=$(crontab -l 2>/dev/null | grep -F "$cron_command_base")
if [[ -n "$existing_cron" ]]; then
  existing_days=$(echo "$existing_cron" | grep -oP "\+\d+" | head -n 1 | tr -d '+')
  if [[ "$existing_days" != "$days_to_keep" ]]; then
    color_echo "33" "A cron job already exists with $existing_days days."
    read -p "Do you want to update it to $days_to_keep days? (y/n): " update_confirm
    if [[ "$update_confirm" == "y" || "$update_confirm" == "Y" ]]; then
      updated_crontab=$(crontab -l 2>/dev/null | sed "s|$existing_cron|$cron_job|")
      echo "$updated_crontab" | crontab -
      if [[ $? -eq 0 ]]; then
        color_echo "32" "Cron job updated successfully to $days_to_keep days."
      else
        color_echo "31" "Failed to update the cron job."
        exit 1
      fi
    else
      color_echo "33" "No changes made to the existing cron job."
      exit 0
    fi
  else
    color_echo "33" "A cron job with the same configuration already exists. No new job added."
    exit 0
  fi
fi

# Add the new job if no existing job was found
current_crontab=$(crontab -l 2>/dev/null | grep -vF "$cron_command_base")
echo -e "$current_crontab\n$cron_job" | sed '/^$/d' | crontab -

# Check if the cron job was added successfully
if [[ $? -eq 0 ]]; then
  color_echo "32" "Cron job created successfully to move logs older than $days_to_keep days to $backup_directory. Logs in the backup directory older than 30 days will be deleted."
else
  color_echo "31" "Failed to create the cron job. Please check if crontab is installed and accessible, and verify the syntax of the command."
  color_echo "33" "Debugging Information:"
  color_echo "33" "Cron Command: $cron_command"
  color_echo "33" "Backup Directory: $backup_directory"
  exit 1
fi

exit 0
