#!/bin/sh -x

# Script to build cyanogen from scratch with kernel
# assumes you have a device attached if false is executed
# use the following code bases:
# kernel: https://github.com/pershoot/gtab-2632.git; gingerbread (5a843d6 serial8250: ratelimit "too much work" error) <-- known to work...
# cyanogen: repo init -u git://github.com/CyanogenMod/android.git -b gingerbread  <-- Worked on Aug 27 anyway ;)

TOP=`pwd`
KERN=$TOP/gtab-2632

if [ "1" == "1" ] ; then
	repo init -u git://github.com/CyanogenMod/android.git -b gingerbread
	repo sync

	git clone https://github.com/pershoot/gtab-2632.git
fi

CROSS=$TOP/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-
PATH=$TOP/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin:$PATH

cd $KERN
git fetch origin
git checkout origin/gingerbread

make ARCH=arm CROSS_COMPILE=$CROSS mrproper
#make ARCH=arm CROSS_COMPILE=$CROSS pershoot_tegra_harmony_smba1002_android_defconfig
make ARCH=arm CROSS_COMPILE=$CROSS pershoot_tegra_harmony_android-newbl_defconfig
time make ARCH=arm CROSS_COMPILE=$CROSS -j 4
make ARCH=arm CROSS_COMPILE=$CROSS modules_install INSTALL_MOD_PATH=$TOP/out/target/product/smb_a1002/system

cp arch/arm/boot/zImage $TOP/device/malata/smb_a1002/kernel
cp drivers/net/wireless/bcm4329/dhd.ko $TOP/device/malata/smb_a1002/wifi/bcm4329.ko


if [ "1" == "1" ] ; then

	cd $TOP
	echo "Did you forget to run ./vendor/cyanogen/get-rommanager "
	./vendor/cyanogen/get-rommanager

	echo "Did you forget to extract propritary files from target?"
	cd $TOP/device/malata/smb_a1002/
	./extract-files.sh 
	#if pulling files from a cm7 rom you get the following errors:
	#remote object '/system/usr/keylayout/gpio-keys.kl' does not exist
	#remote object '/system/usr/keylayout/nvec_keyboard.kl' does not exist
	#remote object '/system/usr/keylayout/tegra-kbc.kl' does not exist
	#remote object '/system/usr/keylayout/usb_keyboard_102_en_us.kl' does not exist

	#fix it so that update image can be isntalled on old CWM-3.0.2.8 that looks for upc300.
	sed -i 's/TARGET_OTA_ASSERT_DEVICE := harmony,smb_a1002$/TARGET_OTA_ASSERT_DEVICE := harmony,smb_a1002,upc300/' BoardConfig.mk

	cd $TOP/vendor/malata/smb_a1002/proprietary
	adb pull /system/usr/keychars/gpio-keys.kl .
	adb pull /system/usr/keychars/nvec_keyboard.kl .
	adb pull /system/usr/keychars/tegra-kbc.kl .
	adb pull /system/usr/keychars/usb_keyboard_102_en_us.kl .
fi

cd $TOP
source $TOP/build/envsetup.sh
lunch cyanogen_smb_a1002-eng
rm nohup.out
time nohup make -j 4 bacon showcommands
time nohup make recoveryzip  showcommands
#note that out/.../utilities/update.zip is setup wrong and you need to rewrite its updater-script to use recovery.img correctly.


echo "DONE"


