#!/bin/bash
#
# script to automate building kernel for the Zenbook laptop on Debian based distributions
# (C) 2012 Bartosz Fenski <fenio@debian.org>
# Version 0.0.2

# fixed DSDT table
# patch to recognize BT
# Eugeni Dodonov's patch to be able to use rc6 with normal/deep/deepest options
# patch to support Sentelic touchpad

# usage:
# ./zenbook-kernel VERSION
# ie ./zenbook-kernel 3.3-rc3

# changelog
# 0.0.2 support DSDT table of UX21E, detects it throught dmidecode
# 0.0.1 initial version

set -e

if [ -z $1 ];
	then 
		echo "Usage:"
		echo "./zenbook-kernel VERSION"
		exit 1;
fi

model=`sudo dmidecode -s system-product-name`

if [ $model == "UX31E" ]; then
	dsdt=ux31e_dsdt;
else
	dsdt=ux21e_dsdt;
fi

echo "Installing dependencies"
sudo apt-get install build-essential kernel-package fakeroot libncurses5-dev git iasl

kernel=linux-$1.tar.bz2

test -d kernel || mkdir kernel
cd kernel

echo "Downloading kernel"
test -f $kernel || wget http://www.kernel.org/pub/linux/kernel/v3.0/testing/linux-$1.tar.bz2

echo "Unpacking kernel"
test -d linux-$1 || tar jxf $kernel

echo "Copying current configuration"
cp /boot/config-`uname -r` linux-$1/.config

echo "Getting touchpad driver"
test -d sentelic || git clone git://github.com/saaros/sentelic.git
cp sentelic/src/sentelic.* linux-$1/drivers/input/mouse/

echo "Fixing DSDT table"
test -f $dsdt.dsl || wget http://files.benesovi.eu/ux31e/$dsdt.dsl
iasl -tc $dsdt.dsl
sed -ie 's/# CONFIG_ACPI_CUSTOM_DSDT is not set/CONFIG_ACPI_CUSTOM_DSDT=y/g' linux-$1/.config 
sed -ie "s@CONFIG_ACPI_CUSTOM_DSDT_FILE.*@CONFIG_ACPI_CUSTOM_DSDT_FILE=\"`pwd`/$dsdt.hex\"@g" linux-$1/.config

echo "Getting RC6 patch"
test -f rc6.patch || wget -O rc6.patch http://people.canonical.com/~ogasawara/eugeni/rc6/0001-drm-i915-allow-to-select-rc6-modes-via-kernel-parame.patch
echo "Getting BT patch"
test -f bluetooth.patch || wget http://fenski.pl/zenbook/bluetooth.patch

cd linux-$1
echo "Patching kernel with RC6 patch"
patch -p1 < ../rc6.patch
echo "Patching kernel with BT patch"
patch -p1 < ../bluetooth.patch

make menuconfig

fakeroot make-kpkg clean
fakeroot make-kpkg --jobs=4 --initrd --append-to-version=-custom --revision=`date +'%Y%m%d%H%M'` kernel_image kernel_headers modules_image

sudo dpkg -i ../*.deb


