From 3de9b57eaafd56200d31143222753a0200b7bc28 Mon Sep 17 00:00:00 2001
From: Krisjanis Rijnieks <krisjanis.rijnieks@gmail.com>
Date: Sun, 6 May 2018 00:41:53 +0200
Subject: [PATCH] Improve travis cfg for rpi

---
 .travis.yml               | 86 ++++++++++++++-------------------------
 scripts/ci/rpi/mkimage.sh | 79 ++++++++++++++++++++++++++++++++++-
 scripts/ci/rpi/pisetup.sh |  0
 3 files changed, 108 insertions(+), 57 deletions(-)
 create mode 100644 scripts/ci/rpi/pisetup.sh

diff --git a/.travis.yml b/.travis.yml
index 7982852..999a437 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,27 +1,12 @@
-# This file allows testing your addon using travis CI servers to use it you'll need to
-# create an account in travis.org and enable your addon there. 
-#
-# By default it will test linux 64bit and osx against the master and stable OF branches. 
-# Other platforms can be enabled by uncommenting the corresponding sections.
-#
-# If any extra install is needed to use the addon it can be included in the corresponding
-# install script in:
-#
-# scripts/ci/$TARGET/install.sh
-#
-
 language: c++
 compiler: gcc
 sudo: true
 matrix:
   include:
-  # fully specify builds, include can't dynamically expand matrix entries
-  # relative order of sudo and env is important so that addons: is recognized
-
   - os: linux
     dist: trusty
     sudo: required
-    env: TARGET="rpi"
+    env: TARGET="rpi" SCRIPT="pisetup.sh" IMAGE="opm-${TRAVIS_TAG}.img" RPI_ZIP="raspbian_lite-2017-07-05-of_v0.9.8.img.zip" RPI_URL="https://github.com/kr15h/openframeworks-rpi-image/releases/download/v1.0.0/${RPI_ZIP}"
     addons:
       apt:
         packages:
@@ -34,8 +19,17 @@ matrix:
         - zip
     script:
     - sudo bash ./scripts/ci/rpi/mkimage.sh
+    - zip ${IMAGE}.zip ${IMAGE}
+    deploy:
+      provider: releases
+      api_key:
+        secure: Z1+IhOaCxrlKGqO3sw2Mz4gD7XaFykI+YA559V+FvynxHilu04jQtnGWUReTr2eHFFzZDzC80Ydk+ORDEIFEtA3VsM/BFJRm9jqtxcY+wtKmXLbh4k6fdgjKFH2QNOScoPj3acuuzV/5/RBXN+avLH53xx8uZkMNPPRpAyVvtZjZ0OSi1nEL5qg/Vnu+RhUM4ISy8r5JMcDghtFTo9hmy3sTPOSS9OSndi/guOgFHBo4x2mVtOKkIa2H/aKf1QR9h+KUK8CG/SYYSMf8gLZ/TZq9LO6wqaBPgqd2DZVPZvxCZ4JmUc7ZlnpBX2zo/WjUXKJyn3dqA/4sr8A74DqbNsZc6Xw4/DEeDGszmX5rudlp07kSXQudUgKILu72SLKMsMmmZ7FBjVS5k6ON7Euo8LlFGjAJhDkMDfzkUY86YkEAPZcerhvxqI4DdfAEhv36g+R2njdbJWjh1t5cHlG2IzJ9Hs4E5EhoY3iSOJoxTK44Q2wlTfFgtVzPv1QnGGDIWfKArlgPQWk0QsYgVoXzTRzAHhbkY/FsfagRG3wmq5BUb5bzR5bgzE/HI3wdqBfg6FwshHD+Y7S9YIPpEIzUCaP61bde2khSUSyWwt/lDMdvHKzwsf+gXyUWfNEivkdZhbZi+q0UlYdsQPEor+o7X5Sp3SrSe6hkcNltroGC65c=
+      file: 
+        - "${IMAGE}.zip"
+      skip_cleanup: true
+      on:
+        tags: true
 
-# Linux 64bit, OF master
   - os: linux
     dist: trusty
     sudo: required
@@ -43,50 +37,32 @@ matrix:
     addons:
       apt:
         sources:
-          - ubuntu-toolchain-r-test
+        - ubuntu-toolchain-r-test
         packages:
-          - gcc-4.9
-          - g++-4.9
-          - gdb
-
-# OSX, OF master
+        - gcc-4.9
+        - g++-4.9
+        - gdb
   - os: osx
     osx_image: xcode8
     compiler: clang
     env: TARGET="osx" OF_BRANCH="master"
-
-# Linux ARM7, OF stable:  Not supported yet
-#  - os: linux
-#    sudo: false
-#    env: TARGET="linuxarmv7l" OF_BRANCH="stable"
-#    cache:
-#      directories:
-#        - ~/rpi2_toolchain
-#        - ~/firmware-master
-#        - ~/archlinux
-
-  # Exclude the default build that would otherwise be generated
-  # see https://github.com/travis-ci/travis-ci/issues/1228
   exclude:
-    - compiler: gcc
-
+  - compiler: gcc
 install:
-  - |
-    if ! [ "${TARGET}" == "rpi" ]; then
-      cd ~
-      git clone --depth=1 --branch=$OF_BRANCH https://github.com/openframeworks/openFrameworks
-      cd ~/openFrameworks/addons
-      git clone --depth=1 https://github.com/jeffcrouse/ofxJSON.git
-      cd ~/openFrameworks
-      scripts/ci/addons/install.sh
-    fi
-
+- |
+  if ! [ "${TARGET}" == "rpi" ]; then
+    cd ~
+    git clone --depth=1 --branch=$OF_BRANCH https://github.com/openframeworks/openFrameworks
+    cd ~/openFrameworks/addons
+    git clone --depth=1 https://github.com/jeffcrouse/ofxJSON.git
+    cd ~/openFrameworks
+    scripts/ci/addons/install.sh
+  fi
 script:
-  - |
-    if ! [ "${TARGET}" == "rpi" ]; then
-      cd ~/openFrameworks
-      scripts/ci/addons/build.sh
-    fi
-
+- |
+  if ! [ "${TARGET}" == "rpi" ]; then
+    cd ~/openFrameworks
+    scripts/ci/addons/build.sh
+  fi
 git:
-  depth: 1
\ No newline at end of file
+  depth: 1
diff --git a/scripts/ci/rpi/mkimage.sh b/scripts/ci/rpi/mkimage.sh
index c5b97e5..48f46e3 100644
--- a/scripts/ci/rpi/mkimage.sh
+++ b/scripts/ci/rpi/mkimage.sh
@@ -1,3 +1,78 @@
-#/bin/bash
+#!/bin/bash
 
-echo "Hello! The rpi mkimage.sh script."
+# Setup script error handling see https://disconnected.systems/blog/another-bash-strict-mode for details
+set -xuo pipefail
+trap 's=$?; echo "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR
+IFS=$'\n\t'
+
+# Ensure we are root
+if [[ $EUID -ne 0 ]]; then
+    echo "This script must be run as root" 1>&2
+    exit 1
+fi
+
+# Image creation constants.
+# See .travis.yml for environment variables.
+MOUNT="mnt"
+
+echo "Welcome to mkimage.sh script."
+
+# Unmount drives and general cleanup on exit, the trap ensures this will always
+# run execpt in the most extream cases.
+cleanup() {
+    [[ -f "${MOUNT}/tmp/${SCRIPT}" ]] && rm "${MOUNT}/tmp/${SCRIPT}"
+    if [[ -d "${MOUNT}" ]]; then
+        umount "${MOUNT}/dev/pts" || true
+        umount "${MOUNT}/dev" || true
+        umount "${MOUNT}/proc" || true
+        umount "${MOUNT}/sys" || true
+        umount "${MOUNT}/boot" || true
+        umount "${MOUNT}" || true
+        rmdir "${MOUNT}" || true
+    fi
+    [ -n "${loopdev:-}" ] && losetup --detach "${loopdev}" || true
+}
+trap cleanup EXIT
+
+# Download raspbian arm only if we have not already done so
+[ ! -f "${RPI_ZIP}" ] && wget "${RPI_URL}"
+
+# Unzip Raspbian
+# -u  update files, create if necessary
+unzip -u "${RPI_ZIP}"
+
+mv "$(ls *.img | head -n 1)" "${IMAGE}"
+
+# Configure loopback device to expand partition 2
+loopdev=$(losetup --find --show "${IMAGE}")
+echo "Created loopback device ${loopdev}"
+echo "Mounting filesystem."
+
+bootdev=$(ls "${loopdev}"*1)
+rootdev=$(ls "${loopdev}"*2)
+partprobe "${loopdev}"
+
+[ ! -d "${MOUNT}" ] && mkdir "${MOUNT}"
+mount "${rootdev}" "${MOUNT}"
+[ ! -d "${MOUNT}/boot" ] && mkdir "${MOUNT}/boot"
+mount "${bootdev}" "${MOUNT}/boot"
+
+# Copy our installation script and other artifacts.
+#install -Dm755 "${SCRIPT}" "${MOUNT}/tmp/${SCRIPT}"
+
+# Prep the chroot.
+mount --bind /proc "${MOUNT}/proc"
+mount --bind /sys "${MOUNT}/sys"
+mount --bind /dev "${MOUNT}/dev"
+mount --bind /dev/pts "${MOUNT}/dev/pts"
+
+cp /etc/resolv.conf "${MOUNT}/etc/resolv.conf"
+cp /usr/bin/qemu-arm-static "${MOUNT}/usr/bin"
+cp "${MOUNT}/etc/ld.so.preload" "${MOUNT}/etc/_ld.so.preload"
+echo "" > "${MOUNT}/etc/ld.so.preload"
+
+# Run the installation script as if we would be inside the Raspberry Pi.
+#chroot "${MOUNT}" "/tmp/${SCRIPT}"
+
+# Put back the old ld.so.preload script.
+mv "${MOUNT}/etc/_ld.so.preload" "${MOUNT}/etc/ld.so.preload"
diff --git a/scripts/ci/rpi/pisetup.sh b/scripts/ci/rpi/pisetup.sh
new file mode 100644
index 0000000..e69de29