#!/bin/sh set -eu usage() { cat <<-'EOF' Usage: msg [-X|-s|-S|-m|-D|-b] [MESSAGE] msg -h EOF } help() { cat <<-'EOF' Options: -X send MESSAGE using the `xmpp` command -s play $XDG_DATA_HOME/msg/medium.ogg sound -S say MESSAGE using `speak` -m send email with MESSAGE as subject and empty body -D send desktop MESSAGE via `notify-send` -b print terminal bell -h, --help show this message MESSAGE the text to be sent by the relevant channel Examples: Ring a terminal bell and play a sound: $ msg -sb Send an email and an XMPP message: $ msg -mX 'The message goes here' EOF } for flag in "$@"; do case "$flag" in --) break ;; --help) usage help exit ;; *) ;; esac done sound() { play "$XDG_DATA_HOME"/msg/medium.ogg 2>/dev/null } XMPP=false SOUND=false SPEAK=false MAIL=false DESKTOP=false BELL=false ACTION_DONE=false SHOW_USAGE=false while getopts 'XsSmDbh' flag; do case "$flag" in X) XMPP=true ACTION_DONE=true ;; s) SOUND=true ACTION_DONE=true ;; S) SPEAK=true ACTION_DONE=true ;; m) MAIL=true ACTION_DONE=true ;; D) DESKTOP=true ACTION_DONE=true ;; b) BELL=true ACTION_DONE=true ;; h) usage help exit ;; *) usage >&2 exit 2 ;; esac done shift $((OPTIND - 1)) if [ "$ACTION_DONE" = false ]; then sound usage help exit fi MESSAGE="${1:-}" assert_arg() { if [ -z "$1" ]; then printf 'Missing %s.\n\n' "$2" >&2 usage >&2 exit 2 fi } if [ "$XMPP" = true ]; then assert_arg "$MESSAGE" '-X MESSAGE' xmpp -m "$MESSAGE" eu@euandreh.xyz & fi if [ "$SOUND" = true ]; then sound & fi if [ "$SPEAK" = true ]; then assert_arg "$MESSAGE" '-S MESSAGE' echo "$MESSAGE" | speak -v pt-BR & fi if [ "$MAIL" = true ]; then assert_arg "$MESSAGE" '-m MESSAGE' echo " " | email -s "$MESSAGE" eu@euandre.org & fi if [ "$DESKTOP" = true ]; then assert_arg "$MESSAGE" '-D MESSAGE' notify-send "$MESSAGE" & fi if [ "$BELL" = true ]; then printf '\a' & fi wait