#!/bin/bash
# SPDX-License-Identifier: GPL-3.0+
# Copyright (C) 2017 Omar Sandoval
#
# scsi_debug helper functions.

_have_scsi_debug() {
	_have_modules scsi_debug
}

_init_scsi_debug() {
	if ! modprobe -r scsi_debug || ! modprobe scsi_debug "$@"; then
		return 1
	fi

	udevadm settle

	local host_sysfs host target_sysfs target
	SCSI_DEBUG_HOSTS=()
	SCSI_DEBUG_TARGETS=()
	SCSI_DEBUG_DEVICES=()
	for host_sysfs in /sys/class/scsi_host/*; do
		if [[ "$(cat "${host_sysfs}/proc_name")" = scsi_debug ]]; then
			host="${host_sysfs#/sys/class/scsi_host/host}"
			SCSI_DEBUG_HOSTS+=("$host")
			for target_sysfs in /sys/class/scsi_device/"$host":*; do
				target="${target_sysfs#/sys/class/scsi_device/}"
				SCSI_DEBUG_TARGETS+=("$target")
				SCSI_DEBUG_DEVICES+=("$(ls "$target_sysfs/device/block")")
			done
		fi
	done

	if [[ ${#SCSI_DEBUG_HOSTS[@]} -eq 0 ]]; then
		echo "Could not find scsi_debug hosts" >&2
		_exit_scsi_debug
		return 1
	fi

	if [[ ${#SCSI_DEBUG_TARGETS[@]} -eq 0 ]]; then
		echo "Could not find scsi_debug targets" >&2
		_exit_scsi_debug
		return 1
	fi

	return 0
}

_exit_scsi_debug() {
	unset SCSI_DEBUG_HOSTS
	unset SCSI_DEBUG_TARGETS
	unset SCSI_DEBUG_DEVICES
	udevadm settle
	modprobe -r scsi_debug
}
