backpack
1 #!/usr/bin/env sh 2 3 set -eu 4 5 ACTION="${1:-}" 6 FLAG="${2:-}" 7 8 if [ -z "$ACTION" ]; then 9 echo "Error: missing argument" >&2 10 echo "Usage: $0 <action> [flags]" >&2 11 echo "" >&2 12 echo "Available actions:" >&2 13 echo " ensure Install and build all packages (without activation)" >&2 14 echo " gc [--dry-run] Remove orphaned packages no longer needed by configuration" >&2 15 exit 1 16 fi 17 18 # Determine the Backpack installation directory based on this script's location 19 # This makes the script work regardless of where it's called from 20 SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" 21 BACKPACK_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" 22 23 # Verify this is actually a Backpack installation 24 if [ ! -f "$BACKPACK_DIR/ensure.el" ] || [ ! -d "$BACKPACK_DIR/base-packages" ]; then 25 echo "Error: This script must be run from the Backpack installation directory" >&2 26 echo "Expected to find ensure.el and base-packages/ in: $BACKPACK_DIR" >&2 27 exit 1 28 fi 29 30 if [ "$ACTION" = "ensure" ]; then 31 echo "Backpack: Synchronizing packages..." 32 echo "Backpack directory: $BACKPACK_DIR" 33 echo "" 34 # Run Emacs in batch mode with ensure.el 35 # ensure.el handles loading backpack.el with sync mode enabled 36 emacs --batch \ 37 --eval "(setq user-emacs-directory \"$BACKPACK_DIR/\")" \ 38 -l "$BACKPACK_DIR/ensure.el" 39 exit $? 40 41 elif [ "$ACTION" = "gc" ]; then 42 DRY_RUN="nil" 43 if [ "$FLAG" = "--dry-run" ] || [ "$FLAG" = "-n" ]; then 44 DRY_RUN="t" 45 echo "Backpack: Garbage collection (dry run)..." 46 else 47 echo "Backpack: Garbage collection..." 48 fi 49 echo "Backpack directory: $BACKPACK_DIR" 50 echo "" 51 # Run Emacs in batch mode with gc.el 52 emacs --batch \ 53 --eval "(setq user-emacs-directory \"$BACKPACK_DIR/\")" \ 54 --eval "(setq backpack-gc-dry-run $DRY_RUN)" \ 55 -l "$BACKPACK_DIR/gc.el" 56 exit $? 57 58 else 59 echo "Error: unknown action '$ACTION'" >&2 60 echo "Available actions: ensure, gc" >&2 61 exit 1 62 fi