MediaImporter / RPI / scripts / rpi-camera-disk-handler.sh
1 contributor
59 lines | 1.433kb
#!/usr/bin/env bash

set -euo pipefail

LOG_TAG="rpi-camera-disk-handler"
APP_BIN="/usr/local/bin/rpi-camera-importer"

log_message() {
  local message="$1"
  local priority="${2:-info}"
  logger -p "local0.${priority}" -t "$LOG_TAG" "$message"
}

usage() {
  echo "Usage: $0 {attach-deferred|detach} <UUID>"
}

action="${1:-}"
uuid="${2:-}"
attach_dry_run="${RPI_CAMERA_IMPORTER_ATTACH_DRY_RUN:-0}"

if [[ -z "$action" || -z "$uuid" ]]; then
  log_message "Invalid arguments: action='$action' uuid='$uuid'" "err"
  usage
  exit 1
fi

case "$action" in
  attach-deferred)
    log_message "Attach deferred triggered for UUID=$uuid"
    if [[ ! -x "$APP_BIN" ]]; then
      log_message "CLI not found: $APP_BIN" "err"
      exit 1
    fi

    # Run import only for configured UUID; unconfigured devices exit cleanly.
    import_args=(import --uuid "$uuid")
    if [[ "$attach_dry_run" == "1" ]]; then
      import_args+=(--dry-run --verbose)
      log_message "Attach handler running in dry-run mode (RPI_CAMERA_IMPORTER_ATTACH_DRY_RUN=1)"
    fi

    if ! "$APP_BIN" "${import_args[@]}"; then
      log_message "Import flow failed for UUID=$uuid" "err"
      exit 1
    fi
    ;;
  detach)
    # No detach-side action required; importer handles mount/unmount around imports.
    log_message "Detach event for UUID=$uuid (no-op)"
    ;;
  *)
    log_message "Unsupported action: $action" "err"
    usage
    exit 1
    ;;
esac

exit 0