aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/randombg
blob: e894a095615d3b3b4663b746277d856f5c2bd6e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/sh
killall swaybg
PIDFILE="/tmp/randombg.pid"
# Check if the PID file exists and if the process is running
if [ -e "$PIDFILE" ] && kill -0 "$(bat "$PIDFILE")"; then
  echo "Another instance of the script is already running."
  kill -9 "$(bat $PIDFILE)"  # Forcefully kill the old process
fi
echo $$ > "$PIDFILE"

cleanup() {
  [ -n "$OLD_PID" ] && kill "$OLD_PID" 2>/dev/null
  rm -f "$PIDFILE"
  exit 0
}
trap cleanup INT TERM EXIT

sleep_interruptible() {
  t=$1
  while [ "$t" -gt 0 ]; do
    sleep 1 || return
    t=$((t-1))
  done
}

swaybg -i "$(fd . /mnt/ssd/papes -t f | shuf -n1)" -m fill &
OLD_PID=$!
while true; do
  sleep_interruptible 300
  swaybg -i "$(fd . /mnt/ssd/papes -t f | shuf -n1)" -m fill &
  NEXT_PID=$!
  sleep_interruptible 5
  kill $OLD_PID
  OLD_PID=$NEXT_PID
done