+#!/bin/sh
+
+# sh-compatible version of Tridactyl native binary updater
+# Python 3 is required. If it is not present, installation will fail.
+
+# Installation:
+# curl -fsSl 'url to this script' | sh
+# or:
+# wget -qO - 'url to this script' | sh
+
+# Handle errors (bash only)
+echoerr() {
+ red="\033[31m"
+ normal="\e[0m"
+ echo -e "$red$@$normal" >&2
+}
+trap "echoerr 'Failed to install!'" ERR >/dev/null 2>&1 #can't trap ERR in sh
+
+# Check requirements for native messenger
+python_path=$(which python3) || python_path=""
+if [ ! -x "$python_path" ]; then
+ echoerr "Error: Python 3 must exist in PATH."
+ echoerr "Please install it and run this script again."
+ exit 1
+fi
+
+# Set up environment
+manifest_loc="https://raw.githubusercontent.com/cmcaine/tridactyl/master/native/tridactyl.json"
+native_loc="https://raw.githubusercontent.com/cmcaine/tridactyl/master/native/native_main.py"
+OSTYPE="${OSTYPE:-`uname -s | tr '[:upper:]' '[:lower:]'`}"
+XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
+data_home="${XDG_LOCAL_HOME:-$XDG_DATA_HOME/tridactyl}"
+native_file="$data_home/native_main.py.new"
+native_file_final="$data_home/native_main.py"
+# Decide where to put the manifest based on OS
+case "$OSTYPE" in
+ darwin*) manifest_home="$HOME/Library/Application Support/Mozilla/NativeMessagingHosts";;
+ *) manifest_home="$HOME/.mozilla/native-messaging-hosts";;
+esac
+manifest_file="$manifest_home/tridactyl.json"
+# Test for curl
+if ! command -v curl; then
+ # Based on busybox-compatible wget
+ dl="wget -qO"
+else
+ dl="curl -sSo"
+fi
+
+echo "Installing script here: $data_home"
+echo "Installing manifest here: $manifest_home"
+
+# Download files
+mkdir -p "$manifest_home" "$data_home"
+# Use local copy for testing and/or use in git repo
+if [ "$1" == "local" ]; then
+ cp -f native/tridactyl.json "$manifest_file"
+ cp -f native/native_main.py "$native_file"
+else
+ $dl "$manifest_file" "$manifest_loc"
+ $dl "$native_file" "$native_loc"
+fi
+
+# Fix file contents
+sedEscape() {
+ sed 's/[&/\]/\\&/g' <<_EOF_
+$@
+_EOF_
+}
+# Replace native executable path in manifest
+sed -i.bak "s/REPLACE_ME_WITH_SED/$(sedEscape "$native_file_final")/" "$manifest_file"
+chmod +x $native_file
+# Replace python shebang in native messenger script
+sed -i.bak "1s/.*/#!$(sedEscape /usr/bin/env) $(sedEscape "$python_path")/" "$native_file"
+mv "$native_file" "$native_file_final"
+
+echo
+echo "Successfully installed Tridactyl native messenger!"
+echo "Run ':native' in Firefox to check."