abducate (1181B)
1 #!/bin/sh 2 PROGNAME="$(basename $0)" 3 [ "$ABDUCATE_DIR" ] || ABDUCATE_DIR="$HOME/.abducate" 4 export ABDUCO_SOCKET_DIR="$ABDUCATE_DIR" 5 6 err() { 7 echo "$PROGNAME: $*" >&2 8 } 9 10 usage() { 11 err "usage: $PROGNAME [-n] [-l label] [start | stop | toggle | list | attach | status] command [args...]" 12 } 13 14 15 while getopts "nl:" opt; do 16 case ${opt} in 17 n) 18 NOTIFY=1 19 ;; 20 l) 21 LABEL="${OPTARG}" 22 ;; 23 *) 24 usage 25 exit 1;; 26 esac 27 done 28 29 shift $((OPTIND-1)) 30 31 [ "$1" ] && WHAT=$1 && shift; 32 [ "$1" ] && SERVE=$1 && shift; 33 [ ! "$LABEL" ] && LABEL=$SERVE; 34 35 read _ _ _ PID _ <<<$(abduco | sed -n '2,$p' | awk -v label=$LABEL '$1=="*"{$1=""} $NF==label && $1!="+"') 36 if [ "$WHAT" = "toggle" ]; then [ $PID ] && WHAT="stop" || WHAT="start"; fi 37 38 case $WHAT in 39 start) 40 if [ $PID ]; then err "already running" && exit 1; fi 41 42 abduco -frn $LABEL $SHELL -c "ABDUCO_SOCKET_DIR='' $SERVE $*" 43 [ $NOTIFY ] && notify-send "$PROGNAME: enabled $LABEL";; 44 stop) 45 if [ ! $PID ]; then err "not running" && exit 1; fi 46 kill $PID 47 [ $NOTIFY ] && notify-send "$PROGNAME: disabled $LABEL";; 48 list) 49 abduco;; 50 attach) 51 abduco -a $LABEL;; 52 status) 53 [ $PID ] && echo "enabled" || echo "disabled";; 54 *) 55 usage && exit 1;; 56 esac 57 58 exit 0;