From 7a721be7dc5ec9ce07de057f5ef0714a4a65b750 Mon Sep 17 00:00:00 2001
From: Cassiano Aquino <cassianoaquino@me.com>
Date: Mon, 21 Oct 2019 16:38:53 +0100
Subject: [PATCH 1/3] papirus, fbi and idempotency changes

---
 builder/pwnagotchi.yml | 81 ++++++++++++++++++++++++------------------
 1 file changed, 47 insertions(+), 34 deletions(-)

diff --git a/builder/pwnagotchi.yml b/builder/pwnagotchi.yml
index b0f5e6a..6356d18 100644
--- a/builder/pwnagotchi.yml
+++ b/builder/pwnagotchi.yml
@@ -9,10 +9,12 @@
     system:
       boot_options:
         - "dtoverlay=dwc2"
-        - "dtparam=spi=on"
         - "dtoverlay=spi1-3cs"
-        - "dtoverlay=i2c_arm=on"
-        - "dtoverlay=i2c1=on"
+        - "dtparam=spi=on"
+        - "dtparam=i2c_arm=on"
+        - "dtparam=i2c1=on"
+      modules:
+        - "i2c-dev"
     services:
       enable:
         - dphys-swapfile.service
@@ -95,27 +97,22 @@
           - libfuse-dev
           - bc
           - fonts-freefont-ttf
+          - fbi
 
   tasks:
-
-  - name: selected hostname
-    debug:
-      msg: "{{ pwnagotchi.hostname }}"
-
-  - name: build version
-    debug:
-      msg: "{{ pwnagotchi.version }}"
-
   - name: change hostname
     hostname:
       name: "{{pwnagotchi.hostname}}"
+    when: lookup('file', '/etc/hostname') == "raspberrypi"
+    register: hostname
 
   - name: add hostname to /etc/hosts
     lineinfile:
       dest: /etc/hosts
-      regexp: '^127\.0\.0\.1[ \t]+localhost'
-      line: '127.0.0.1 localhost {{pwnagotchi.hostname}} {{pwnagotchi.hostname}}.local'
+      regexp: '^127\.0\.1\.1[ \t]+raspberrypi'
+      line: '127.0.1.1\t{{pwnagotchi.hostname}}'
       state: present
+    when: hostname.changed
 
   - name: Add re4son-kernel repo key
     apt_key:
@@ -161,6 +158,7 @@
     git:
       repo: https://github.com/repaper/gratis.git
       dest: /usr/local/src/gratis
+    register: gratisgit
 
   - name: build papirus service
     make:
@@ -169,6 +167,7 @@
       params:
         EPD_IO: epd_io.h
         PANEL_VERSION: 'V231_G2'
+    when: gratisgit.changed
 
   - name: install papirus service
     make:
@@ -177,6 +176,7 @@
       params:
         EPD_IO: epd_io.h
         PANEL_VERSION: 'V231_G2'
+    when: gratisgit.changed
 
   - name: configure papirus display size
     lineinfile:
@@ -184,6 +184,16 @@
       regexp: "#EPD_SIZE=2.0"
       line: "EPD_SIZE=2.0"
 
+  - name: collect python pip package list
+    command: "pip3 list"
+    register: pip_output
+
+  - name: set python pip package facts
+    set_fact:
+      pip_packages: >
+        {{ pip_packages | default({}) | combine( { item.split()[0]: item.split()[1] } ) }}
+    with_items: "{{ pip_output.stdout_lines }}"
+
   - name: acquire python3 pip target
     command: "python3 -c 'import sys;print(sys.path.pop())'"
     register: pip_target
@@ -192,26 +202,39 @@
     git:
       repo: https://github.com/evilsocket/pwnagotchi.git
       dest: /usr/local/src/pwnagotchi
+    register: pwnagotchigit
+
+  - name: fetch pwnagotchi version
+    set_fact:
+      pwnagotchi_version: "{{ lookup('file', '/usr/local/src/pwnagotchi/pwnagotchi/__init__.py') | replace('\n', ' ') | regex_replace('.*version.*=.*''([0-9]+\\.[0-9]+\\.[0-9]+[A-Za-z0-9]*)''.*', '\\1') }}"
+
+  - name: pwnagotchi version found
+    debug:
+      msg: "{{ pwnagotchi_version }}"
 
   - name: build pwnagotchi wheel
     command: "python3 setup.py sdist bdist_wheel"
     args:
       chdir: /usr/local/src/pwnagotchi
+    when: (pwnagotchigit.changed) or (pip_packages['pwnagotchi'] is undefined) or (pip_packages['pwnagotchi'] != pwnagotchi.version)
 
   - name: install opencv-python
     pip:
       name: "https://www.piwheels.hostedpi.com/simple/opencv-python/opencv_python-3.4.3.18-cp37-cp37m-linux_armv6l.whl"
       extra_args: "--no-deps --no-cache-dir --platform=linux_armv6l --only-binary=:all: --target={{ pip_target.stdout }}"
+    when: (pip_packages['opencv-python'] is undefined) or (pip_packages['opencv-python'] != '3.4.3.18')
 
   - name: install tensorflow
     pip:
       name: "https://www.piwheels.hostedpi.com/simple/tensorflow/tensorflow-1.13.1-cp37-none-linux_armv6l.whl"
       extra_args: "--no-deps --no-cache-dir --platform=linux_armv6l --only-binary=:all: --target={{ pip_target.stdout }}"
+    when: (pip_packages['tensorflow'] is undefined) or (pip_packages['tensorflow'] != '1.13.1')
 
   - name: install pwnagotchi wheel and dependencies
     pip:
       name: "{{ lookup('fileglob', '/usr/local/src/pwnagotchi/dist/pwnagotchi*.whl') }}"
       extra_args: "--no-cache-dir"
+    when: (pwnagotchigit.changed) or (pip_packages['pwnagotchi'] is undefined) or (pip_packages['pwnagotchi'] != pwnagotchi.version)
 
   - name: download and install pwngrid
     unarchive:
@@ -234,11 +257,13 @@
     git:
       repo: https://github.com/bettercap/caplets.git
       dest: /tmp/caplets
+    register: capletsgit
 
   - name: install bettercap caplets
     make:
       chdir: /tmp/caplets
       target: install
+    when: capletsgit.changed
 
   - name: download and install bettercap ui
     unarchive:
@@ -247,26 +272,6 @@
       remote_src: yes
       mode: 0755
 
-  - name: create cpuusage script
-    copy:
-      dest: /usr/bin/cpuusage
-      mode: 0755
-      content: |
-        #!/usr/bin/env bash
-        while true
-        do
-          top -b -n1 | awk '/Cpu\(s\)/ { printf("%d %", $2 + $4 + 0.5) }'
-          sleep 3
-        done
-
-  - name: create memusage script
-    copy:
-      dest: /usr/bin/memusage
-      mode: 0755
-      content: |
-        #!/usr/bin/env bash
-        free -m | awk '/Mem/ { printf( "%d %", $3 / $2 * 100 + 0.5 ) }'
-
   - name: create bootblink script
     copy:
       dest: /usr/bin/bootblink
@@ -434,6 +439,13 @@
       line: '{{ item }}'
     with_items: "{{system.boot_options}}"
 
+  - name: adjust /etc/modules
+    lineinfile:
+      dest: /etc/modules
+      insertafter: EOF
+      line: '{{ item }}'
+    with_items: "{{system.modules}}"
+
   - name: change root partition
     replace:
       dest: /boot/cmdline.txt
@@ -480,6 +492,7 @@
         touch /root/.pwnagotchi-auto && systemctl restart pwnagotchi
 
         You learn more about me at https://pwnagotchi.ai/
+    when: hostname.changed
 
   - name: clean apt cache
     apt:

From 84fa293a11d70d0d6438ffc531f458298e2faabb Mon Sep 17 00:00:00 2001
From: Cassiano Aquino <cassianoaquino@me.com>
Date: Mon, 21 Oct 2019 17:58:00 +0100
Subject: [PATCH 2/3] change quotes to allow tab expansion

---
 builder/pwnagotchi.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builder/pwnagotchi.yml b/builder/pwnagotchi.yml
index 6356d18..e60e345 100644
--- a/builder/pwnagotchi.yml
+++ b/builder/pwnagotchi.yml
@@ -110,7 +110,7 @@
     lineinfile:
       dest: /etc/hosts
       regexp: '^127\.0\.1\.1[ \t]+raspberrypi'
-      line: '127.0.1.1\t{{pwnagotchi.hostname}}'
+      line: "127.0.1.1\t{{pwnagotchi.hostname}}"
       state: present
     when: hostname.changed
 

From ace1244d1028aa50448088fd6fbc9868cbb8f760 Mon Sep 17 00:00:00 2001
From: Cassiano Aquino <cassianoaquino@me.com>
Date: Mon, 21 Oct 2019 18:41:36 +0100
Subject: [PATCH 3/3] disable sap plugin for bluetooth service

---
 builder/pwnagotchi.yml | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/builder/pwnagotchi.yml b/builder/pwnagotchi.yml
index e60e345..4d3ab94 100644
--- a/builder/pwnagotchi.yml
+++ b/builder/pwnagotchi.yml
@@ -114,6 +114,13 @@
       state: present
     when: hostname.changed
 
+  - name: disable sap plugin for bluetooth.service
+    lineinfile:
+      dest: /lib/systemd/system/bluetooth.service
+      regexp: '^ExecStart=/usr/lib/bluetooth/bluetoothd$'
+      line: 'ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=sap'
+      state: present
+
   - name: Add re4son-kernel repo key
     apt_key:
       url: https://re4son-kernel.com/keys/http/archive-key.asc