#! /bin/bash

__show_spec_log () {
	local from

	echo "%changelog"
	printf "* %s %s <%s> %s\n" \
		"$(LC_TIME=en_US date +"%a %b %d %Y")" \
		"$(git config --get user.name)"        \
		"$(git config --get user.email)"        \
		$uname_r

	#
	# Get the base of the log, the idea is simple: if a release
	# has already been made (KERNLE_VERSION_PACKAGE is defined)
	# then use the n-1 release otherwise don't log anything (we
	# shouldn't have been called).
	#
	if ! test $KERNEL_VERSION_PACKAGE; then
		die "BUG: show_spec_log has been called with package num unset"
	fi

	i=$KERNEL_VERSION_PACKAGE

	if test $scmversion; then
		from=$(kdist__make_tag $i)
	else
		#
		# Unfortunately with the great 3.0 num serie, the tag
		# doesn't match the kernel version anymore: for
		# example tag can be 3.0 and the kernel version is
		# 3.0.0... hence the call to git-describe. oh well...
		#
		if test $i -gt 1; then
			from=$(kdist__make_tag $((i-1)))
		else
			from=$(git__describe --abbrev=0 HEAD~1)
		fi
	fi
	git --no-pager shortlog --first-parent -e $from..
}

__show_source_spec () {
	echo "Name:          $rpm_name"
	echo "Summary:       Linux kernel source files"
	echo "Version:       $rpm_version"
	echo "Release:       %mkrel $rpm_release"
	echo "License:       GPL v2"
	echo "Group:         System/Kernel and hardware"
	echo "URL:           http://www.kernel.org"
	if [ $rpm_variety = srpm ]; then
	echo "Source:        $rpm_archive.tar.bz2"
	fi
	echo "Buildarch:     noarch"
	echo "BuildRoot:     %{_tmppath}/%{name}-%{PACKAGE_VERSION}-root"
	echo "Provides:      $rpm_name = $rpm_fullversion"
	echo "AutoReqProv:   no"
	echo "BuildRequires: rsync"
	echo
	echo "%define debug_package %{nil}"
	echo "%define __check_files %{nil}"
	echo
	echo "%description"
	echo "This package provides the whole kernel source files."
	echo
	if [ $rpm_variety = srpm ]; then
	echo "%prep"
	echo "%setup -q -n $uname_r"
	echo
	fi
	echo "%install"
	echo "mkdir -p   %{buildroot}/usr/src/linux-$uname_r"
	echo "rsync -a . %{buildroot}/usr/src/linux-$uname_r"
	echo
	echo "%clean"
	echo "rm -rf %{buildroot}"
	echo
	echo "%files"
	echo "%defattr (-, root, root)"
	echo "/usr/src"
	echo
	if $rpm_log; then
		__show_spec_log
	fi
}

__show_firmware_spec () {
	echo "Name:          $rpm_name"
	echo "Summary:       Linux kernel firmware files"
	echo "Version:       $rpm_version"
	echo "Release:       %mkrel $rpm_release"
	echo "License:       GPL v2"
	echo "Group:         System/Kernel and hardware"
	echo "URL:           http://www.kernel.org"
	if [ $rpm_variety = srpm ]; then
	echo "Source:        $rpm_archive.tar.bz2"
	fi
	echo "Buildarch:     noarch"
	echo "BuildRoot:     %{_tmppath}/%{name}-%{PACKAGE_VERSION}-root"
	echo "Provides:      $rpm_name = $rpm_fullversion"
	echo "AutoReqProv:   no"
	echo "BuildRequires: rsync"
	echo
	echo "%define debug_package %{nil}"
	echo "%define __check_files %{nil}"
	echo
	echo "%description"
	echo "Firmware files needed by some devices."
	echo
	if [ $rpm_variety = srpm ]; then
	echo "%prep"
	echo "%setup -q -n $rpm_archive"
	echo
	fi
	echo "%install"
	echo "rsync -a lib %{buildroot}"
	echo
	echo "%clean"
	echo "rm -rf %{buildroot}"
	echo
	echo "%files"
	echo "%defattr (-, root, root)"
	echo "/lib/firmware"
	echo
	if $rpm_log; then
		__show_spec_log
	fi
}

__show_headers_spec () {
	echo "Name:          $rpm_name"
	echo "Summary:       Linux kernel header files mostly used by your C library"
	echo "Version:       $rpm_version"
	echo "Release:       %mkrel $rpm_release"
	echo "License:       GPL v2"
	echo "Group:         System/Kernel and hardware"
	echo "URL:           http://www.kernel.org"
	if [ $rpm_variety = srpm ]; then
	echo "Source:        $rpm_archive.tar.bz2"
	fi
	echo "BuildRoot:     %{_tmppath}/%{name}-%{PACKAGE_VERSION}-root"
	echo "Provides:      $rpm_name = $rpm_fullversion"
	echo "AutoReqProv:   no"
	echo "BuildRequires: rsync"
	echo
	echo "%define debug_package %{nil}"
	echo "%define __check_files %{nil}"
	echo
	echo "%description"
	echo "C header files from the Linux kernel. The header files define"
	echo "structures and constants that are needed for building most"
	echo "standard programs, notably the C library."
	echo
	echo "This package is not suitable for building kernel modules, you"
	echo "should use the 'kernel-devel' package instead."
	echo
	if [ $rpm_variety = srpm ]; then
	echo "%prep"
	echo "%setup -q -n $uname_r"
	echo
	echo "%install"
	echo "make INSTALL_HDR_PATH=%{buildroot}/usr headers_install"
	else
	echo "%install"
	echo "rsync -a usr %{buildroot}"
	fi
	echo
	echo "%clean"
	echo "rm -rf %{buildroot}"
	echo
	echo "%files"
	echo "%defattr (-, root, root)"
	echo "/usr/include"
	echo
	if $rpm_log; then
		__show_spec_log
	fi
}

with_devel=
with_debuginfo=

__show_vmlinux_spec () {
	local develpath=/usr/src/devel/$uname_r

	echo "Name:          $rpm_name"
	echo "Summary:       The Linux Kernel for Mandriva systems"
	echo "License:       GPLv2"
	echo "Version:       $rpm_version"
	echo "Release:       %mkrel $rpm_release"
	echo "URL:           http://www.kernel.org"
	if [ $rpm_variety = srpm ]; then
	echo "Source:        $rpm_archive.tar.bz2"
	fi
	echo "ExclusiveArch: $rpm_exclusive"
	echo "BuildRoot:     %{_tmppath}/%{name}-%{PACKAGE_VERSION}-root"
	echo "AutoReqProv:   no"
	echo
	if [ $with_debuginfo ]; then
	echo "%global __debug_package 1"
	fi
	echo "%define debug_package %{nil}"
	echo "%define __check_files %{nil}"
	echo "%ifarch %ix86 x86_64"
	echo "%define asmarch x86"
	echo "%define bzImage arch/x86/boot/bzImage"
	echo "%endif"
	echo
	#
	# %package kernel
	#
	echo "Summary:       The Linux Kernel for Mandriva systems"
	echo "Provides:      kernel = $rpm_fullversion"
	echo "Provides:      $rpm_name = $rpm_fullversion"
	echo "Group:         System/Kernel and hardware"
	echo
	echo "%description"
	echo "The Linux Kernel, the operating system core itself"
	echo
	#
	# %package kernel-devel
	#
	if [ $with_devel ]; then
	echo "%package devel"
	echo "Summary:       The minimal Linux Kernel for building kernel modules"
	echo "Provides:      kernel-devel = $rpm_fullversion"
	echo "Provides:      $rpm_name-devel = $rpm_fullversion"
	echo "BuildRequires: rsync"
	echo
	echo "%description -n $rpm_name-devel"
	echo "This package provides kernel headers, makefiles and a couple"
	echo "of others files sufficient to build external kernel modules."
	echo
	fi
	#
	# %package kernel-debuginfo
	#
	if [ $with_debuginfo ]; then
	echo "%package debuginfo"
	echo "Summary:       The debug information for $rpm_name"
	echo "Provides:      kernel-debuginfo = $rpm_fullversion"
	echo "Provides:      $rpm_name-debuginfo = $rpm_fullversion"
	echo
	echo "%description -n $rpm_name-debuginfo"
	echo "This package provides the kernel's debug information required"
	echo "by some binary object tools like kgdb, perf, etc..."
	echo
	fi
	if [ $rpm_variety = srpm ]; then
	#
	# %prep
	#
	echo "%prep"
	echo "%setup -q -n $uname_r"
	echo
	#
	# %build
	#
	echo "%build"
	echo "make defconfig"
	echo "make -s kernelrelease"
	echo "test \$(make -s kernelrelease) = $uname_r"
	echo "make %{?_smp_mflags}"
	echo
	fi
	#
	# %install
	#
	echo "%install"
	if [ $have_modules ]; then
	#
	# Don't specify parallel jobs here since it may broke firmware
	# installation somehow...
	#
	echo "make -s INSTALL_MOD_PATH=%{buildroot} modules_install"
	#
	# Mark all kernel modules as executable so they will be
	# stripped and their corresponding debug info files will be
	# generated if needed.
	#
	echo "find %{buildroot} -name \*.ko -exec chmod u+x {} \;"
	echo
	fi
	echo "mkdir -p %{buildroot}/boot"
	echo "cp %{bzImage} %{buildroot}/boot/vmlinuz-$uname_r"
	echo "cp System.map %{buildroot}/boot/System.map-$uname_r"
	echo "cp .config    %{buildroot}/boot/config-$uname_r"
	if [ $have_modules ]; then
	echo "ln -snf $develpath %{buildroot}/lib/modules/$uname_r/build"
	echo "ln -snf build %{buildroot}/lib/modules/$uname_r/source"
	fi
	echo
	if [ $with_devel ]; then
	echo "mkdir -p %{buildroot}$develpath"
	echo "cat develfiles-%asmarch.list >>develfiles.list"
	echo "rsync -ar --files-from=develfiles.list . %{buildroot}$develpath"
	echo
	fi
	#
	# %post, %preun, %postun... for main package
	#
	echo "%post -n $rpm_name"
	echo "/sbin/installkernel $uname_r"
	echo
	echo "%preun -n $rpm_name"
	echo "/sbin/installkernel -R $uname_r"
	echo
	echo "%postun -n $rpm_name"
	echo "/sbin/kernel_remove_initrd $uname_r"
	echo
	#
	# %clean
	#
	echo "%clean"
	echo "rm -rf %{buildroot}"
	echo
	#
	# %files
	#
	echo "%files -n $rpm_name"
	echo '%defattr (-, root, root)'
	if [ $have_modules ]; then
	echo "%dir /lib/modules"
	echo "/lib/modules/$uname_r"
	fi
	echo "/boot"
	echo
	if [ $with_devel ]; then
	echo "%files -n $rpm_name-devel"
	echo "%defattr (-, root, root)"
	echo "$develpath"
	echo
	fi
	if [ $with_debuginfo ]; then
	echo "%files -n $rpm_name-debuginfo -f debugfiles.list"
	echo "%defattr (-, root, root)"
	echo
	fi
	#
	# %changelog
	#
	if $rpm_log; then
		__show_spec_log
	fi
}

show_spec () {
	local rpm_version=${rpm_version//-/.}
	local rpm_release=${rpm_release//-/.}
	local rpm_fullversion=$rpm_version.$rpm_release
	local rpm_uname=${uname_r//-/.}

	if test "$spec_file"; then
		cat "$spec_file"
		return
	fi
	echo "#"
	echo "# Spec file generated by kdist version $(kdist_version)"
	echo "#"
	__show_$1_spec
}

rpm__architecture () {
	local arg arch

	for arg; do
		echo $arg
	done | sort | uniq | while read arch
	do
		case $arch in
		x86_64)		echo -n "x86_64 ";;
		i?86|x86_32)	echo -n "%ix86 " ;;
		*)		die "Unsupported architecture '$arch'"
		esac
	done
}