#!/bin/bash
# btrfs_check start <dev> <id> <options> 
# btrfs_check status <dev> <id>
# btrfs_check cancel <dev> <id>

case "$1" in
'start')
  exec /sbin/btrfs check $4 $2 &> /var/lib/btrfs/check.status.$3 &
;;
'status')
  if [ -f /var/lib/btrfs/check.status.$3 ]; then
    cat /var/lib/btrfs/check.status.$3
  else
    echo "Not available"
  fi;
  # establish retval of this script: 0 running, 1 not running
  pgrep -f "/sbin/btrfs check .*$2" >/dev/null
;;
'cancel')
  pkill -f "/sbin/btrfs_check.*$2"
  while pgrep -f "/sbin/btrfs check .*$2" >/dev/null ; do
    sleep 1
  done
  echo "Cancelled" >> /var/lib/btrfs/check.status.$3
;;
esac
