diff --git a/Tools/scripts/arduino_version.sh b/Tools/scripts/arduino_version.sh
new file mode 100755
index 0000000000000000000000000000000000000000..c87fb7baca05ea12d89d4128c6a35d50c3a23a80
--- /dev/null
+++ b/Tools/scripts/arduino_version.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+# find arduino version
+
+ARDUINO=$1
+if test -f $ARDUINO/lib/version.txt; then
+    # arduino 1.0 uses this file
+    ver=$(head -1 $ARDUINO/lib/version.txt | cut -c1-4)
+    # cope with pre 1.0 versions
+    leading=$(echo $ver | cut -c1)
+    if [ "$leading" = "0" ]; then
+	echo $ver
+	exit 0
+    fi
+    major=$(echo $ver | cut -d. -f1)
+    minor=$(echo $ver | cut -d. -f2)
+    v=$(expr $major \* 100 + $minor)
+    echo $v
+elif test -f $ARDUINO/revisions.txt; then
+    ARDUINO_VER=$(head -1 $ARDUINO/revisions.txt | cut -d' ' -f 2)
+else
+    echo "UNKNOWN"
+fi
diff --git a/libraries/AP_Common/Arduino.mk b/libraries/AP_Common/Arduino.mk
index a87d99767cdb802d9b4d17cbee452d7b00d583f5..381125f2535eabddfb8fcb6731bf2196fd0017c5 100644
--- a/libraries/AP_Common/Arduino.mk
+++ b/libraries/AP_Common/Arduino.mk
@@ -333,17 +333,8 @@ LIBOBJS			:=	$(SKETCHLIBOBJS) $(ARDUINOLIBOBJS)
 # *duino core
 #
 
-# Pull the Arduino version from the revisions.txt file
-#
-# XXX can we count on this?  If not, what?
-ARDUINO_VERS	:=	$(shell expr `head -1 $(ARDUINO)/revisions.txt | cut -d ' ' -f 2`)
-# If the version is not a number, try it again, using another file
-ifneq ($(ARDUINO_VERS),$(shell echo $(ARDUINO_VERS) | sed 's/[^0-9]*//g'))
-	ARDUINO_VERS	:=	$(shell expr `head -1 $(ARDUINO)/lib/version.txt | cut -d ' ' -f 2`)
-endif
-ifneq ($(ARDUINO_VERS),$(shell echo $(ARDUINO_VERS) | sed 's/[^0-9]*//g'))
-	$(warning Could not determine Arduino version)
-endif
+# Pull the Arduino version
+ARDUINO_VERS	:=	$(shell $(SKETCHBOOK)/Tools/scripts/arduino_version.sh $(ARDUINO))
 
 # Find the hardware directory to use
 HARDWARE_DIR		:=	$(firstword $(wildcard $(SKETCHBOOK)/hardware/$(HARDWARE) \
@@ -384,7 +375,7 @@ CORESRC_PATTERNS	=	$(foreach suffix,/*.cpp /*.c /*.S,$(addsuffix $(suffix),$(COR
 CORESRCS		:=	$(wildcard $(CORESRC_PATTERNS))
 
 # Include spec for core includes
-COREINCLUDES		=	-I$(CORESRC_DIR)
+COREINCLUDES		=	-I$(CORESRC_DIR) -I$(HARDWARE_DIR)/variants/mega
 
 # Hardware object files
 CORELIBOBJS		:=	$(subst $(CORESRC_DIR),$(BUILDROOT)/$(HARDWARE),$(CORESRCS))
@@ -543,15 +534,15 @@ $(BUILDROOT)/libraries/%.o: $(ARDUINO)/libraries/%.S
 #
 $(BUILDROOT)/$(HARDWARE)/%.o: $(CORESRC_DIR)/%.cpp
 	$(RULEHDR)
-	$(v)$(CXX) $(filter-out -W%,$(CXXFLAGS)) -c -o $@ $< -I$(CORESRC_DIR)
+	$(v)$(CXX) $(filter-out -W%,$(CXXFLAGS)) -c -o $@ $< $(COREINCLUDES)
 
 $(BUILDROOT)/$(HARDWARE)/%.o: $(CORESRC_DIR)/%.c
 	@mkdir -p $(dir $@)
-	$(v)$(CC) $(filter-out -W%,$(CFLAGS)) -c -o $@ $< -I$(CORESRC_DIR)
+	$(v)$(CC) $(filter-out -W%,$(CFLAGS)) -c -o $@ $< $(COREINCLUDES)
 
 $(BUILDROOT)/$(HARDWARE)/%.o: $(CORESRC_DIR)/%.S
 	$(RULEHDR)
-	$(v)$(AS) $(ASFLAGS) -c -o $@ $< -I$(CORESRC_DIR)
+	$(v)$(AS) $(ASFLAGS) -c -o $@ $< $(COREINCLUDES)
 
 #
 # Build the core library
@@ -581,7 +572,11 @@ $(SKETCHCPP):	$(SKETCHCPP_SRC)
 	$(RULEHDR)
 	$(v)$(AWK) -v mode=header '$(SKETCH_SPLITTER)'   $(SKETCHCPP_SRC) >  $@
 	$(v)echo "#line 1 \"autogenerated\""                              >> $@
+	$(v)echo "#if defined(ARDUINO) && ARDUINO >= 100"                 >> $@
+	$(v)echo "#include \"Arduino.h\""                                 >> $@
+	$(v)echo "#else"                                                  >> $@
 	$(v)echo "#include \"WProgram.h\""                                >> $@
+	$(v)echo "#endif"                                                 >> $@
 	$(v)$(AWK)                '$(SKETCH_PROTOTYPER)' $(SKETCHCPP_SRC) >> $@
 	$(v)$(AWK) -v mode=body   '$(SKETCH_SPLITTER)'   $(SKETCHCPP_SRC) >> $@