#!/bin/sh
#
# Copyright 2003 Wichert Akkerman <wichert@wiggy.net>
#
# Simple script to generate a debian/ directory for a Linux kernel.

set -e

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 ;;
	mips*)
		debarch=mips$(grep -q CPU_LITTLE_ENDIAN=y $KCONFIG_CONFIG && echo el || true) ;;
	aarch64|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 its 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
}

# Some variables and settings used throughout the script
version=$KERNELRELEASE
if [ -n "$KDEB_PKGVERSION" ]; then
	packageversion=$KDEB_PKGVERSION
else
	revision=$(cat .version 2>/dev/null||echo 1)
	packageversion=$version-$revision
fi
sourcename=$KDEB_SOURCENAME
packagename=linux-image-$version
kernel_headers_packagename=linux-headers-$version
dbg_packagename=$packagename-dbg
debarch=
set_debarch

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

# 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
# In some cases lsb_release returns the codename as n/a, which breaks dpkg-parsechangelog
elif distribution=$(lsb_release -cs 2>/dev/null) && [ -n "$distribution" ] && [ "$distribution" != "n/a" ]; 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

mkdir -p debian/
echo $debarch > debian/arch

# Generate a simple changelog template
cat <<EOF > debian/changelog
$sourcename ($packageversion) $distribution; urgency=low

  * Custom built Linux kernel.

 -- $maintainer  $(date -R)
EOF

# 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 archive sites, including:
https://www.kernel.org/pub/linux/kernel

Copyright: 1991 - 2017 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

# Generate a control file
cat <<EOF > debian/control
Source: $sourcename
Section: kernel
Priority: optional
Maintainer: $maintainer
Build-Depends: bc, kmod, cpio
Homepage: http://www.kernel.org/

Package: $packagename
Architecture: $debarch
Description: Linux kernel, version $version
 This package contains the Linux kernel, modules and corresponding other
 files, version: $version.

Package: $kernel_headers_packagename
Architecture: $debarch
Description: Linux kernel headers for $version on $debarch
 This package provides kernel header files for $version on $debarch
 .
 This is useful for people who need to build external modules

Package: $dbg_packagename
Section: debug
Architecture: $debarch
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

cat <<EOF > debian/rules
#!/usr/bin/make -f

build:
	\$(MAKE) KERNELRELEASE=${version} ARCH=${ARCH} KBUILD_SRC=

binary-arch:
	\$(MAKE) KERNELRELEASE=${version} ARCH=${ARCH} KBUILD_SRC= intdeb-pkg

clean:
	rm -rf debian/*tmp debian/files
	\$(MAKE) clean

binary: binary-arch
EOF

exit 0
