slackpkg-pre-install.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/bash
  2. # SlackPkg Pre-install Hook for snap-slack
  3. # Place this in /etc/slackpkg/hooks/pre-install.sh
  4. # Configuration
  5. SNAP_SLACK=/usr/bin/snap-slack
  6. ENABLE_AUTO_SNAPSHOTS=1 # Set to 0 to disable automatic snapshots
  7. MAX_PACKAGE_COUNT=100 # Maximum number of packages to include in snapshot name
  8. # Check if snap-slack is installed
  9. if [ ! -x "$SNAP_SLACK" ]; then
  10. echo "WARNING: snap-slack not found at $SNAP_SLACK, skipping pre-install snapshot"
  11. exit 0
  12. fi
  13. # Check if auto snapshots are enabled
  14. if [ "$ENABLE_AUTO_SNAPSHOTS" != "1" ]; then
  15. echo "INFO: Automatic snapshots are disabled in slackpkg hook"
  16. exit 0
  17. fi
  18. # Function to create a snapshot
  19. create_snapshot() {
  20. local desc="$1"
  21. echo "Creating snapshot before package operations: $desc"
  22. $SNAP_SLACK create --description "$desc"
  23. }
  24. # Get the operation being performed
  25. OPERATION="$1"
  26. shift
  27. PACKAGES="$@"
  28. # Create appropriate snapshot based on operation
  29. case "$OPERATION" in
  30. upgrade-all)
  31. create_snapshot "pre-upgrade-all"
  32. ;;
  33. install|upgrade)
  34. # Limit the number of packages in the snapshot name for readability
  35. if [ "$(echo $PACKAGES | wc -w)" -gt $MAX_PACKAGE_COUNT ]; then
  36. PKG_COUNT=$(echo $PACKAGES | wc -w)
  37. create_snapshot "pre-$OPERATION-$PKG_COUNT-packages"
  38. else
  39. create_snapshot "pre-$OPERATION-$(echo $PACKAGES | tr ' ' '-')"
  40. fi
  41. ;;
  42. remove)
  43. if [ "$(echo $PACKAGES | wc -w)" -gt $MAX_PACKAGE_COUNT ]; then
  44. PKG_COUNT=$(echo $PACKAGES | wc -w)
  45. create_snapshot "pre-remove-$PKG_COUNT-packages"
  46. else
  47. create_snapshot "pre-remove-$(echo $PACKAGES | tr ' ' '-')"
  48. fi
  49. ;;
  50. *)
  51. # For other operations, create a generic snapshot
  52. create_snapshot "pre-slackpkg-operation"
  53. ;;
  54. esac
  55. exit 0