%define kmod_name scst
# kversion: Kernel version as it appears under /lib/modules.
# The algorithm for setting the variable kversion is as follows:
# - If the variable kversion has been set, use its value.
# - Otherwise use the version number of the running kernel.
%{!?kversion:%define kversion %{expand:%%(uname -r)}}
%{echo:kversion=%{kversion}
}
# kernel_rpm: Name of the kernel RPM if the kernel is available as an RPM.
# krpmver: Version of the kernel RPM. Not necessarily identical to kversion.
%if %{expand:%%(rpm --quiet -qf /lib/modules/%{kversion}/kernel/arch 2>/dev/null;
		echo $((1-$?)))}
%define kernel_rpm %{expand:%%(
	    rpm -q --qf '%%%%{name}\\n' -f \\
		/lib/modules/%%{kversion}/kernel/arch | uniq
	)}
%define krpmver %{expand:%%(
	    rpm -q --qf '%%%%{version}-%%%%{release}\\n' -f \\
		/lib/modules/%%{kversion}/kernel/arch | uniq
	)}
%else
%if %{expand:%%(rpm --quiet -q kernel-headers; echo $((1-$?)))}
%define kernel_rpm kernel
%define krpmver %{expand:%%(
	    rpm -q --qf '%%%%{BUILDTIME} %%%%{version}-%%%%{release}\\n' \\
		kernel-headers | sort | tail -n1 | { read a b; echo $b; }
	)}
%endif
%endif
%{?kernel_rpm:
%{echo:kernel_rpm=%{kernel_rpm}
}
%{echo:krpmver=%{krpmver}
}}
# kernel_devel_rpm: Name of the kernel development RPM.
%if 0%{?kernel_rpm:1}
%if %([ %{kernel_rpm} = kernel-plus-core ]; echo $((1-$?)))
# CentOSPlus
%define kernel_devel_rpm kernel-plus-devel
%else
%if %([ %{kernel_rpm} = kernel-ml-core ]; echo $((1-$?)))
# RHEL/CentOS elrepo mainline
%define kernel_devel_rpm kernel-ml-devel
%else
%if %([ %{kernel_rpm} = kernel-core ]; echo $((1-$?)))
# Fedora
%define kernel_devel_rpm kernel-devel
%else
%if %([ %{kernel_rpm} = kernel-modules-core ]; echo $((1-$?)))
# CentOS Stream 9
%define kernel_devel_rpm kernel-devel
%else
%if %([ %{kernel_rpm} = kernel-uek-core ]; echo $((1-$?)))
# UEK 7
%define kernel_devel_rpm kernel-uek-devel
%else
# Other Linux distros
%define kernel_devel_rpm %{kernel_rpm}-devel
%endif
%endif
%endif
%endif
%endif
%{echo:kernel_devel_rpm=%{kernel_devel_rpm}
}
%endif
# Version of the RPM that is being built.
%define rpm_version @rpm_version@
# Make command with or without flags.
%define make %{expand:%%(echo ${MAKE:-make})}
%define pkgrel 1
%define dkms_version %{rpm_version}-%{pkgrel}%{?dist}

Name:		%{kmod_name}-dkms
Version:	%{rpm_version}
Release:	%{pkgrel}%{?dist}
Summary:	SCST mid-layer kernel drivers
Group:		System/Kernel
License:	GPLv2
Vendor:		http://scst.sourceforge.net/
URL:		http://scst.sourceforge.net/
# Unfortunately the Red Hat / CentOS kernel-debug-devel RPM provides
# kernel-devel so a workaround is needed to match the kernel-devel RPM.
BuildRequires:	%{?kernel_rpm:%{kernel_rpm} = %{krpmver} %{kernel_devel_rpm} = %{krpmver}} gcc make

Source:		%{kmod_name}-%{version}.tar.bz2
BuildRoot:	%{_tmppath}/%{name}-%{version}-build
AutoReqProv:	no

%description
A generic SCSI target subsystem for Linux that allows to convert any Linux
server into a sophisticated storage server. The three layers in SCST are the
target driver layer; the SCSI target core and the device handler layer. SCST
target drivers realize communication with an initiator and implement a storage
protocol like iSCSI, FC or SRP. SCST device handlers implement a SCSI
interface on top of local storage. Examples of such local storage are SCSI
RAID controller (dev_disk handler), block device (vdisk_blockio handler), file
(vdisk_fileio handler) or custom block device behavior implemented in user
space (scst_user).

Authors:
--------
    Vladislav Bolkhovitin, Bart Van Assche and others

%package devel
Summary:	SCST mid-layer kernel driver development package
Group:		Development/Kernel
BuildArch:	noarch

%description devel
A generic SCSI target subsystem for Linux (SCST) that allows to convert
any Linux server into a sophisticated storage server. SCST target drivers
implement protocols like iSCSI, FC or SRP. SCST device handlers either
provide access to a local SCSI RAID controller (dev_disk), block device
(vdisk_blockio), file (vdisk_fileio) or custom block device behavior
implemented in user space (scst_user).

Authors:
--------
    Vladislav Bolkhovitin, Bart Van Assche and others

%package userspace
Summary:	SCST mid-layer user space software
Group:		Development/Kernel
Requires:	scst-dkms

%description userspace
A generic SCSI target subsystem for Linux that allows to convert any Linux
server into a sophisticated storage server. The three layers in SCST are the
target driver layer; the SCSI target core and the device handler layer. SCST
target drivers realize communication with an initiator and implement a storage
protocol like iSCSI, FC or SRP. SCST device handlers implement a SCSI
interface on top of local storage. Examples of such local storage are SCSI
RAID controller (dev_disk handler), block device (vdisk_blockio handler), file
(vdisk_fileio handler) or custom block device behavior implemented in user
space (scst_user).

%prep

%setup -q -n %{kmod_name}-%{version}

%build
export KVER=%{kversion} PREFIX=%{_prefix}
export BUILD_2X_MODULE=y CONFIG_SCSI_QLA_FC=y CONFIG_SCSI_QLA2XXX_TARGET=y
make %{pkg_build_mode}
for d in scst fcst iscsi-scst qla2x00t-32gbit/qla2x00-target scst_local srpt; do
    %{make} -C $d
done

%install
export KVER=%{kversion} PREFIX=%{_prefix} MANDIR=%{_mandir} DEPMOD=true
export BUILD_2X_MODULE=y CONFIG_SCSI_QLA_FC=y CONFIG_SCSI_QLA2XXX_TARGET=y
for d in scst; do
    DESTDIR=%{buildroot} %{make} -C $d install
done
for d in fcst iscsi-scst qla2x00t-32gbit/qla2x00-target scst_local srpt; do
    DESTDIR=%{buildroot} INSTALL_MOD_PATH=%{buildroot} %{make} -C $d install
done
rm -f %{buildroot}/lib/modules/%{kversion}/[Mm]odule*
rm -rf %{buildroot}/lib/modules/%{kversion}/extra

install -d -m 755 %{buildroot}/usr/src/%{kmod_name}-%{dkms_version}
(
    cd %{buildroot}/usr/src/%{kmod_name}-%{dkms_version} &&
    tar --strip-components=1 -xaf %{SOURCE0}
)
cat >%{buildroot}/usr/src/%{kmod_name}-%{dkms_version}/dkms.conf <<"EOF"
PACKAGE_VERSION="%{dkms_version}"
PACKAGE_NAME="%{kmod_name}"
AUTOINSTALL=yes
MAKE[0]="export KVER=${kernelver} KDIR=${kernel_source_dir} BUILD_2X_MODULE=y CONFIG_SCSI_QLA_FC=y CONFIG_SCSI_QLA2XXX_TARGET=y && make %{pkg_build_mode} && make -sC scst && make -sC fcst && make -sC iscsi-scst && make -sC qla2x00t-32gbit/qla2x00-target && make -sC scst_local && make -sC srpt && cp */*.ko */*/*.ko *scst*/*/*/*.ko ."
CLEAN="make clean"
# Remove any existing ib_srpt.ko kernel modules
PRE_INSTALL="find /lib/modules/${kernelver} -name ib_srpt.ko -exec rm {} \;"
BUILT_MODULE_NAME[ 0]="fcst"
BUILT_MODULE_NAME[ 1]="ib_srpt"
BUILT_MODULE_NAME[ 2]="iscsi-scst"
BUILT_MODULE_NAME[ 3]="qla2x00tgt"
BUILT_MODULE_NAME[ 4]="qla2xxx_scst"
BUILT_MODULE_NAME[ 5]="scst"
BUILT_MODULE_NAME[ 6]="scst_local"
BUILT_MODULE_NAME[ 7]="scst_cdrom"
BUILT_MODULE_NAME[ 8]="scst_changer"
BUILT_MODULE_NAME[ 9]="scst_disk"
BUILT_MODULE_NAME[10]="scst_modisk"
BUILT_MODULE_NAME[11]="scst_processor"
BUILT_MODULE_NAME[12]="scst_raid"
BUILT_MODULE_NAME[13]="scst_tape"
BUILT_MODULE_NAME[14]="scst_user"
BUILT_MODULE_NAME[15]="scst_vdisk"
BUILT_MODULE_NAME[16]="isert-scst"
DEST_MODULE_LOCATION[ 0]="/extra"
DEST_MODULE_LOCATION[ 1]="/extra"
DEST_MODULE_LOCATION[ 2]="/extra"
DEST_MODULE_LOCATION[ 3]="/extra"
DEST_MODULE_LOCATION[ 4]="/extra"
DEST_MODULE_LOCATION[ 5]="/extra"
DEST_MODULE_LOCATION[ 6]="/extra"
DEST_MODULE_LOCATION[ 7]="/extra/dev_handlers"
DEST_MODULE_LOCATION[ 8]="/extra/dev_handlers"
DEST_MODULE_LOCATION[ 9]="/extra/dev_handlers"
DEST_MODULE_LOCATION[10]="/extra/dev_handlers"
DEST_MODULE_LOCATION[11]="/extra/dev_handlers"
DEST_MODULE_LOCATION[12]="/extra/dev_handlers"
DEST_MODULE_LOCATION[13]="/extra/dev_handlers"
DEST_MODULE_LOCATION[14]="/extra/dev_handlers"
DEST_MODULE_LOCATION[15]="/extra/dev_handlers"
DEST_MODULE_LOCATION[16]="/extra"
EOF

%clean
rm -rf %{buildroot}

%pre
# Remove any existing ib_srpt.ko kernel modules
find /lib/modules/%{kversion} -name ib_srpt.ko -exec rm {} \;
# Remove files installed by "make install"
rm -f /usr/local/man/man5/iscsi-scstd.conf.5
rm -f /usr/local/man/man8/iscsi-scst-adm.8
rm -f /usr/local/man/man8/iscsi-scstd.8
rm -f /usr/local/sbin/iscsi-scst-adm
rm -f /usr/local/sbin/iscsi-scstd
rm -rf /usr/local/include/scst

%pre userspace
# Remove files installed by "make install"
rm -f /usr/local/man/man5/iscsi-scstd.conf.5
rm -f /usr/local/man/man8/iscsi-scst-adm.8
rm -f /usr/local/man/man8/iscsi-scstd.8
rm -f /usr/local/sbin/iscsi-scst-adm
rm -f /usr/local/sbin/iscsi-scstd
rm -rf /usr/local/include/scst

%post
dkms add -m %{kmod_name} -v %{dkms_version} --rpm_safe_upgrade
dkms build -m %{kmod_name} -v %{dkms_version}
dkms install -m %{kmod_name} -v %{dkms_version}

%preun
dkms remove -m %{kmod_name} -v %{dkms_version} --rpm_safe_upgrade --all
true

%files
%defattr(-,root,root,0755)
/usr/src/%{kmod_name}-%{dkms_version}/

%files userspace
%{_mandir}/man5/iscsi-scstd.conf.5.gz
%{_mandir}/man8/iscsi-scst-adm.8.gz
%{_mandir}/man8/iscsi-scstd.8.gz
%{_sbindir}/iscsi-scst-adm
%{_sbindir}/iscsi-scstd
%dir /var/lib/scst/dif_tags
%dir /var/lib/scst/pr
%dir /var/lib/scst/vdev_mode_pages

%files devel
%defattr(-,root,root,0755)
%dir /usr/include/scst
/usr/include/scst/Module.symvers
/usr/include/scst/backport.h
/usr/include/scst/build_mode.h
/usr/include/scst/scst.h
/usr/include/scst/scst_const.h
/usr/include/scst/scst_debug.h
/usr/include/scst/scst_itf_ver.h
/usr/include/scst/scst_sgv.h
/usr/include/scst/scst_user.h

%changelog
* Sun Mar  8 2020 Bart Van Assche <bvanassche@acm.org>
- Added support for the CentOSPlus kernel.
* Tue Oct  8 2019 Bart Van Assche <bvanassche@acm.org>
- Make this spec file compatible with rpmbuild v4.14.
* Wed May  6 2015 Bart Van Assche <bart.vanassche@sandisk.com>
- Build in release mode instead of debug mode.
* Mon May  4 2015 Bart Van Assche <bart.vanassche@sandisk.com>
- Added iSER target driver.
* Mon Feb 23 2015 Bart Van Assche <bart.vanassche@sandisk.com>
- Split spec file into a non-DKMS and a DKMS spec file.
* Fri Jan 16 2015 Bart Van Assche <bart.vanassche@sandisk.com>
- Added DKMS support.
* Fri Nov 22 2013 Bart Van Assche <bvanassche@acm.org>
- Initial spec file.
