#!/bin/sh
#
# builddeb 1.3
# Copyright 2003 Wichert Akkerman <wichert@wiggy.net>
#
# Simple script to generate a deb package for a Linux kernel. All the
# complexity of what to do with a kernel after it is installed or removed
# is left to other scripts and packages: they can install scripts in the
# /etc/kernel/{pre,post}{inst,rm}.d/ directories (or an alternative location
# specified in KDEB_HOOKDIR) that will be called on package install and
# removal.

set -e

add_missing_header_warn() {
	sed -i "/^test -d/i\\
warn() {\\
	words=\$\*\\
	echo \"\\\\e[33m\$words\\\\e[0m\" # no echo -e in dash\\
}\\
\\
# Warn about missing linux-headers required by dkms autoinstall\\
header_version=\$(dpkg-query -W -f='\${Version}' \"$kernel_headers_packagename\" 2>/dev/null || echo \"\")\\
\\
if [ \"_\$header_version\" != \"_$package_version\" ]; then\\
	warn \"************************ Warning ************************\"\\
	warn \"安装内核镜像 $packagename(= $package_version)\"\\
	warn \"需要预先安装 $kernel_headers_packagename(= $package_version)\"\\
	if [ -z \"\$header_version\" ]; then\\
		warn \"当前未安装 $kernel_headers_packagename\"\\
	else\\
		warn \"当前版本为 $kernel_headers_packagename(= \$header_version)\"\\
	fi\\
	warn \"此缺陷可能导致dkms模块无法安装，报告此风险但不阻塞内核镜像安装。\"\\
	warn \"*********************** Warning end *********************\"\\
fi\\
" $tmpdir/DEBIAN/postinst
}
create_package() {
	local pname="$1" pdir="$2"

	mkdir -m 755 -p "$pdir/DEBIAN"
	mkdir -p "$pdir/usr/share/doc/$pname"
	cp debian/copyright "$pdir/usr/share/doc/$pname/"
	cp debian/changelog "$pdir/usr/share/doc/$pname/changelog.Debian"
	gzip -9 "$pdir/usr/share/doc/$pname/changelog.Debian"
	sh -c "cd '$pdir'; find . -type f ! -path './DEBIAN/*' -printf '%P\0' \
		| xargs -r0 md5sum > DEBIAN/md5sums"

	# Fix ownership and permissions
	chown -R root:root "$pdir"
	chmod -R go-w "$pdir"

	# Create the package
	dpkg-gencontrol $forcearch -Vkernel:debarch="${debarch}" -p$pname -P"$pdir"
	dpkg --build "$pdir" ..
}

set_debarch() {
	# Attempt to find the correct Debian architecture
	case "$UTS_MACHINE" in
	i386|ia64|alpha)
		debarch="$UTS_MACHINE" ;;
	x86_64)
		debarch=amd64 ;;
	sparc*)
		debarch=sparc ;;
	s390*)
		debarch=s390$(grep -q CONFIG_64BIT=y $KCONFIG_CONFIG && echo x || true) ;;
	ppc*)
		debarch=$(grep -q CPU_LITTLE_ENDIAN=y $KCONFIG_CONFIG && echo ppc64el || echo powerpc) ;;
	parisc*)
		debarch=hppa ;;
	mips64*)
		debarch=mips64$(grep -q CPU_LITTLE_ENDIAN=y $KCONFIG_CONFIG && echo el || true) ;;
	mips*)
		debarch=mips$(grep -q CPU_LITTLE_ENDIAN=y $KCONFIG_CONFIG && echo el || true) ;;
	loongarch*)
		debarch=loongarch64 ;;
	arm64)
		debarch=arm64 ;;
	arm*)
		if grep -q CONFIG_AEABI=y $KCONFIG_CONFIG; then
		    if grep -q CONFIG_VFP=y $KCONFIG_CONFIG; then
			debarch=armhf
		    else
			debarch=armel
		    fi
		else
		    debarch=arm
		fi
		;;
	*)
		debarch=$(dpkg --print-architecture)
		echo "" >&2
		echo "** ** **  WARNING  ** ** **" >&2
		echo "" >&2
		echo "Your architecture doesn't have it's equivalent" >&2
		echo "Debian userspace architecture defined!" >&2
		echo "Falling back to using your current userspace instead!" >&2
		echo "Please add support for $UTS_MACHINE to ${0} ..." >&2
		echo "" >&2
	esac
	if [ -n "$KBUILD_DEBARCH" ] ; then
		debarch="$KBUILD_DEBARCH"
	fi
	forcearch="-DArchitecture=$debarch"

}

# Some variables and settings used throughout the script
version=$KERNELRELEASE
sourcename=$KDEB_SOURCENAME
tmpdir="$objtree/debian/tmp"
fwdir="$objtree/debian/fwtmp"
kernel_headers_dir="$objtree/debian/hdrtmp"
libc_headers_dir="$objtree/debian/headertmp"
dbg_dir="$objtree/debian/dbgtmp"
case $ARCH in
	mips|mips64)
		if grep CONFIG_CPU_TYPE=4000 $KCONFIG_CONFIG; then
			echo 4000
			cpu_type="-4000"
		fi
		packagename=linux-image-$version$cpu_type
		fwpackagename=linux-firmware-image-$version$cpu_type 
		kernel_headers_packagename=linux-headers-$version$cpu_type
		libc_headers_packagename=linux-libc-dev ;;
	*)
		packagename=linux-image-$version
		fwpackagename=linux-firmware-image-$version
		kernel_headers_packagename=linux-headers-$version
		libc_headers_packagename=linux-libc-dev ;;
esac

dbg_packagename=$packagename-dbg
package_version=$(dpkg-parsechangelog -n0 -SVersion)
debarch=
forcearch=
set_debarch

if [ "$ARCH" = "um" ] ; then
	packagename=user-mode-linux-$version
fi

# Not all arches have the same installed path in debian
# XXX: have each arch Makefile export a variable of the canonical image install
# path instead
case $ARCH in
um)
	installed_image_path="usr/bin/linux-$version"
	;;
parisc|powerpc)
	installed_image_path="boot/vmlinux-$version"
	;;
*)
	installed_image_path="boot/vmlinuz-$version"
esac

BUILD_DEBUG="$(grep -s '^CONFIG_DEBUG_INFO=y' $KCONFIG_CONFIG || true)"

# Setup the directory structure
rm -rf "$tmpdir" "$fwdir" "$kernel_headers_dir" "$libc_headers_dir" "$dbg_dir" $objtree/debian/files
mkdir -m 755 -p "$tmpdir/DEBIAN"
mkdir -p "$tmpdir/lib" "$tmpdir/boot"
mkdir -p "$fwdir/lib/firmware/$version/"
mkdir -p "$kernel_headers_dir/lib/modules/$version/"

# Build and install the kernel
if [ "$ARCH" = "um" ] ; then
	mkdir -p "$tmpdir/usr/lib/uml/modules/$version" "$tmpdir/usr/bin" "$tmpdir/usr/share/doc/$packagename"
	$MAKE linux
	cp System.map "$tmpdir/usr/lib/uml/modules/$version/System.map"
	cp $KCONFIG_CONFIG "$tmpdir/usr/share/doc/$packagename/config"
	gzip "$tmpdir/usr/share/doc/$packagename/config"
else
	cp System.map "$tmpdir/boot/System.map-$version"
	cp $KCONFIG_CONFIG "$tmpdir/boot/config-$version"
fi
# Not all arches include the boot path in KBUILD_IMAGE
if [ -e $KBUILD_IMAGE ]; then
	#cp $KBUILD_IMAGE "$tmpdir/$installed_image_path"
	cp vmlinuz "$tmpdir/$installed_image_path"
	cp vmlinuz.efi "$tmpdir/$installed_image_path.efi"
else
	#cp arch/$ARCH/boot/$KBUILD_IMAGE "$tmpdir/$installed_image_path"
	cp arch/$ARCH/boot/vmlinuz "$tmpdir/$installed_image_path"
fi
thtf_uefi_cert=UEFI-CA-CERT/UniontechUEFIDB-RSA.crt
if [ -f auto_sign_kernel -a -f "$tmpdir/$installed_image_path.efi" ];then
	echo "  SBSIGN  $tmpdir/$installed_image_path.efi"
	sbsign --hwkey 1 --cert "$thtf_uefi_cert" --output "$tmpdir/$installed_image_path.efi" "$tmpdir/$installed_image_path.efi"
	sbverify --cert "$thtf_uefi_cert" "$tmpdir/$installed_image_path.efi"
fi

if grep -q "^CONFIG_OF=y" $KCONFIG_CONFIG ; then
	# Only some architectures with OF support have this target
	if grep -q dtbs_install "${srctree}/arch/$SRCARCH/Makefile"; then
		$MAKE KBUILD_SRC= INSTALL_DTBS_PATH="$tmpdir/usr/lib/$packagename" dtbs_install
	fi
fi

if grep -q '^CONFIG_MODULES=y' $KCONFIG_CONFIG ; then
	INSTALL_MOD_PATH="$tmpdir" $MAKE KBUILD_SRC= modules_install
	rm -f "$tmpdir/lib/modules/$version/build"
	rm -f "$tmpdir/lib/modules/$version/source"
	if [ "$ARCH" = "um" ] ; then
		mv "$tmpdir/lib/modules/$version"/* "$tmpdir/usr/lib/uml/modules/$version/"
		rmdir "$tmpdir/lib/modules/$version"
	fi
	if [ -n "$BUILD_DEBUG" ] ; then
		for module in $(find $tmpdir/lib/modules/ -name *.ko -printf '%P\n'); do
			module=lib/modules/$module
			mkdir -p $(dirname $dbg_dir/usr/lib/debug/$module)
			# only keep debug symbols in the debug file
			$OBJCOPY --only-keep-debug $tmpdir/$module $dbg_dir/usr/lib/debug/$module
			# strip original module from debug symbols
			$OBJCOPY --strip-debug $tmpdir/$module
			# then add a link to those
			$OBJCOPY --add-gnu-debuglink=$dbg_dir/usr/lib/debug/$module $tmpdir/$module
		done

		# resign stripped modules
		MODULE_SIG_ALL="$(grep -s '^CONFIG_MODULE_SIG_ALL=y' $KCONFIG_CONFIG || true)"
		if [ -n "$MODULE_SIG_ALL" ]; then
			INSTALL_MOD_PATH="$tmpdir" $MAKE KBUILD_SRC= modules_sign
		fi
	fi
fi

if [ "$ARCH" != "um" ]; then
	$MAKE headers_check KBUILD_SRC=
	$MAKE headers_install KBUILD_SRC= INSTALL_HDR_PATH="$libc_headers_dir/usr"
fi

# Install the maintainer scripts
# Note: hook scripts under /etc/kernel are also executed by official Debian
# kernel packages, as well as kernel packages built using make-kpkg.
# make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and
# so do we; recent versions of dracut and initramfs-tools will obey this.
debhookdir=${KDEB_HOOKDIR:-/etc/kernel}
if grep -q '^CONFIG_BLK_DEV_INITRD=y' $KCONFIG_CONFIG; then
	want_initrd=Yes
else
	want_initrd=No
fi

for script in postinst ; do
	mkdir -p "$tmpdir$debhookdir/$script.d"
	cat <<EOF > "$tmpdir/DEBIAN/$script"
#!/bin/sh

set -e

# Pass maintainer script parameters to hook scripts
export DEB_MAINT_PARAMS="\$*"

# Tell initramfs builder whether it's wanted
export INITRD=$want_initrd

# 判断需要的格式，删除不需要的
save_kernel_selected_fmt() {
    # (1) 读取配置，非法或不存在时默认 elf
    config_file=/etc/kernel-fmt.conf
    if [ -f "\$config_file" ] && grep -qxE "efi|elf" "\$config_file"; then
        wanted_fmt=\$(tr -d '[:space:]' < "\$config_file")
        echo "postinst: Valid config, use config '\$wanted_fmt'"
    else
        wanted_fmt=elf
        echo "postinst: Invalid config, defaulting to 'elf'"
    fi
    rm \$config_file

    # (2) 根据格式删除另一种内核
    case "\$wanted_fmt" in
        efi)
            echo "postinst: Keep efi kernel for UEFI BOOT"
            mv -fv /$installed_image_path.efi /$installed_image_path
            ;;
        elf)
            echo "postinst: Keep elf kernel for LEGACY BOOT"
            rm -fv /$installed_image_path.efi
            ;;
    esac
}
save_kernel_selected_fmt

test -d $debhookdir/$script.d && run-parts --arg="$version" --arg="/$installed_image_path" $debhookdir/$script.d

exit 0
EOF
	chmod 755 "$tmpdir/DEBIAN/$script"
done
add_missing_header_warn

for script in postrm preinst prerm ; do
	mkdir -p "$tmpdir$debhookdir/$script.d"
	cat <<EOF > "$tmpdir/DEBIAN/$script"
#!/bin/sh

set -e

# Pass maintainer script parameters to hook scripts
export DEB_MAINT_PARAMS="\$*"

# Tell initramfs builder whether it's wanted
export INITRD=$want_initrd

test -d $debhookdir/$script.d && run-parts --arg="$version" --arg="/$installed_image_path" $debhookdir/$script.d

exit 0
EOF
	chmod 755 "$tmpdir/DEBIAN/$script"
done

cat <<EOF > "$tmpdir/DEBIAN/preinst"
#!/bin/bash

function remove_mwv206_dkms_ko() {
	MODS=\`ls /lib/modules/\`
	for MOD in \$MODS
	do
		rm -rf /lib/modules/\${MOD}/extra/mwv206*.ko
		EXISTKOS=\`find /lib/modules/\${MOD}/kernel/ -name mwv206*.ko.bak 2>/dev/null\`
		for EKOBAK in \${EXISTKOS}
		do
			if [ \${EKOBAK} != "" ]; then
				if [ -e \${EKOBAK} ]; then
					EKO=\`echo \${EKOBAK} | sed 's/\.bak//g'\`
					mv \${EKOBAK} \${EKO}
				fi
			fi
		done
		sync
		depmod -a
		update-initramfs -u -k \${MOD}
	done
}

function remove_mwv206_file() {
	if [ -d /opt/mwv206 ]; then
		rm -rf /opt/mwv206
	fi
}

function remove_mwv206_info_file() {
	if [ -d /usr/lib/dpkg-db/info ]; then
		rm -rf /usr/lib/dpkg-db/info/mwv206-dkms.*
	fi
}

function remove_mwv206_dkms() {
	mwv206_dkms_info=\`find /usr/lib/dpkg-db/info/ -name "mwv206-dkms.*" 2>/dev/null\`
	if [ "\$mwv206_dkms_info" != "" ]; then
		remove_mwv206_dkms_ko
		remove_mwv206_file
		remove_mwv206_info_file
	fi
}

remove_mwv206_dkms

function select_kernel_fmt() {
    # (1) 检查当前内核格式
    current_file=/$installed_image_path
    current_fmt=elf                             # 默认ELF
    if [ -e "\$current_file" ] && file \$current_file | grep -q 'MS-DOS'; then
            current_fmt=efi                       # 仅当 file 输出包含'MS-DOS', 认为是 EFI 内核
    fi

    # (2) 检查 kernel_fmt=xxx 是否存在,并提取 wanted_fmt
    config_file=/etc/kernel-fmt.conf
    if [ -f "\$config_file" ] && [ -s "\$config_file" ]; then
        wanted_fmt=\$(tr -d '[:space:]' < "\$config_file") # 去除所有空格/换行
        # 检查 wanted_fmt 是否合法
        case "\$wanted_fmt" in
        elf|efi)
            # (2a) 配置合法，选用配置，提示与当前格式是否一致
            if [ "\$current_fmt" = "\$wanted_fmt" ]; then
                echo "preinst: Using fmt: \$wanted_fmt"
            else
                echo "preinst: Current fmt: '\$current_fmt', but using Config fmt: '\$wanted_fmt'"
            fi
            ;;
        *)
            # (2b) 非法值，替换为当前各式
            echo "preinst: Invalid fmt '\$wanted_fmt' in \$config_file, fixing to '\$current_fmt'"
            echo "\$current_fmt" > "\$config_file"
            ;;
        esac
    else
        # 文件不存在或为空，写入当前各式
        echo "preinst: Creating \$config_file with current fmt: \$current_fmt"
        echo "\$current_fmt" > "\$config_file"
    fi
}

select_kernel_fmt

exit 0
EOF

# Try to determine maintainer and email values
if [ -n "$DEBEMAIL" ]; then
       email=$DEBEMAIL
elif [ -n "$EMAIL" ]; then
       email=$EMAIL
else
       email=$(id -nu)@$(hostname -f 2>/dev/null || hostname)
fi
if [ -n "$DEBFULLNAME" ]; then
       name=$DEBFULLNAME
elif [ -n "$NAME" ]; then
       name=$NAME
else
       name="Anonymous"
fi
maintainer="$name <$email>"

# Try to determine distribution
if [ -n "$KDEB_CHANGELOG_DIST" ]; then
        distribution=$KDEB_CHANGELOG_DIST
elif distribution=$(lsb_release -cs 2>/dev/null) && [ -n "$distribution" ]; then
        : # nothing to do in this case
else
        distribution="unstable"
        echo >&2 "Using default distribution of 'unstable' in the changelog"
        echo >&2 "Install lsb-release or set \$KDEB_CHANGELOG_DIST explicitly"
fi


# Generate copyright file
cat <<EOF > debian/copyright
This is a packacked upstream version of the Linux kernel.

The sources may be found at most Linux ftp sites, including:
ftp://ftp.kernel.org/pub/linux/kernel

Copyright: 1991 - 2015 Linus Torvalds and others.

The git repository for mainline kernel development is at:
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2 dated June, 1991.

On Debian GNU/Linux systems, the complete text of the GNU General Public
License version 2 can be found in \`/usr/share/common-licenses/GPL-2'.
EOF


build_depends="bc, kmod, cpio "

# Generate a control file
cat <<EOF > debian/control
Source: $sourcename
Section: kernel
Priority: optional
Maintainer: $maintainer
Build-Depends: $build_depends
Standards-Version: 3.8.4
Homepage: http://www.kernel.org/
EOF

if [ "$ARCH" = "um" ]; then
	cat <<EOF >> debian/control

Package: $packagename
Provides: linux-image, linux-image-2.6, linux-modules-$version
Architecture: any
Description: User Mode Linux kernel, version $version
 User-mode Linux is a port of the Linux kernel to its own system call
 interface.  It provides a kind of virtual machine, which runs Linux
 as a user process under another Linux kernel.  This is useful for
 kernel development, sandboxes, jails, experimentation, and
 many other things.
 .
 This package contains the Linux kernel, modules and corresponding other
 files, version: $version.
EOF

else
	cat <<EOF >> debian/control

Package: $packagename
Provides: linux-image, linux-image-2.6, linux-modules-$version
Suggests: $fwpackagename
Architecture: any
Description: Linux kernel, version $version
 This package contains the Linux kernel, modules and corresponding other
 files, version: $version.
EOF

fi

# Build kernel header package
(cd $srctree; find . -name Makefile\* -o -name Kconfig\* -o -name \*.pl) > "$objtree/debian/hdrsrcfiles"
(cd $srctree; find . -name Platform\* -o -name Kbuild\* -o -name \*.pl >> "$objtree/debian/hdrsrcfiles")
(cd $srctree; find arch/$SRCARCH/include include scripts -type f) >> "$objtree/debian/hdrsrcfiles"
(cd $srctree; find arch/$SRCARCH -name module.lds -o -name Kbuild.platforms -o -name Platform) >> "$objtree/debian/hdrsrcfiles"
(cd $srctree; find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f) >> "$objtree/debian/hdrsrcfiles"
if grep -q '^CONFIG_STACK_VALIDATION=y' $KCONFIG_CONFIG ; then                                                        
       (cd $objtree; find tools/objtool -type f -executable) >> "$objtree/debian/hdrobjfiles"                         
fi
(cd $objtree; find arch/$SRCARCH/include Module.symvers include scripts -type f) >> "$objtree/debian/hdrobjfiles"
destdir=$kernel_headers_dir/usr/src/linux-headers-$version
mkdir -p "$destdir"
(cd $srctree; tar -c -f - -T -) < "$objtree/debian/hdrsrcfiles" | (cd $destdir; tar -xf -)
(cd $objtree; tar -c -f - -T -) < "$objtree/debian/hdrobjfiles" | (cd $destdir; tar -xf -)
(cd $objtree; cp $KCONFIG_CONFIG $destdir/.config) # copy .config manually to be where it's expected to be
ln -sf "/usr/src/linux-headers-$version" "$kernel_headers_dir/lib/modules/$version/build"
rm -f "$objtree/debian/hdrsrcfiles" "$objtree/debian/hdrobjfiles"

cat <<EOF >> debian/control

Package: $kernel_headers_packagename
Provides: linux-headers, linux-headers-2.6
Architecture: any
Description: Linux kernel headers for $KERNELRELEASE on \${kernel:debarch}
 This package provides kernel header files for $KERNELRELEASE on \${kernel:debarch}
 .
 This is useful for people who need to build external modules
EOF

# Do we have firmware? Move it out of the way and build it into a package.
if [ -e "$tmpdir/lib/firmware" ]; then
	mv "$tmpdir/lib/firmware"/* "$fwdir/lib/firmware/$version/"
	rmdir "$tmpdir/lib/firmware"

	cat <<EOF >> debian/control

Package: $fwpackagename
Architecture: all
Description: Linux kernel firmware, version $version
 This package contains firmware from the Linux kernel, version $version.
EOF

	create_package "$fwpackagename" "$fwdir"
fi

cat <<EOF >> debian/control

Package: $libc_headers_packagename
Section: devel
Provides: linux-kernel-headers
Architecture: any
Description: Linux support headers for userspace development
 This package provides userspaces headers from the Linux kernel.  These headers
 are used by the installed headers for GNU glibc and other system libraries.
EOF
case $ARCH in
	mips|mips64)
		if [ -n "$CROSS_COMPILE" ]; then
		   ${CROSS_COMPILE}gcc -o  "$destdir/scripts/basic/fixdep" "$objtree/scripts/basic/fixdep.c"
			if grep -q '^CONFIG_BUILD_BIN2C' $KCONFIG_CONFIG ; then
				${CROSS_COMPILE}gcc -O2 -o "$destdir/scripts/bin2c" "$objtree/scripts/basic/bin2c.c"
			fi
			if grep -q '^CONFIG_KALLSYMS' $KCONFIG_CONFIG ; then
				${CROSS_COMPILE}gcc -O2 -o "$destdir/scripts/kallsyms" "$objtree/scripts/kallsyms.c"
			fi
			if grep -q '^CONFIG_ASN1' $KCONFIG_CONFIG ; then
				${CROSS_COMPILE}gcc -O2 -o "$destdir/scripts/asn1_compiler" "$objtree/scripts/asn1_compiler.c" -I$srctree/include
			fi
			if grep -q '^CONFIG_VT' $KCONFIG_CONFIG ; then
				${CROSS_COMPILE}gcc -O2 -o "$destdir/scripts/conmakehash" "$objtree/scripts/conmakehash.c"
			fi
			${CROSS_COMPILE}gcc -mips64r2 -mabi=64 -O2 -o "$destdir/scripts/basic/fixdep" "$objtree/scripts/basic/fixdep.c"
			${CROSS_COMPILE}gcc -mips64r2 -mabi=64 -O2 -o "$destdir/scripts/unifdef" "$objtree/scripts/unifdef.c"
			${CROSS_COMPILE}gcc -mips64r2 -mabi=64 -O2 -o "$destdir/scripts/mod/mk_elfconfig" "$objtree/scripts/mod/mk_elfconfig.c"
			${CROSS_COMPILE}gcc -mips64r2 -mabi=64 -O2 -o "$destdir/scripts/mod/modpost" "$objtree/scripts/mod/modpost.c" "$objtree/scripts/mod/file2alias.c" "$objtree/scripts/mod/sumversion.c"
		fi
esac

if [ "$ARCH" != "um" ]; then
	create_package "$kernel_headers_packagename" "$kernel_headers_dir"
	create_package "$libc_headers_packagename" "$libc_headers_dir"
fi

create_package "$packagename" "$tmpdir"

if [ -n "$BUILD_DEBUG" ] ; then
       # Build debug package
       # Different tools want the image in different locations
       # perf
	mkdir -p $dbg_dir/usr/lib/debug/lib/modules/$version/
	cp vmlinux $dbg_dir/usr/lib/debug/lib/modules/$version/
       # systemtap
	mkdir -p $dbg_dir/usr/lib/debug/boot/
	ln -s ../lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/boot/vmlinux-$version
       # kdump-tools
	ln -s lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/vmlinux-$version

cat <<EOF >> debian/control

Package: $dbg_packagename
Section: debug
Provides: linux-debug, linux-debug-$version
Architecture: any
Description: Linux kernel debugging symbols for $version
 This package will come in handy if you need to debug the kernel. It provides
 all the necessary debug symbols for the kernel and its modules.
EOF

	create_package "$dbg_packagename" "$dbg_dir"
fi

exit 0
