Merge pull request #11 from ErickReyesR/master

adeb: add prebuilt headers from Android common kernel
diff --git a/README.md b/README.md
index a81a89d..23ae44d 100644
--- a/README.md
+++ b/README.md
@@ -51,12 +51,13 @@
 # Add some short cuts:
 sudo ln -s $(pwd)/adeb /usr/bin/adeb
 
-# For cached image downloads which result in a huge speed-up,
-# You could set the ADEB_REPO_URL environment variable in your
-# bashrc file.
+# Cached image downloads result in a huge speed-up. These are automatic if you
+# cloned the repository using git. However, if you downloaded the repository
+# as a zip file (or you want to host images elsewere), you could set the
+# ADEB_REPO_URL environment variable in your bashrc file.
 # Disclaimer: Google is not liable for the below URL and this
 #             is just an example.
-export ADEB_REPO_URL="github.com/joelagnel/"
+export ADEB_REPO_URL="github.com/joelagnel/adeb/"
 ```
 
 * Installing adeb onto your device:
diff --git a/addons/bashrc.common b/addons/bashrc.common
index f357b6f..14e09d3 100644
--- a/addons/bashrc.common
+++ b/addons/bashrc.common
@@ -17,6 +17,16 @@
 
 	if [ $this_vers -eq $h_vers ]; then
 		override_vers=0
+	else
+		this_major=$(($this_vers / (256 * 256)))
+		this_minor=$(($this_vers / 256 % 256))
+		h_major=$(($h_vers / (256 * 256)))
+		h_minor=$(($h_vers / 256 % 256))
+		if [ $this_major -ne $h_major ] || [ $this_minor -ne $h_minor ]; then
+			echo "Error: kernel version does not match installed headers ($this_major.$this_minor <> $h_major.$h_minor)"
+			echo "BCC will not work"
+			override_vers=0
+		fi
 	fi
 fi
 
diff --git a/androdeb b/androdeb
index 15f7b54..ee898fb 100755
--- a/androdeb
+++ b/androdeb
@@ -204,6 +204,45 @@
 	c_info "All done! Run \"adeb shell\" to enter environment"
 }
 
+function detect_repo_url() {
+	ADEB_REPO_URL=`cd $spath && git config -l | grep -m1 remote | grep url | sed -e "s/.*url=//" \
+		 -e "s/.*@//"  \
+		 -e "s/https:\/\///" \
+		 -e "s/:/\//"  \
+		 -e "s/\.git$//"`"/"
+	c_info "Detected URL: $ADEB_REPO_URL"
+}
+
+function check_repo_url () {
+	if [ -z $ADEB_REPO_URL ]; then
+		c_info "No repository URL provided in enviromnent. Attempting to auto-detect it"
+		detect_repo_url
+	fi
+
+	if [ -z $ADEB_REPO_URL ]; then
+		c_warning "Automatic download is disabled. To enable it, please set the \$ADEB_REPO_URL"
+		c_warning "environment variable as recommended in the setup instructions in the README.md"
+		do_cleanup
+		exit 0
+	fi
+}
+
+function download_headers() {
+	KERNEL_MAJOR=`$ADB shell uname -r | cut -d - -f 1 | cut -d . -f 1`
+	KERNEL_MINOR=`$ADB shell uname -r | cut -d - -f 1 | cut -d . -f 2`
+	KERNEL_VERSION="$KERNEL_MAJOR.$KERNEL_MINOR"
+	PREBUILT_HEADERS_FILE=headers-$ARCH-$KERNEL_VERSION.tar.gz.zip
+
+	check_repo_url
+
+	curl -L https://$ADEB_REPO_URL/releases/download/$VERSION/$PREBUILT_HEADERS_FILE --output $TDIR_ABS/$PREBUILT_HEADERS_FILE ||
+		   die 9 "Failed to download kernel headers. Please check your internet connection and repository URL"
+
+	unzip -e $TDIR_ABS/$PREBUILT_HEADERS_FILE -d $TDIR_ABS/ ||
+		   die 10 "Failed to download kernel headers. Kernel $KERNEL_VERSION for $ARCH may not be supported or ADEB_REPO_URL is incorrect."
+	KERNELHDRS=$TDIR_ABS/`echo "$PREBUILT_HEADERS_FILE" | sed "s/.zip//"`
+}
+
 # Prepare is the last command checked
 if [ -z "$PREPARE" ]; then usage; fi
 
@@ -225,14 +264,9 @@
    c_info "Downloading Androdeb from the web..."; c_info ""
 
    # Github dropped tar gz support! ##?#??#! Now we've to zip everything.
-   if [ -z $ADEB_REPO_URL ]; then
-      c_warning "Automatic download is disabled. To enable it, please set the \$ADEB_REPO_URL"
-      c_warning "environment variable as recommended in the setup instructions in the README.md"
-      do_cleanup
-      exit 0
-   fi
+   check_repo_url
 
-   curl -L https://$ADEB_REPO_URL/adeb/releases/download/$VERSION/$FNAME --output $TDIR_ABS/$FNAME ||
+   curl -L https://$ADEB_REPO_URL/releases/download/$VERSION/$FNAME --output $TDIR_ABS/$FNAME ||
 		   die 9 "Failed to download adeb release."
 
    unzip -e $TDIR_ABS/$FNAME -d $TDIR_ABS/ ||
@@ -240,6 +274,11 @@
    TARF=$TDIR_ABS/$FNAME_UZ
 fi
 
+if [ ! -z "$FULL" ] && [ -z "$KERNELSRC" ] && [ -z "$KERNELHDRS" ]; then
+	c_info "Kernel headers are needed but none were provided. Downloading pre-built headers"
+	download_headers
+fi
+
 OUT_TMP=$TDIR/debian; rm -rf $OUT_TMP; mkdir -p $OUT_TMP
 
 # Unpack the supplied kernel headers tar.gz directly into androdeb root
diff --git a/utils/packheaders.sh b/utils/packheaders.sh
new file mode 100755
index 0000000..d10b81b
--- /dev/null
+++ b/utils/packheaders.sh
@@ -0,0 +1,25 @@
+#!/bin/bash
+
+# Utility to build kernel headers tar/zip file
+# must be run from the top level of a kernel source directory
+# and supplied an output file name
+
+MKTEMP=0; if [[ -z ${TDIR+x} ]]  || [[ ! -d "${TDIR}" ]]; then
+	TDIR=`mktemp -d`; MKTEMP=1; fi
+rm -rf $TDIR/*
+TDIR_ABS=$( cd "$TDIR" ; pwd -P )
+
+
+if [ $# -ne 1 ]; then
+  echo "usage: makeheaders.sh <output file name>"
+  exit 0
+fi
+
+mkdir -p $TDIR_ABS/kernel-headers
+
+find arch -name include -type d -print | xargs -n1 -i: find : -type f -exec cp --parents {} $TDIR_ABS/kernel-headers/ \;
+find include -exec cp --parents {} $TDIR_ABS/kernel-headers/ 2> /dev/null \;
+tar -zcf $1 --directory=$TDIR_ABS kernel-headers
+
+zip -r $1.zip $1
+rm -rf $TDIR/*; if [ $MKTEMP -eq 1 ]; then rm -rf $TDIR; fi