#!/bin/bash set -e prefs="${HOME}/Library/Preferences" espanso_tap="federico-terzi/espanso" espanso_packages=( "mac-symbols" "all-emojis" ) uninstall=1 remove=1 copy=1 link=0 _usage() { printf "\n" >&2 printf "Usage: setup [OPTIONS]\n" >&2 printf "\n" >&2 printf "Installs or Uninstalls espanso & configuration files.\n" >&2 printf "\n" >&2 printf "Options:\n" >&2 printf "\t-c | --copy : Copy the configuration files, instead of creating symlinks.\n" >&2 printf "\t-h | --help : Print usage information.\n" >&2 printf "\t-l | --link : Create symlinks to the configuration files (Default).\n" >&2 printf "\t-r | --remove : Removes the entire ~/Library/Preferences/espanso directory.\n" >&2 printf "\t-u | --uninstall : Uninstalls configuration files and espanso.\n" >&2 printf "\n" >&2 printf "If called without the uninstall option then it will install the configuration files.\n" >&2 printf "The copy and link options are ignored if called with the uninstall or remove option.\n" >&2 printf "\n" >&2 } _parse_options() { arg= while ! test -z "$1"; do arg="$1" case $arg in -c | --copy) copy=0 link=1 shift;; -h | --help) _usage exit;; -l | --link) copy=1 link=0 shift;; -r | --remove) remove=0 shift;; -u | --uninstall) uninstall=0 shift;; *) echo "Unknown option $arg" >&2 shift;; esac done } _make_dirs() { if ! test -d "$prefs"; then mkdir "$prefs" fi if ! test -d "$prefs/espanso"; then mkdir "$prefs/espanso" fi } _command_exists() { command -v "$1" >/dev/null 2>&1 } _install_packages() { for arg in "${espanso_packages[@]}"; do echo "Installing espanso package: $arg" espanso install "$arg" done } _uninstall_packages() { for arg in "${espanso_packages[@]}"; do echo "Uninstalling espanso package: $arg" espanso uninstall "$arg" done } _remove_espanso_config() { echo "Removing espanso configuration..." if test -e "$prefs/espanso"; then rm -rf "$prefs/espanso" fi } _copy_espanso() { echo "Copying espanso configuration..." cp -r "$PWD/espanso" "$prefs" } _link_espanso() { echo "Linking espanso configuration..." # linking the espanso directory is not permitted, so we can only # link the configuration inside of the preferences directory. ln -sfv "$PWD/espanso/default.yml" "$prefs/espanso" ln -sfv "$PWD/espanso/user" "$prefs/espanso" } _install() { # check if espanso is installed already. if ! _command_exists espanso; then if ! _command_exists brew; then echo "Homebrew is not installed." exit 1 fi echo "Installing espanso with brew..." brew tap "$espanso_tap" brew install espanso fi # We have espanso, so install configuration files _make_dirs # must copy / link configuration before installing packages, # else the default configuration get's installed and causes this # script to fail if test $copy -eq 0 ; then _copy_espanso elif test $link -eq 0; then _link_espanso else # we don't know what to do. echo "Neither link nor copy was passed in, use --help for usage." exit 1 fi _install_packages } _uninstall() { # remove the configuration files _remove_espanso_config if _command_exists espanso; then # uninstall packages _uninstall_packages if _command_exists brew; then # uninstall espanso brew uninstall espanso fi fi echo "Uninstalled espanso..." } #-------------- main --------------- main() { _parse_options "$@" # check if we are in uninstall or remove mode. test "$remove" -eq 0 && _remove_espanso_config && exit "$?" test "$uninstall" -eq 0 && _uninstall && exit "$?" # else install espanso _install echo "" echo "Espanso installed, you will need to enable accessibility options in your settings" echo "Use 'espanso start' to start the espanso service." echo "" } main "$@"