dotfiles/zsh/.config/zsh/maintenance.zsh
2025-04-21 12:17:14 -03:00

69 lines
1.8 KiB
Bash

#!/usr/bin/env zsh
# Configuration
MAINTENANCE_SCRIPT="$HOME/.local/bin/maintenance.sh"
TIMESTAMP_FILE="$HOME/.local/share/arch_maintenance_timestamp"
INTERVAL=7
if [ ! -f "$MAINTENANCE_SCRIPT" ]; then
echo "Maintenance script not found at $MAINTENANCE_SCRIPT"
return 0
fi
if [ -f "$TIMESTAMP_FILE" ]; then
LAST_RUN=$(cat $TIMESTAMP_FILE)
CURRENT_TIME=$(date +%s)
DAYS_DIFF=$(( (CURRENT_TIME - LAST_RUN) / 86400 ))
# Return immediately if maintenance not due
[ $DAYS_DIFF -lt $INTERVAL ] && return 0
else
# Create timestamp file if missing (first run)
echo $(($(date +%s) - ${INTERVAL}*86400 - 1)) > $TIMESTAMP_FILE
DAYS_DIFF=$INTERVAL # Set for first run
fi
# Simple output functions that work with or without gum
print_styled() {
if command -v gum &> /dev/null; then
gum style "$@"
else
echo "$1"
fi
}
ask_confirm() {
if command -v gum &> /dev/null; then
gum confirm "$1"
else
echo "$1 (y/n)"
read -k 1 REPLY
echo ""
[[ $REPLY =~ ^[Yy]$ ]]
fi
}
# Function to run maintenance script
run_maintenance() {
print_styled "Running system maintenance script..."
if sudo $MAINTENANCE_SCRIPT; then
date +%s > $TIMESTAMP_FILE
print_styled "✓ Maintenance completed successfully."
else
print_styled "✗ Maintenance script failed. Please check the output and run it manually."
fi
}
# Display maintenance notification
echo ""
print_styled "⚠️ It's been $DAYS_DIFF days since your last system maintenance"
if ask_confirm "Would you like to run maintenance now?"; then
run_maintenance
else
print_styled "Maintenance skipped. You'll be reminded next time you start your shell."
print_styled "To run manually: sudo $MAINTENANCE_SCRIPT"
fi
return 0