From a6ed6c0a12897a434548cd9376d0bd670be01d6c Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@robotics.uk.to>
Date: Fri, 20 Nov 2015 17:19:00 +0000
Subject: [PATCH] Prep can use a given disk image

---
 src/freedombone-prep | 43 +++++++++++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 10 deletions(-)

diff --git a/src/freedombone-prep b/src/freedombone-prep
index 2e3a2d9f8..beff4fac5 100755
--- a/src/freedombone-prep
+++ b/src/freedombone-prep
@@ -51,10 +51,11 @@ NAMESERVER2='85.214.20.141'
 
 MICROSD_MOUNT_POINT="/media/$USER"
 
-DEBIAN_FILE_NAME="bone-debian-8.1-console-armhf-2015-07-12-2gb"
+DEBIAN_IMAGE_FILENAME=
+DEBIAN_FILE_NAME="bone-debian-8.1-console-armhf-2015-07-12-2gb.img"
 
 # Downloads for the Debian installer
-DOWNLOAD_LINK1="https://rcn-ee.com/rootfs/bb.org/testing/2015-07-12/console/$DEBIAN_FILE_NAME.img.xz"
+DOWNLOAD_LINK1="https://rcn-ee.com/rootfs/bb.org/testing/2015-07-12/console/$DEBIAN_FILE_NAME.xz"
 
 ROOTFS='rootfs'
 
@@ -106,6 +107,11 @@ case $key in
     MICROSD_DRIVE="$1"
     ;;
     # BBB static IP address on the LAN
+    -i|--image)
+    shift
+    DEBIAN_IMAGE_FILENAME="$1"
+    ;;
+    # BBB static IP address on the LAN
     --ip)
     shift
     BBB_FIXED_IP_ADDRESS="$1"
@@ -172,22 +178,39 @@ if [ ! -d ~/freedombone ]; then
     mkdir ~/freedombone
 fi
 cd ~/freedombone
-if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME.img.xz ]; then
-    if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME.img ]; then
-        wget $DOWNLOAD_LINK1
+
+# extract the image
+if [ $DEBIAN_IMAGE_FILENAME ]; then
+    # debian image filename specified as an option
+    if [[ $DEBIAN_IMAGE_FILENAME == *".bz2" ]]; then
+        tar -xjvf $DEBIAN_IMAGE_FILENAME
+        pattern="*.img"
+        files=( $pattern )
+        DEBIAN_IMAGE_FILENAME=${files[0]}
+    fi
+    if [[ $DEBIAN_IMAGE_FILENAME != *".img" ]]; then
+        echo 'Debian image (.img) file expected'
+        exit 62394
+    fi
+    DEBIAN_FILE_NAME=$DEBIAN_IMAGE_FILENAME
+else
+    # default debian image downloaded from elsewhere
+    if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME.xz ]; then
+        if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME ]; then
+            wget $DOWNLOAD_LINK1
+        fi
     fi
+    xz -d $DEBIAN_FILE_NAME.xz
 fi
 
-echo 'Extracting image...'
-xz -d $DEBIAN_FILE_NAME.img.xz
-if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME.img ]; then
-    echo "Couldn't extract image"
+if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME ]; then
+    echo "Couldn't extract image $DEBIAN_FILE_NAME"
     exit 4
 fi
 
 cd ~/freedombone
 echo 'Flashing image. This may take a while.'
-$SUDO dd if=$DEBIAN_FILE_NAME.img of=$MICROSD_DRIVE
+$SUDO dd if=$DEBIAN_FILE_NAME of=$MICROSD_DRIVE
 sync
 
 sleep 5
-- 
GitLab