From a495ffb13627fb1c6256f58f75a4dc1c7ebf7da2 Mon Sep 17 00:00:00 2001
From: Justin-p <justin-p@users.noreply.github.com>
Date: Wed, 2 Oct 2019 00:29:14 +0200
Subject: [PATCH 1/4] Add update script

---
 README.md                    | 14 +++++++
 scripts/update_pwnagotchi.sh | 77 ++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)
 create mode 100644 scripts/update_pwnagotchi.sh

diff --git a/README.md b/README.md
index a24bb05..35f79ba 100644
--- a/README.md
+++ b/README.md
@@ -80,6 +80,20 @@ usage: ./scripts/create_sibling.sh [OPTIONS]
 
 If you connect to the unit via `usb0` (thus using the data port), you might want to use the `scripts/linux_connection_share.sh` script to bring the interface up on your end and share internet connectivity from another interface, so you can update the unit and generally download things from the internet on it.
 
+#### Update your pwnagotchi
+
+You can use the `scripts/update_pwnagotchi.sh` script to update to the most recent version of pwnagotchi.
+
+```shell
+Usage: ./update_pwnagitchi.sh [-m | --master] [-c | --commit | --branch] [-bc | --backupconfig] [-rc | --restoreconfig] [-h | --help]
+
+   -m, --master               Update to the master branch. Used by default.
+   -c, --commit, --branch     Update to the specific commit/branch.
+   -bc, --backupconfig        Backup the current pwnagotchi config.
+   -rc, --restoreconfig       Restore the current pwnagotchi config. -bc will be enabled.
+   -h, --help                 Shows this help.
+```
+
 ### UI
 
 The UI is available either via display if installed, or via http://pwnagotchi.local:8080/ if you connect to the unit via `usb0` and set a static address on the network interface (change `pwnagotchi` with the hostname of your unit).
diff --git a/scripts/update_pwnagotchi.sh b/scripts/update_pwnagotchi.sh
new file mode 100644
index 0000000..c5cb757
--- /dev/null
+++ b/scripts/update_pwnagotchi.sh
@@ -0,0 +1,77 @@
+#!/bin/bash
+# Default variables
+folder="/tmp/pwnagotchi"
+version="master"
+commit=0
+backupconfig=0
+restoreconfig=0
+
+# functions
+function display_help {
+        echo "Usage: $0 [-m | --master] [-c | --commit | --branch] [-bc | --backupconfig] [-rc | --restoreconfig] [-h | --help]" >&2
+        echo
+        echo "   -m, --master               Update to the master branch. Used by default."
+        echo "   -c, --commit, --branch     Update to the specific commit/branch."
+        echo "   -bc, --backupconfig        Backup the current pwnagotchi config."
+        echo "   -rc, --restoreconfig       Restore the current pwnagotchi config. -bc will be enabled."
+        echo "   -h, --help                 Shows this help."
+        echo ""
+        echo
+        exit 1
+}
+function test_root {
+# Check if we are running as root.
+    if ! [ $(id -u) = 0 ]; then
+            echo " [!] This script must be run as root."
+            exit 3
+    fi
+}
+function test_github {
+    wget -q  --spider https://github.com/evilsocket/pwnagotchi/
+    if [ $? -ne 0 ]; then
+            echo " [!] Cannot reach github. This script requires internet access, ensure connection sharing is working."
+            exit 4
+    fi
+}
+
+# Commandline arguments
+while [[ "$#" -gt 0 ]]; do case $1 in
+        -m|--master) version="master"; shift;;
+        -c|--commit|---branch) commit="$2" version="other"; shift;;
+        -bc|--backupconfig) backupconfig=1; shift;;
+        -rc|--restoreconfig) backupconfig=1 restoreconfig=1; shift;;
+        -h|--help) display_help;;
+        *) echo "Unknown parameter passed: $1"; exit 2;;
+esac; shift; done
+
+echo " [+] Checking prerequisites."
+test_root
+test_github
+
+# clean up old files, clone master, set checkout to commit if needed.
+echo " [+] Cloning to $folder..."
+rm $folder -rf
+git clone https://github.com/evilsocket/pwnagotchi $folder -q
+cd $folder
+if [ $version == "other" ]; then
+        git checkout $commit -q
+fi
+echo " [+] Installing $(git log -1 --format="%h")"
+
+echo " [+] Updating..."
+if [ $backupconfig -eq 1 ]; then
+        echo " [+] Creating backup of config.yml"
+        mv /root/pwnagotchi/config.yml ~/config.yml.bak -f
+fi
+rm /root/pwnagotchi -rf
+rsync -aPq $folder/sdcard/rootfs/* /
+cd /tmp
+rm $folder -rf
+if [ $restoreconfig -eq 1 ]; then
+        echo " [+] Restoring backup of config.yml"
+        mv ~/config.yml.bak /root/pwnagotchi/config.yml -f
+fi
+
+echo " [+] Restarting pwnagotchi in auto mode. $( screen -X -S pwnagotchi quit)"
+sudo -H -u root /usr/bin/screen -dmS pwnagotchi -c /root/pwnagotchi/data/screenrc.auto
+echo " [+] Finished"
\ No newline at end of file

From cd5be9c4cdd6a7bd0f236707a6b5b20352fd98cc Mon Sep 17 00:00:00 2001
From: Justin-p <justin-p@users.noreply.github.com>
Date: Wed, 2 Oct 2019 10:14:26 +0200
Subject: [PATCH 2/4] Add /boot. Redo help. Replaced --commit/--branch with
 --version. Added --url to easly switch to other fork for testing

---
 scripts/update_pwnagotchi.sh | 93 +++++++++++++++++++-----------------
 1 file changed, 49 insertions(+), 44 deletions(-)

diff --git a/scripts/update_pwnagotchi.sh b/scripts/update_pwnagotchi.sh
index c5cb757..6010721 100644
--- a/scripts/update_pwnagotchi.sh
+++ b/scripts/update_pwnagotchi.sh
@@ -1,77 +1,82 @@
 #!/bin/bash
 # Default variables
-folder="/tmp/pwnagotchi"
+git_folder="/tmp/pwnagotchi"
+git_url="https://github.com/evilsocket/pwnagotchi/"
 version="master"
-commit=0
 backupconfig=0
 restoreconfig=0
 
-# functions
-function display_help {
-        echo "Usage: $0 [-m | --master] [-c | --commit | --branch] [-bc | --backupconfig] [-rc | --restoreconfig] [-h | --help]" >&2
-        echo
-        echo "   -m, --master               Update to the master branch. Used by default."
-        echo "   -c, --commit, --branch     Update to the specific commit/branch."
-        echo "   -bc, --backupconfig        Backup the current pwnagotchi config."
-        echo "   -rc, --restoreconfig       Restore the current pwnagotchi config. -bc will be enabled."
-        echo "   -h, --help                 Shows this help."
-        echo ""
-        echo
-        exit 1
+# Functions
+function usage() {
+        cat <<EOF
+
+ usage: $0 [OPTIONS]
+
+   Options:
+      -v, --version              # Version to update to, can be a branch or commit. (default: master)
+      -u, --url                  # Url to clone from. (default: https://github.com/evilsocket/pwnagotchi)
+      -bc, --backupconfig        # Backup the current pwnagotchi config.
+      -rc, --restoreconfig       # Restore the current pwnagotchi config. -bc will be enabled.
+      -h, --help                 # Shows this help.
+
+EOF
+        exit 0
 }
-function test_root {
-# Check if we are running as root.
+
+function test_root() {
     if ! [ $(id -u) = 0 ]; then
-            echo " [!] This script must be run as root."
-            exit 3
+            echo "[!] This script must be run as root."
+            exit 1
     fi
 }
-function test_github {
-    wget -q  --spider https://github.com/evilsocket/pwnagotchi/
+
+function test_github() {
+    wget -q  --spider $git_url
     if [ $? -ne 0 ]; then
-            echo " [!] Cannot reach github. This script requires internet access, ensure connection sharing is working."
-            exit 4
+            echo "[!] Cannot reach github. This script requires internet access, ensure connection sharing is working."
+            exit 2
     fi
 }
 
 # Commandline arguments
 while [[ "$#" -gt 0 ]]; do case $1 in
-        -m|--master) version="master"; shift;;
-        -c|--commit|---branch) commit="$2" version="other"; shift;;
+        -v|--version) version="$2"; shift;;
+        -u|--url) git_url="$2"; shift;;
         -bc|--backupconfig) backupconfig=1; shift;;
         -rc|--restoreconfig) backupconfig=1 restoreconfig=1; shift;;
-        -h|--help) display_help;;
-        *) echo "Unknown parameter passed: $1"; exit 2;;
+        -h|--help) usage;;
+        *) echo "Unknown parameter passed: $1"; exit 3;;
 esac; shift; done
 
-echo " [+] Checking prerequisites."
+echo "[+] Checking prerequisites."
 test_root
 test_github
 
 # clean up old files, clone master, set checkout to commit if needed.
-echo " [+] Cloning to $folder..."
-rm $folder -rf
-git clone https://github.com/evilsocket/pwnagotchi $folder -q
-cd $folder
-if [ $version == "other" ]; then
-        git checkout $commit -q
+echo "[+] Cloning to $git_folder..."
+rm $git_folder -rf
+git clone $git_url $git_folder -q
+cd $git_folder
+if [ $version != "master" ]; then
+        git checkout $version -q
 fi
-echo " [+] Installing $(git log -1 --format="%h")"
+echo "[+] Installing $(git log -1 --format="%h")"
 
-echo " [+] Updating..."
+echo "[+] Updating..."
 if [ $backupconfig -eq 1 ]; then
-        echo " [+] Creating backup of config.yml"
-        mv /root/pwnagotchi/config.yml ~/config.yml.bak -f
+        echo "[+] Creating backup of config.yml"
+        mv /root/pwnagotchi/config.yml /root/config.yml.bak -f
 fi
-rm /root/pwnagotchi -rf
-rsync -aPq $folder/sdcard/rootfs/* /
+rm /root/pwnagotchi -rf # ensures old files are removed
+rsync -aPq $git_folder/sdcard/boot/*   /boot/
+rsync -aPq $git_folder/sdcard/rootfs/* /
 cd /tmp
-rm $folder -rf
+rm $git_folder -rf
 if [ $restoreconfig -eq 1 ]; then
-        echo " [+] Restoring backup of config.yml"
-        mv ~/config.yml.bak /root/pwnagotchi/config.yml -f
+        echo "[+] Restoring backup of config.yml"
+        mv /root/config.yml.bak /root/pwnagotchi/config.yml -f
 fi
 
-echo " [+] Restarting pwnagotchi in auto mode. $( screen -X -S pwnagotchi quit)"
+echo "[+] Restarting pwnagotchi in auto mode. $( screen -X -S pwnagotchi quit)"
 sudo -H -u root /usr/bin/screen -dmS pwnagotchi -c /root/pwnagotchi/data/screenrc.auto
-echo " [+] Finished"
\ No newline at end of file
+echo "[+] Finished"
\ No newline at end of file

From bc2bca79b5139d4d65d3fd7e824ff2793e12cdda Mon Sep 17 00:00:00 2001
From: Justin-p <justin-p@users.noreply.github.com>
Date: Wed, 2 Oct 2019 10:40:16 +0200
Subject: [PATCH 3/4] updated arguments to getopts. Added -m for restart mode.
 Moved prerequisites checks.

---
 scripts/update_pwnagotchi.sh | 119 ++++++++++++++++++++++-------------
 1 file changed, 74 insertions(+), 45 deletions(-)

diff --git a/scripts/update_pwnagotchi.sh b/scripts/update_pwnagotchi.sh
index 6010721..34e4eed 100644
--- a/scripts/update_pwnagotchi.sh
+++ b/scripts/update_pwnagotchi.sh
@@ -1,82 +1,111 @@
 #!/bin/bash
 # Default variables
-git_folder="/tmp/pwnagotchi"
-git_url="https://github.com/evilsocket/pwnagotchi/"
-version="master"
-backupconfig=0
-restoreconfig=0
+GIT_FOLDER="/tmp/pwnagotchi"
+GIT_URL="https://github.com/evilsocket/pwnagotchi/"
+VERSION="master"
+SUPPORTED_RESTART_MODES=( 'auto' 'manual' )
+MODE="auto"
+BACKUPCONFIG=0
+RESTORECONFIG=0
 
 # Functions
 function usage() {
-        cat <<EOF
+    cat <<EOF
 
  usage: $0 [OPTIONS]
-
+ 
    Options:
-      -v, --version              # Version to update to, can be a branch or commit. (default: master)
-      -u, --url                  # Url to clone from. (default: https://github.com/evilsocket/pwnagotchi)
-      -bc, --backupconfig        # Backup the current pwnagotchi config.
-      -rc, --restoreconfig       # Restore the current pwnagotchi config. -bc will be enabled.
-      -h, --help                 # Shows this help.
+      -v        # Version to update to, can be a branch or commit. (default: master)
+      -u        # Url to clone from. (default: https://github.com/evilsocket/pwnagotchi)
+      -m        # Mode to restart to. (Supported: ${SUPPORTED_RESTART_MODES[*]}; default: auto)
+      -b        # Backup the current pwnagotchi config.
+      -r        # Restore the current pwnagotchi config. (-b will be enabled.)
+      -h        # Shows this help.
 
 EOF
-        exit 0
+    exit 0
 }
 
 function test_root() {
     if ! [ $(id -u) = 0 ]; then
-            echo "[!] This script must be run as root."
-            exit 1
+        echo "[!] This script must be run as root."
+        exit 1
     fi
 }
 
 function test_github() {
-    wget -q  --spider $git_url
+    wget -q  --spider $GIT_URL
     if [ $? -ne 0 ]; then
-            echo "[!] Cannot reach github. This script requires internet access, ensure connection sharing is working."
-            exit 2
+        echo "[!] Cannot reach github. This script requires internet access, ensure connection sharing is working."
+        exit 2
     fi
 }
 
-# Commandline arguments
-while [[ "$#" -gt 0 ]]; do case $1 in
-        -v|--version) version="$2"; shift;;
-        -u|--url) git_url="$2"; shift;;
-        -bc|--backupconfig) backupconfig=1; shift;;
-        -rc|--restoreconfig) backupconfig=1 restoreconfig=1; shift;;
-        -h|--help) usage;;
-        *) echo "Unknown parameter passed: $1"; exit 3;;
-esac; shift; done
-
 echo "[+] Checking prerequisites."
 test_root
 test_github
 
+while getopts ":v:u:m:b:r:h" o; do
+  case "${o}" in
+    v)
+      VERSION="${OPTARG}"
+      ;;
+    u)
+      GIT_URL="${OPTARG}"
+      ;;
+    m)
+      if [[ "${SUPPORTED_RESTART_MODES[*]}" =~ ${OPTARG} ]]; then
+        MODE="${OPTARG}"
+      else
+        usage
+      fi      
+      ;;      
+    b)
+      BACKUPCONFIG=1
+      ;;
+    r)
+      BACKUPCONFIG=1    
+      RESTORECONFIG=1
+      ;;      
+    h)
+      usage
+      ;;
+    *)
+      usage
+      ;;
+  esac
+done
+shift $((OPTIND-1))
+
 # clean up old files, clone master, set checkout to commit if needed.
-echo "[+] Cloning to $git_folder..."
-rm $git_folder -rf
-git clone $git_url $git_folder -q
-cd $git_folder
-if [ $version != "master" ]; then
-        git checkout $version -q
+echo "[+] Cloning to $GIT_FOLDER..."
+rm $GIT_FOLDER -rf
+git clone $GIT_URL $GIT_FOLDER -q
+cd $GIT_FOLDER
+if [ $VERSION != "master" ]; then
+    git checkout $VERSION -q
 fi
 echo "[+] Installing $(git log -1 --format="%h")"
 
 echo "[+] Updating..."
-if [ $backupconfig -eq 1 ]; then
-        echo "[+] Creating backup of config.yml"
-        mv /root/pwnagotchi/config.yml /root/config.yml.bak -f
+if [ $BACKUPCONFIG -eq 1 ]; then
+    echo "[+] Creating backup of config.yml"
+    mv /root/pwnagotchi/config.yml /root/config.yml.bak -f
 fi
 rm /root/pwnagotchi -rf # ensures old files are removed
-rsync -aPq $git_folder/sdcard/boot/*   /boot/
-rsync -aPq $git_folder/sdcard/rootfs/* /
+rsync -aPq $GIT_FOLDER/sdcard/boot/*   /boot/
+rsync -aPq $GIT_FOLDER/sdcard/rootfs/* /
 cd /tmp
-rm $git_folder -rf
-if [ $restoreconfig -eq 1 ]; then
-        echo "[+] Restoring backup of config.yml"
-        mv /root/config.yml.bak /root/pwnagotchi/config.yml -f
+rm $GIT_FOLDER -rf
+if [ $RESTORECONFIG -eq 1 ]; then
+    echo "[+] Restoring backup of config.yml"
+    mv /root/config.yml.bak /root/pwnagotchi/config.yml -f
 fi
 
-echo "[+] Restarting pwnagotchi in auto mode. $( screen -X -S pwnagotchi quit)"
-sudo -H -u root /usr/bin/screen -dmS pwnagotchi -c /root/pwnagotchi/data/screenrc.auto
+echo "[+] Restarting pwnagotchi in $MODE mode. $( screen -X -S pwnagotchi quit)"
+if [ $MODE == "auto" ]; then
+    sudo -H -u root /usr/bin/screen -dmS pwnagotchi -c /root/pwnagotchi/data/screenrc.auto
+elif [ $MODE == "manual" ]; then
+    sudo -H -u root /usr/bin/screen -dmS pwnagotchi -c /root/pwnagotchi/data/screenrc.manual
+fi
 echo "[+] Finished"
\ No newline at end of file

From c784d1b6bebe38be73ff37c55fb1f522e38ce7dc Mon Sep 17 00:00:00 2001
From: Justin-p <justin-p@users.noreply.github.com>
Date: Wed, 2 Oct 2019 10:41:42 +0200
Subject: [PATCH 4/4] Updated README

---
 README.md | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/README.md b/README.md
index 35f79ba..24f0cec 100644
--- a/README.md
+++ b/README.md
@@ -85,13 +85,16 @@ If you connect to the unit via `usb0` (thus using the data port), you might want
 You can use the `scripts/update_pwnagotchi.sh` script to update to the most recent version of pwnagotchi.
 
 ```shell
-Usage: ./update_pwnagitchi.sh [-m | --master] [-c | --commit | --branch] [-bc | --backupconfig] [-rc | --restoreconfig] [-h | --help]
+usage: ./update_pwnagitchi.sh [OPTIONS]
+
+   Options:
+      -v                # Version to update to, can be a branch or commit. (default: master)
+      -u                # Url to clone from. (default: https://github.com/evilsocket/pwnagotchi)
+      -m                # Mode to restart to. (Supported: auto manual; default: auto)
+      -b                # Backup the current pwnagotchi config.
+      -r                # Restore the current pwnagotchi config. -b will be enabled.
+      -h                # Shows this help.             Shows this help.
 
-   -m, --master               Update to the master branch. Used by default.
-   -c, --commit, --branch     Update to the specific commit/branch.
-   -bc, --backupconfig        Backup the current pwnagotchi config.
-   -rc, --restoreconfig       Restore the current pwnagotchi config. -bc will be enabled.
-   -h, --help                 Shows this help.
 ```
 
 ### UI