]> git.sev.monster Git - dotfiles.git/blob - bin/update-tridactyl-native.sh
fix gnu find depth warning, update comments
[dotfiles.git] / bin / update-tridactyl-native.sh
1 #!/bin/sh
2
3 # sh-compatible version of Tridactyl native binary updater
4 # Python 3 is required. If it is not present, installation will fail.
5
6 # Installation:
7 #   curl -fsSl 'url to this script' | sh
8 # or:
9 #   wget -qO - 'url to this script' | sh
10
11 # Handle errors (bash only)
12 echoerr() {
13     red="\033[31m"
14     normal="\e[0m"
15     echo -e "$red$@$normal" >&2
16 }
17 trap "echoerr 'Failed to install!'" ERR >/dev/null 2>&1 #can't trap ERR in sh
18
19 # Check requirements for native messenger
20 python_path=$(which python3) || python_path=""
21 if [ ! -x "$python_path" ]; then
22     echoerr "Error: Python 3 must exist in PATH."
23     echoerr "Please install it and run this script again."
24     exit 1
25 fi
26
27 # Set up environment
28 manifest_loc="https://raw.githubusercontent.com/cmcaine/tridactyl/master/native/tridactyl.json"
29 native_loc="https://raw.githubusercontent.com/cmcaine/tridactyl/master/native/native_main.py"
30 OSTYPE="${OSTYPE:-`uname -s | tr '[:upper:]' '[:lower:]'`}"
31 XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
32 data_home="${XDG_LOCAL_HOME:-$XDG_DATA_HOME/tridactyl}"
33 native_file="$data_home/native_main.py.new"
34 native_file_final="$data_home/native_main.py"
35 # Decide where to put the manifest based on OS
36 case "$OSTYPE" in
37     darwin*) manifest_home="$HOME/Library/Application Support/Mozilla/NativeMessagingHosts";;
38     *)       manifest_home="$HOME/.mozilla/native-messaging-hosts";;
39 esac
40 manifest_file="$manifest_home/tridactyl.json"
41 # Test for curl
42 if ! command -v curl; then
43     # Based on busybox-compatible wget
44     dl="wget -qO"
45 else
46     dl="curl -sSo"
47 fi
48
49 echo "Installing script here: $data_home"
50 echo "Installing manifest here: $manifest_home"
51
52 # Download files
53 mkdir -p "$manifest_home" "$data_home"
54 # Use local copy for testing and/or use in git repo
55 if [ "$1" == "local" ]; then
56     cp -f native/tridactyl.json "$manifest_file"
57     cp -f native/native_main.py "$native_file"
58 else
59     $dl "$manifest_file" "$manifest_loc"
60     $dl "$native_file" "$native_loc"
61 fi
62
63 # Fix file contents
64 sedEscape() {
65     sed 's/[&/\]/\\&/g' <<_EOF_
66 $@
67 _EOF_
68 }
69 # Replace native executable path in manifest
70 sed -i.bak "s/REPLACE_ME_WITH_SED/$(sedEscape "$native_file_final")/" "$manifest_file"
71 chmod +x $native_file
72 # Replace python shebang in native messenger script
73 sed -i.bak "1s/.*/#!$(sedEscape /usr/bin/env) $(sedEscape "$python_path")/" "$native_file"
74 mv "$native_file" "$native_file_final"
75
76 echo
77 echo "Successfully installed Tridactyl native messenger!"
78 echo "Run ':native' in Firefox to check."
This page took 0.036345 seconds and 4 git commands to generate.