maintenance.zsh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env zsh
  2. # Configuration
  3. MAINTENANCE_SCRIPT="$HOME/.local/bin/maintenance.sh"
  4. TIMESTAMP_FILE="$HOME/.local/share/arch_maintenance_timestamp"
  5. DISABLED_FILE="$HOME/.local/share/arch_maintenance_disabled"
  6. INTERVAL=14 # Increased from 7 to 14 days
  7. QUIET_MODE=${MAINTENANCE_QUIET:-false} # Set MAINTENANCE_QUIET=true to disable
  8. if [ ! -f "$MAINTENANCE_SCRIPT" ]; then
  9. echo "Maintenance script not found at $MAINTENANCE_SCRIPT"
  10. return 0
  11. fi
  12. # Check if maintenance is disabled
  13. if [ -f "$DISABLED_FILE" ] || [ "$QUIET_MODE" = "true" ]; then
  14. return 0
  15. fi
  16. # Only run in interactive shells (not scripts)
  17. if [[ ! -o interactive ]]; then
  18. return 0
  19. fi
  20. if [ -f "$TIMESTAMP_FILE" ]; then
  21. LAST_RUN=$(cat $TIMESTAMP_FILE)
  22. CURRENT_TIME=$(date +%s)
  23. DAYS_DIFF=$(( (CURRENT_TIME - LAST_RUN) / 86400 ))
  24. # Return immediately if maintenance not due
  25. [ $DAYS_DIFF -lt $INTERVAL ] && return 0
  26. else
  27. # Create timestamp file if missing (first run)
  28. echo $(($(date +%s) - ${INTERVAL}*86400 - 1)) > $TIMESTAMP_FILE
  29. DAYS_DIFF=$INTERVAL # Set for first run
  30. fi
  31. # Simple output functions that work with or without gum
  32. print_styled() {
  33. if command -v gum &> /dev/null; then
  34. gum style "$@"
  35. else
  36. echo "$1"
  37. fi
  38. }
  39. ask_confirm() {
  40. if command -v gum &> /dev/null; then
  41. gum confirm "$1"
  42. else
  43. echo "$1 (y/n)"
  44. read -k 1 REPLY
  45. echo ""
  46. [[ $REPLY =~ ^[Yy]$ ]]
  47. fi
  48. }
  49. # Function to run maintenance script
  50. run_maintenance() {
  51. print_styled "Running system maintenance script..."
  52. if sudo $MAINTENANCE_SCRIPT; then
  53. date +%s > $TIMESTAMP_FILE
  54. print_styled "✓ Maintenance completed successfully."
  55. else
  56. print_styled "✗ Maintenance script failed. Please check the output and run it manually."
  57. fi
  58. }
  59. # Function to snooze for 7 more days
  60. snooze_maintenance() {
  61. SNOOZE_TIME=$(($(date +%s) - (${INTERVAL} - 7) * 86400))
  62. echo $SNOOZE_TIME > $TIMESTAMP_FILE
  63. print_styled "⏰ Maintenance reminder snoozed for 7 more days."
  64. }
  65. # Function to disable maintenance permanently
  66. disable_maintenance() {
  67. touch "$DISABLED_FILE"
  68. print_styled "🔕 Maintenance reminders disabled permanently."
  69. print_styled "To re-enable: rm $DISABLED_FILE"
  70. print_styled "To run manually: sudo $MAINTENANCE_SCRIPT"
  71. }
  72. # Display maintenance notification
  73. echo ""
  74. print_styled "⚠️ It's been $DAYS_DIFF days since your last system maintenance"
  75. # Enhanced confirmation with four options
  76. if command -v gum &> /dev/null; then
  77. CHOICE=$(gum choose "Run maintenance now" "Skip this time" "Don't ask for 7 more days" "Disable permanently")
  78. case "$CHOICE" in
  79. "Run maintenance now")
  80. run_maintenance
  81. ;;
  82. "Skip this time")
  83. print_styled "Maintenance skipped. You'll be reminded next time you start your shell."
  84. print_styled "To run manually: sudo $MAINTENANCE_SCRIPT"
  85. ;;
  86. "Don't ask for 7 more days")
  87. snooze_maintenance
  88. ;;
  89. "Disable permanently")
  90. disable_maintenance
  91. ;;
  92. esac
  93. else
  94. # Fallback for systems without gum
  95. echo "Choose an option:"
  96. echo "1) Run maintenance now"
  97. echo "2) Skip this time"
  98. echo "3) Don't ask for 7 more days"
  99. echo "4) Disable permanently"
  100. echo -n "Enter your choice (1-4): "
  101. read -k 1 CHOICE
  102. echo ""
  103. case "$CHOICE" in
  104. 1)
  105. run_maintenance
  106. ;;
  107. 2)
  108. print_styled "Maintenance skipped. You'll be reminded next time you start your shell."
  109. print_styled "To run manually: sudo $MAINTENANCE_SCRIPT"
  110. ;;
  111. 3)
  112. snooze_maintenance
  113. ;;
  114. 4)
  115. disable_maintenance
  116. ;;
  117. *)
  118. print_styled "Invalid choice. Maintenance skipped."
  119. print_styled "To run manually: sudo $MAINTENANCE_SCRIPT"
  120. ;;
  121. esac
  122. fi
  123. return 0