#!/bin/sh
set -eu

for mp in $(lsblk --list --noheadings --output fstype,mountpoint | \
            awk '$1 == "btrfs" {print $2}' | \
            sort -u)
do
    # In case there are no btrfs volumes, the for loop will execute once with an
    # empty string for mp. Validate that indeed we're passed a mountpoint.
    [ -d "$mp" ] || continue
    status="$(btrfs scrub status "$mp" | awk '$1 == "Status:" {print $2}')"
    if [ "$status" = 'running' ]
    then
        echo "Scrub already running on $mp, skipping." >&2
        continue
    fi
    btrfs scrub start "$mp" >&2
done
