KERNEL_DIR=/media/scottb/linaro/kernel/output/kernel-hikey-mali
DEBIAN_DIR=$HOME/Downloads/hikey/2015-03-24/debian
EMMC_IMAGE=hikey-jessie_alip_20150324-135.emmc.img
rm -f boot-fat.img
rm -fr boot-files
mkdir boot-fat
mkdir boot-files
echo "console=tty0 console=ttyAMA0,115200n8 root=/dev/mmcblk0p7 rootwait rw" > boot-files/cmdline
sudo cp -a $KERNEL_DIR/arch/arm64/boot/Image boot-files/Image
sudo cp -a $KERNEL_DIR/arch/arm64/boot/dts/hi6220-hikey.dtb boot-files/lcb.dtb
sudo cp -a $DEBIAN_DIR/initrd.img-3.18.0-linaro-hikey boot-files/ramdisk.img

dd if=/dev/zero of=boot-fat.img bs=512 count=131072
sudo mkfs.fat -n "BOOT IMG" boot-fat.img
sudo mount -o loop,rw,sync boot-fat.img boot-fat
sudo cp -a boot-files/* boot-fat/
sudo umount boot-fat
rm -rf boot-fat

rm -rf raw.img
mkdir raw-files
simg2img $DEBIAN_DIR/$EMMC_IMAGE raw.img
sudo mount raw.img raw-files

sudo chmod 0600 raw-files/etc/network/interfaces
sudo cp rootfs-files/wlan0 raw-files/etc/network/interfaces.d
sudo cp rootfs-files/eth0 raw-files/etc/network/interfaces.d

sudo cp rootfs-files/99-sysctl.conf raw-files/etc/sysctl.d

SCRIPT_DIR=`pwd`
MODULES_DIR=`pwd`/raw-files

cd $KERNEL_DIR/source
sudo make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=$KERNEL_DIR INSTALL_MOD_PATH=$MODULES_DIR modules_install
cd $SCRIPT_DIR

#sudo mkdir raw-files/root/.ssh/
#sudo cp my_id_rsa.pub raw-files/root/.ssh/authorized_keys

sudo make_ext4fs -L rootfs -l 1500M -s $EMMC_IMAGE raw-files
sudo umount raw-files
