all repos

tmux-stare @ e1f53d7

session manager, but my session manager

tmux-stare/prune.sh (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
prettify prune.sh, 20 days ago
1
#!/usr/bin/env bash
2
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
3
source "$CURRENT_DIR/scripts/helpers.sh"
4
5
set -euo pipefail
6
cd "$(get_opt_dir)"
7
8
DAYS=7
9
10
CUTOFF=$(date -d "$DAYS days ago" +%Y%m%dT%H%M%S)
11
12
13
RED='\033[0;31m'
14
MAGENTA='\033[0;35m'
15
YELLOW='\033[0;33m'
16
RESET='\033[0m'
17
18
# Build set of active session names (those with a _last symlink)
19
declare -A ACTIVE
20
for l in *_last; do
21
  [ -L "$l" ] || continue
22
  ACTIVE["${l%_last}"]=1
23
done
24
25
DELETED=0
26
ORPHANS=0
27
FIXED=0
28
29
for f in *_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]T[0-9][0-9][0-9][0-9][0-9][0-9]; do
30
  [ -f "$f" ] || continue
31
  name="${f%_*}"
32
  ts="${f##*_}"
33
34
  if [[ -v ACTIVE["$name"] ]]; then
35
    # Active session: keep the _last target always, prune old others
36
    target="$(readlink "$name"_last 2>/dev/null || true)"
37
    if [ "$f" = "$target" ]; then continue; fi # always keep the _last target
38
    if [[ "$ts" < "$CUTOFF" ]]; then
39
      rm "$f"
40
      echo -e "${RED}pruned:${RESET} $f"
41
      DELETED=$((DELETED + 1))
42
    fi
43
  else
44
    # Orphaned session: delete everything
45
    rm "$f"
46
    echo -e "${MAGENTA}pruned (orphan):${RESET} $f"
47
    DELETED=$((DELETED + 1))
48
    ORPHANS=$((ORPHANS + 1))
49
  fi
50
done
51
52
# Clean dangling _last symlinks (target was deleted)
53
for l in *_last; do
54
  [ -L "$l" ] || continue
55
  [ -e "$l" ] && continue
56
  rm "$l"
57
  echo -e "${YELLOW}cleaned:${RESET} $l (broken symlink)"
58
  FIXED=$((FIXED + 1))
59
done
60
61
# Clean dangling global last symlink
62
if [ -L "last" ] && [ ! -e "last" ]; then
63
  rm "last"
64
  echo -e "${YELLOW}cleaned:${RESET} last (broken symlink)"
65
  FIXED=$((FIXED + 1))
66
fi
67
68
echo "Done. $DELETED session files pruned, $ORPHANS orphan sessions deleted, $FIXED broken symlinks cleaned."