From e3186c17414c1a22fc41ec43731476522c1032e2 Mon Sep 17 00:00:00 2001
From: Anta Huang <anta.huang@eurecom.fr>
Date: Fri, 17 Feb 2017 13:56:00 +0100
Subject: [PATCH] Issue #211 - enhance build script for snapping package -
 build script has ability to indicate location for downloading uhd images -
 one simple wrapper to set environment variables and initiate another program
 (supposed to be lte-softmodem)

---
 cmake_targets/build_oai           | 10 +++++++++-
 cmake_targets/snap_environment.sh |  3 +++
 cmake_targets/tools/build_helper  |  6 +++++-
 3 files changed, 17 insertions(+), 2 deletions(-)
 create mode 100755 cmake_targets/snap_environment.sh

diff --git a/cmake_targets/build_oai b/cmake_targets/build_oai
index 2272a338be..6d22fe9ba4 100755
--- a/cmake_targets/build_oai
+++ b/cmake_targets/build_oai
@@ -292,6 +292,10 @@ function main() {
             UE_AUTOTEST_TRACE="True"
             echo_info "Enabling autotest specific trace for UE"
             shift 1;;
+        --uhd-images-dir)
+            UHD_IMAGES_DIR=$2
+            echo_info "Downloading UHD images in the indicated location"
+            shift 2;;
         -h | --help)
             print_help
             exit 1;;
@@ -362,6 +366,10 @@ function main() {
   echo_info "Flags for Deadline scheduler: $DEADLINE_SCHEDULER_FLAG_USER"
   echo_info "Flags for CPU Affinity: $CPU_AFFINITY_FLAG_USER"
 
+  if [ -n "$UHD_IMAGES_DIR" ] && [ -z "$INSTALL_EXTERNAL" ]; then
+    echo_error "UHD images download settings will not be applied without -I present"
+    exit
+  fi
   ############################################
   # setting and printing OAI envs, we should check here
   ############################################
@@ -399,7 +407,7 @@ function main() {
       echo_info "installing packages for USRP support"
       check_install_usrp_uhd_driver
       if [ ! "$DISABLE_HARDWARE_DEPENDENCY" == "True" ]; then
-        install_usrp_uhd_driver
+        install_usrp_uhd_driver $UHD_IMAGES_DIR
       fi
     fi 
     if [ "$HW" == "OAI_BLADERF" ] ; then
diff --git a/cmake_targets/snap_environment.sh b/cmake_targets/snap_environment.sh
new file mode 100755
index 0000000000..1c0725d8b5
--- /dev/null
+++ b/cmake_targets/snap_environment.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+export UHD_IMAGES_DIR=$SNAP/uhd_images
+exec $@
diff --git a/cmake_targets/tools/build_helper b/cmake_targets/tools/build_helper
index f73078afc5..c52c175c05 100755
--- a/cmake_targets/tools/build_helper
+++ b/cmake_targets/tools/build_helper
@@ -216,7 +216,11 @@ install_usrp_uhd_driver() {
         # We move uhd-host apart because it depends on linux kernel version
         # On newer kernels, it fails to install
         $SUDO apt-get -y install uhd-host
-        $SUDO uhd_images_downloader 
+        if [ -z $1 ]; then
+          $SUDO uhd_images_downloader
+        else
+          $SUDO uhd_images_downloader -i $1
+        fi
 }
 
 check_install_bladerf_driver(){
-- 
2.26.2