#
# Copyright (c) 2003 Linuxant inc.
#
# NOTE: The use and distribution of this software is governed by the terms in
# the file LICENSE, which is included in the package. You must read this and
# agree to these terms before using or distributing this software.
#

# set _target_kernel to generic if it wasn't defined on the command-line
%if %{expand:%{?_target_kernel:0}%{!?_target_kernel:1}}
%define _target_kernel generic
%endif

%if %{expand:%{?_build_doc:0}%{!?_build_doc:1}}
%define _build_doc 0
%endif

%if "%{_target_kernel}" == "generic"
%define ver      @CNXTLINUXVERSION@
%define rel      @CNXTLINUXRPM_REL@
%define automrecomp 1
%else
%define ver      @CNXTLINUXVERSION@_k%(eval echo %{_target_kernel} | sed 's/-/_/g')
%define rel      @CNXTLINUXRPM_REL@%{_target_distro}
%define automrecomp 0
%endif

%define cnxtdriver  @CNXTDRIVER@
%define cnxtdrvdsc  @CNXTDRVDSC@ driver
%define cnxttarget  @CNXTTARGET@

%define scr_support @SCR_SUPPORT@
%define dmp_support @DMP_SUPPORT@
%define blam_support @BLAM_SUPPORT@

# Note that this is NOT a relocatable package

%define cnxtetcdir @CNXTETCDIR@
%define cnxtlibdir @CNXTLIBDIR@

# Even though newer versions of RPM provide definitions for these,
# we must still accomodate the older ones
%define prefix   /usr
%define etcdir   /etc
%define libdir   %{prefix}/lib
%define sbindir  %{prefix}/sbin
%define bindir   %{prefix}/bin

Summary:   %{cnxtdrvdsc}
Name:      %{cnxttarget}modem
ExclusiveOS: Linux
%if "%{_target_kernel}" == "generic"
%if "%{cnxtdriver}" == "hsf"
ExclusiveArch: athlon i686 i586 i386
%endif
%if "%{cnxtdriver}" == "hcf"
ExclusiveArch: athlon i686 i586 i486 i386
%endif
%else
ExclusiveArch: %{_target_cpu}
%endif
Version:   %ver
Release:   %rel
Vendor:	   Linuxant
Copyright: Copyright (c) 2003 Linuxant inc. All rights reserved.
Group:     System Environment/Kernel
Source:    %{cnxttarget}modem-@CNXTLINUXVERSION@.tar.gz
%if %{_build_doc}
Source2:   100498D_RM_HxF_Released.pdf
%endif
URL:       http://www.linuxant.com/drivers/%{cnxtdriver}
BuildRoot: %{_tmppath}/%{cnxttarget}modem-%{PACKAGE_VERSION}-root
Packager:  Linuxant
Requires:  pciutils
%if %{automrecomp}
Requires:  kernel-source
Requires:  gcc
%endif
%if "%{cnxtdriver}" == "hsf"
Requires:  perl
%endif
Conflicts: %{cnxtdriver}linmodem
Autoreq:   0

%if %{_build_doc}
%package doc
Group:     Documentation
Summary:   Documentation for %{cnxtdrvdsc}
BuildArch: noarch
ExclusiveArch: noarch
%endif

%description
%{cnxtdrvdsc}

@LICENSE_FILE@

%if %{_build_doc}
%description doc
This package contains the documentation for the %{cnxtdrvdsc}.
%endif

%prep
%setup -q -n %{cnxttarget}modem-@CNXTLINUXVERSION@

%build

%if %{_build_doc}
if [ -f %{SOURCE2} ]; then cp %{SOURCE2} .; else true; fi
%else
make --quiet --no-print-directory all
%endif

%if "%{_target_kernel}" == "generic"

%else

%if "%{_target_distro}" == "rh"
(
	if cd modules; then
		make --quiet --no-print-directory KERNELSRC=%{_distro_kernels}/%{_target_distro}/linux-%{_target_kernel} DISTRO_CFLAGS="-D__BOOT_KERNEL_H_ -D__BOOT_KERNEL_ENTERPRISE=0 -D__BOOT_KERNEL_SMP=0 -D__BOOT_KERNEL_UP=1 -D__MODULE_KERNEL_%{_target_cpu}=1" CNXT_MODS_DIR=binaries/%{_target_cpu}/%{_target_distro}/linux-%{_target_kernel} clean all modules_install || exit $?
	fi
) || exit $?
%endif

%if "%{_target_distro}" == "mdk"
(
	if cd modules; then
		make --quiet --no-print-directory KERNELSRC=%{_distro_kernels}/%{_target_distro}/linux-%{_target_kernel} DISTRO_CFLAGS="-D__BOOT_KERNEL_H_ -D__BOOT_KERNEL_ENTERPRISE=0 -D__BOOT_KERNEL_SMP=0 -D__BOOT_KERNEL_UP=1 -D__MODULE_KERNEL_%{_target_cpu}=1" CNXT_MODS_DIR=binaries/%{_target_cpu}/%{_target_distro}/linux-%{_target_kernel} clean all modules_install || exit $?
		ln -s i586 binaries/i686
		ln -s i586 binaries/athlon
	fi
) || exit $?
%endif

%if "%{_target_distro}" == "suse"
(
	if cd modules; then
		make --quiet --no-print-directory KERNELSRC=%{_distro_kernels}/%{_target_distro}/linux-%{_target_kernel} CNXT_MODS_DIR=binaries/%{_target_cpu}/%{_target_distro}/linux-%{_target_kernel} clean all modules_install || exit $?
%if "%{_target_cpu}" == "i586"
		for a in i686 athlon; do
%else
		for a in i386 i486 i586 i686 athlon; do
%endif
			[ -e binaries/$a ] || ln -s %{_target_cpu} binaries/$a
		done
	fi
) || exit $?
%endif

%endif

%install
rm -rf $RPM_BUILD_ROOT

make --quiet --no-print-directory ROOT=$RPM_BUILD_ROOT install

( cd $RPM_BUILD_ROOT%{cnxtetcdir}/nvm && cd .. && tar czf nvm.tar.gz nvm && rm -rf nvm/*)

%clean
rm -rf $RPM_BUILD_ROOT

%files

%defattr(0555, root, root, 755)

%{sbindir}/%{cnxttarget}config
%{sbindir}/%{cnxttarget}stop
%if "%{cnxtdriver}" == "hsf"
%{sbindir}/%{cnxttarget}dcpd
%endif

%if %{scr_support}
%{sbindir}/%{cnxttarget}scr
%{bindir}/qtmodemon
%endif
%if %{dmp_support}
%{sbindir}/%{cnxttarget}dmp
%endif
%if %{blam_support}
%{sbindir}/%{cnxttarget}diag
%endif

%defattr(0444, root, root, 755)
%dir %{cnxtetcdir}
%dir %{cnxtetcdir}/nvm
%{cnxtetcdir}/nvm.tar.gz
%dir %{cnxtlibdir}
%doc %{cnxtlibdir}/LICENSE
%dir %{cnxtlibdir}/modules
%dir %{cnxtlibdir}/modules/GPL
%doc %{cnxtlibdir}/modules/GPL/COPYING
%{cnxtlibdir}/modules/binaries

%if %{automrecomp}
%config %{cnxtlibdir}/config.mak
%{cnxtlibdir}/modules/imported
%attr(755, root, root) %{cnxtlibdir}/modules/*.sh
%{cnxtlibdir}/modules/Makefile
%{cnxtlibdir}/modules/*.c
%{cnxtlibdir}/modules/GPL/*.c
%{cnxtlibdir}/modules/GPL/*.h
%dir %{cnxtlibdir}/modules/include
%{cnxtlibdir}/modules/include/*.h
%endif

%doc BUGS CHANGES CREDITS FAQ INSTALL LICENSE README

%pre
if [ "$1" = 1 ]; then
	if [ -e "%{cnxtetcdir}" ]; then
		echo "Removing old %{cnxtetcdir}"
		rm -rf "%{cnxtetcdir}"
	fi
	if [ -e "%{cnxtlibdir}" ]; then
		echo "Removing old %{cnxtlibdir}"
		rm -rf "%{cnxtlibdir}"
	fi
fi
exit 0

%post

if [ -f %{cnxtetcdir}/nvm.tar.gz ]; then
	( cd %{cnxtetcdir} && tar xzf nvm.tar.gz )
fi

#%if ! %{automrecomp}
CNXT_AUTOCONFIG=true
#%endif
if [ -n "${CNXT_AUTOCONFIG}" -a -z "${CNXT_NOAUTOCONFIG}" ]; then
	%{sbindir}/%{cnxttarget}config --auto
	exit $?
else
	echo "To complete the installation and configuration of your modem,"
	echo "please run \"%{cnxttarget}config\" (or \"%{sbindir}/%{cnxttarget}config\")"
	exit 0
fi

%preun
%{sbindir}/%{cnxttarget}stop
if [ "$1" = 0 ]; then

	if [ -z "${CNXT_NOAUTOCONFIG}" ]; then
		%{sbindir}/%{cnxttarget}config --remove
	fi

	if [ -f %{cnxtetcdir}/nvm.tar.gz ]; then
		( cd %{cnxtetcdir} && rm -rf `tar tzf %{cnxtetcdir}/nvm.tar.gz | egrep '^nvm/[^/]+/?$'` )
	fi
else
	exit 0
fi

%if %{_build_doc}
%files doc
%doc *.pdf
%endif

# This must be last since the file CHANGES is automatically appended
%changelog

