#!/bin/bash
set -e

if wget -S http://proxyserver.lan.:3128 2>&1 | grep -q Server\:\ squid; then
	http_proxy=http://proxyserver.lan:3128
	https_proxy=http://proxyserver.lan:3128
	ftp_proxy=http://proxyserver.lan:3128
	export http_proxy https_proxy ftp_proxy
fi

VERSION_DEBIAN="${1}"
VERSION="${2}"
VERSION_DEBIAN_BINNMU="${3}"

K_TEMP="${VERSION%%~*}"
K_TEMP="${K_TEMP%%-*}"

K_VERSION="$(echo ${K_TEMP} | cut -d\. -f1)"
K_PATCHLEVEL="$(echo ${K_TEMP} | cut -d\. -f2)"
K_SUBLEVEL="$(echo ${K_TEMP} | cut -d\. -f3)"

if [ -x "$(which pixz)" ]; then
	case $(uname -m) in
		i[0-9]86|x86_64)
			XZ="pixz -8"
			;;
		*)
			# ease up compression on, typically, slow architectures
			XZ="pixz -7"
			;;
	esac
elif [ -x "$(which xz)" ]; then
	XZ="xz -6"
else
	echo "ERROR: neither xz nor pixz found in the PATH" >&2
fi

case "${VERSION}" in
	2\.*)
		MAJOR_VER="${K_VERSION}.${K_PATCHLEVEL}.${K_SUBLEVEL}"
		K_PATCHLEVEL_SYM="${K_PATCHLEVEL}"
		;;
	*)
		MAJOR_VER="${K_VERSION}.${K_PATCHLEVEL}"
		K_PATCHLEVEL_SYM="x"
		;;
esac

RC_VER="${VERSION#*\~}"
[ "x${VERSION%\~*}" = "x${RC_VER}" ] && RC_VER=""

TMP_DIR="$(mktemp -d --tmpdir linux-siduction.XXXXXX)"
pushd "${TMP_DIR}"
	if [ -z "$RC_VER" ]; then
		# stable kernel
		UP_TARBALL="linux-${MAJOR_VER}.tar.gz"
		wget -Nc "https://git.kernel.org/torvalds/t/${UP_TARBALL}"
#		wget -Nc "https://www.kernel.org/pub/linux/kernel/v${K_VERSION}.${K_PATCHLEVEL_SYM}/${UP_TARBALL}"

		if [ -n "${K_SUBLEVEL}" ]; then
			ORIG_TARBALL="linux-siduction_${K_VERSION}.${K_PATCHLEVEL}.${K_SUBLEVEL}.orig.tar.xz"
		else
			ORIG_TARBALL="linux-siduction_${K_VERSION}.${K_PATCHLEVEL}.orig.tar.xz"
		fi
	else
		# rc kernel
		UP_TARBALL="linux-${MAJOR_VER}-${RC_VER}.tar.gz"
		wget -Nc "https://git.kernel.org/torvalds/t/${UP_TARBALL}"
#		cp /home/towo/source/kernel/linux-4.12-rc6.tar.gz ${TMP_DIR}/

		if [ -n "${K_SUBLEVEL}" ]; then
			ORIG_TARBALL="linux-siduction_${K_VERSION}.${K_PATCHLEVEL}.${K_SUBLEVEL}~${RC_VER}.orig.tar.xz"
		else
			ORIG_TARBALL="linux-siduction_${K_VERSION}.${K_PATCHLEVEL}~${RC_VER}.orig.tar.xz"
		fi
	fi
popd >/dev/null 2>&1

mkdir "${TMP_DIR}/linux-${VERSION}"
pushd "${TMP_DIR}/linux-${VERSION}" >/dev/null 2>&1
	tar --strip 1 -xzf "${TMP_DIR}/${UP_TARBALL}"
	rm "${TMP_DIR}/${UP_TARBALL}"

	# remove shipped firmware images
	rm -rf	drivers/video/nvidia \
		drivers/video/riva

	# remove individual blobby files throughout the tree
	rm	Documentation/netlabel/draft-ietf-cipso-ipsecurity-01.txt \
		drivers/media/usb/dvb-usb/af9005-script.h \


	# These include apparently obfuscated code
	rm -rf	drivers/video/fbdev/nvidia/ \
		drivers/video/fbdev/riva/

	echo "mark pruned modules as BROKEN"
	patch -p1 < ${0%$(basename $0)}/../patches/dfsg/orig/disable-pruned-modules.patch
popd >/dev/null 2>&1

pushd "${TMP_DIR}" >/dev/null 2>&1
	echo ""
	echo "create pruned ${ORIG_TARBALL}"
	tar	\
		--exclude \*\\.orig \
		--posix \
		--numeric-owner \
		--owner=0 \
		--group=0 \
		-cf - "linux-${VERSION}" | \
			${XZ} >"${TMP_DIR}/${ORIG_TARBALL}"

	rm -rf ${TMP_DIR}/linux-${VERSION}
popd >/dev/null 2>&1

echo "COMPLETED: ${TMP_DIR}/${ORIG_TARBALL}"
