--- a/ohbuild.sh
+++ b/ohbuild.sh
@@ -30,15 +30,16 @@
 # There are 3 modes of operation:
 #   -c: clone, adjust, trim the source directories and produce a tar file
 #   -b: clone, adjust the source dirs and build
-#   -t: extract tar file and build.
+#   -t: extract tar file and build (this is used mostly for packaging).
 #
 # When cloning, we checkout a known ok version (it's more or less
-# random, based on the last date I tried this, sometimes more recent
-# versions don't build versions don't build), then build the different
-# dirs.
+# random, based on the last date I tried this successfully, sometimes
+# more recent versions don't build), then apply some small patches
+# mostly related to the build mechanism, and build in the different
+# top directories.
 #
 # When producing the tar file, we get rid of the .git directory and a
-# bunch of other things to reduce the size
+# bunch of other things to reduce the size.
 
 
 fatal()
@@ -93,6 +94,9 @@
 arch=
 debug=
 
+test -d ohpatches || fatal "Can't find ohpatches directory? Must run in the top sc2mpd source dir"
+srcdir=`pwd`
+
 test -d $topdir || mkdir $topdir || fatal "Can't create $topdir"
 
 cd $topdir || exit 1
@@ -100,9 +104,25 @@
 topdir=`pwd`
 
 otherfiles=`echo *.*`
-test "$otherfiles"  != '*.*' && fatal topdir should not contain files
-
-clone_oh()
+test "$otherfiles"  != '*.*' && fatal "topdir should not contain files (found $otherfiles)"
+
+# Get patches from $srcdir/ohpatches/$1 and apply them. 
+apply_patches()
+{
+    subdir=$1
+    if test ! -d ${srcdir}/ohpatches/${subdir} ; then
+        echo "-- No patches for $subdir --"
+        return
+    else
+        echo "-- Patching in $subdir --"
+    fi
+    cd $topdir/$subdir/
+    for p in ${srcdir}/ohpatches/${subdir}/*.patch;do
+        patch -p1 < $p || exit 1
+    done
+}
+
+gitclone_oh()
 {
     echo "Cloning OpenHome from git repos into $topdir"
     cd $topdir
@@ -118,130 +138,35 @@
         echo $dir
         test ! -d $dir && git clone $rep
     done
-
-
+}
+
+clone_oh()
+{
+    gitclone_oh
+    
     cd $topdir/ohNet
     #  ohNet_1.17.2857, Mon Feb 19 00:40:18 2018 +0000
     git checkout 07831fe44c45ccde939ceaa4e99e46d2dbd2cf4e || exit 1
-    git checkout Makefile
-    # Note: the 'make: o: Command not found' errors originate in
-    # common.mak and are due to variable t4 being undefined. t4 is
-    # normally defined as 'mono' in T4Linux.mak, included in Makefile
-    # only if 'uset4' is set (which it is not by default). Common.mak
-    # should heed uset4, but it does not. This does not seem to have
-    # consequences, and the errors are suppressed by defining t4 as 'echo'
-
-    # odroid64: aarch64-linux-gnu.
-    # odroid32,rasp30: arm-linux-gnueabihf,
-    # cubox: armv7hl-redhat-linux-gnueabi
-
-    patch -p1 << "EOF"
-diff --git a/Makefile b/Makefile
-index 29f2d6a..038b05e 100644
---- a/Makefile
-+++ b/Makefile
-@@ -82,12 +82,19 @@ else
-     ifneq (,$(findstring arm,$(gcc_machine)))
-         ifneq (,$(findstring linux-gnueabihf,$(gcc_machine)))
-             detected_openhome_architecture = armhf
-+        else ifneq (,$(findstring armv7hl,$(gcc_machine)))
-+            # cubox: armv7hl-redhat-linux-gnueabi
-+            detected_openhome_architecture = armhf
-         else ifeq (${detected_openhome_system},Qnap)
-             detected_openhome_architecture = x19
-         else
-             detected_openhome_architecture = armel
-         endif
-     endif
-+    ifneq (,$(findstring aarch64,$(gcc_machine)))
-+      # odroid64: aarch64-linux-gnu
-+      detected_openhome_architecture = arm64
-+    endif
-     ifneq (,$(findstring i686,$(gcc_machine)))
-       detected_openhome_architecture = x86
-     endif
-@@ -375,6 +382,10 @@ mkdir = mkdir -p
- rmdir = rm -rf
- uset4 = no
- 
-+# This avoids errors in Generated/Devices.mak (they apparently have no
-+# consequences anyway because we use make native_only=yes)
-+t4 = echo
-+
- ifeq ($(managed_only), yes)
- build_targets_base = make_obj_dir ohNet.net.dll CpProxyDotNetAssemblies DvDeviceDotNetAssemblies
- else
-EOF
+    git checkout .
+    apply_patches ohNet
 
     cd  $topdir/ohNetGenerated
     # Tue May 9 08:54:47 2017 
     git checkout e3edb912410d4c5a4d5323bb1e9c27660a42d78f || exit 1
-    git checkout Makefile
-    patch -p1 << "EOF"
-diff --git a/Makefile b/Makefile
-index bf7a1ba..49bffaf 100644
---- a/Makefile
-+++ b/Makefile
-@@ -70,14 +70,17 @@ else
-     else ifneq (,$(findstring linux,$(gcc_machine)))
-       detected_openhome_system = Linux
-     endif
--    ifeq ($(gcc_machine),arm-none-linux-gnueabi)
--      detected_openhome_architecture = armel
--    endif
--    ifeq ($(gcc_machine),arm-linux-gnueabi)
--      detected_openhome_architecture = armel
--    endif
--    ifeq ($(gcc_machine),arm-linux-gnueabihf)
--      detected_openhome_architecture = armhf
-+    ifneq (,$(findstring arm,$(gcc_machine)))
-+        ifneq (,$(findstring linux-gnueabihf,$(gcc_machine)))
-+            detected_openhome_architecture = armhf
-+        else ifneq (,$(findstring armv7hl,$(gcc_machine)))
-+            # cubox: armv7hl-redhat-linux-gnueabi
-+            detected_openhome_architecture = armhf
-+        else ifeq (${detected_openhome_system},Qnap)
-+            detected_openhome_architecture = x19
-+        else
-+            detected_openhome_architecture = armel
-+        endif
-     endif
-     ifneq (,$(findstring i686,$(gcc_machine)))
-       detected_openhome_architecture = x86
-EOF
-patch -p1 << "EOF"
-diff -u ./Common.mak ../../openhome/ohNetGenerated/Common.mak
---- ./Common.mak	2017-09-19 10:51:13.000000000 +0200
-+++ ../../openhome/ohNetGenerated/Common.mak	2017-09-19 13:21:25.984071432 +0200
-@@ -17,20 +17,6 @@
- $(objdir)ohnet-generated.jar : CpProxyJavaClasses DvDeviceJavaClasses
- 	$(jar) $(jarflags) $(objdir)ohnet-generated.jar -C $(objdir) org
- 
--Generated$(dirsep)GenerateSourceFiles.mak : $(tt) OpenHome$(dirsep)Net$(dirsep)Service$(dirsep)Services.xml OpenHome/Net/T4/Templates/UpnpMakeT4.tt
--	$(mkdir) Generated
--	$(t4) -o Generated$(dirsep)GenerateSourceFiles.mak OpenHome/Net/T4/Templates/UpnpMakeT4.tt -a xml:OpenHome/Net/Service/Services.xml
--	@echo Attention: a makefile has been re-generated.
--
--Generated$(dirsep)Proxies.mak : $(tt) OpenHome$(dirsep)Net$(dirsep)Service$(dirsep)Services.xml OpenHome/Net/T4/Templates/CpUpnpMakeProxies.tt
--	$(mkdir) Generated
--	$(t4) -o Generated$(dirsep)Proxies.mak OpenHome/Net/T4/Templates/CpUpnpMakeProxies.tt -a xml:OpenHome/Net/Service/Services.xml
--	@echo Attention: a makefile has been re-generated.
--
--Generated$(dirsep)Devices.mak : $(tt) OpenHome$(dirsep)Net$(dirsep)Service$(dirsep)Services.xml OpenHome/Net/T4/Templates/DvUpnpMakeDevices.tt
--	$(mkdir) Generated
--	$(t4) -o Generated$(dirsep)Devices.mak OpenHome/Net/T4/Templates/DvUpnpMakeDevices.tt -a xml:OpenHome/Net/Service/Services.xml
--	@echo Attention: a makefile has been re-generated.
- 
- native_targets = make_obj_dir copy_build_includes proxies devices
- 
-EOF
+    git checkout .
+    apply_patches ohNetGenerated
+
     cd  $topdir/ohTopology
     # Wed Mar 22 11:15:28 2017 +0000
     git checkout cc09c09da4be8d3d04adae5b8f0daaf8450906a3 || exit 1
+    git checkout .
+    apply_patches ohTopology
 
     cd  $topdir/ohSongcast
     # Wed May 30 15:56:56 2018 +0200
     git checkout c1cb53cce61e727b6096fc38d921802352b9f66b || exit 1
+    git checkout .
+    apply_patches ohSongcast
 }
 
 make_tarfile()
@@ -349,7 +274,7 @@
 
 official_way()
 {
-    # from README, actually Does not work, for reference. Issues probably have
+    # from README, actually does not work, for reference. Issues probably have
     # something to do with lacking mono or wrong version
     cd ohNet
     make ohNetCore proxies devices TestFramework