#!/usr/local/bin/bash
install() {
uninstall
cat >"$launchd_file" << EOF
Label
$launchd_name
ProgramArguments
`which python3`
$zasd_root/src/zasd.py
KeepAlive
<$keep_alive/>
StandardOutPath
$log_root/zasd.log
StandardErrorPath
$log_root/zasd.err
EOF
load
}
uninstall() {
if is_installed; then
unload
fi
rm -f "$launchd_file"
}
load() {
launchctl load -w "$launchd_file"
}
unload() {
launchctl unload -w "$launchd_file"
}
start() {
launchctl kickstart -k $domain/$launchd_name
}
stop() {
launchctl stop $launchd_name
}
is_installed() {
launchctl list | fgrep -q $launchd_name
}
initialise() {
if [ $UID == 0 ]; then
launchd_file="/Library/LaunchDaemons/$launchd_name.plist"
domain=system
log_root="/var/log"
else
launchd_file="$HOME/Library/LaunchAgents/$launchd_name.plist"
domain=gui/$UID
log_root="$zasd_root/log"
fi
}
zasd_root="$(dirname "$(greadlink -f "$0")")/../.."
launchd_name=no.thj.zasd
keep_alive=true
# Parse arguments
while (($#)); do
case $1 in
-K|--keep-alive)
keep_alive=true
;;
-k|--no-keep-alive)
keep_alive=false
;;
*)
command=$1
;;
esac
shift
done
initialise
case $command in
install)
install
;;
uninstall)
uninstall
;;
load)
load
;;
unload)
unload
;;
start|restart)
start
;;
stop)
stop
;;
*)
echo "./zasctl [options] "
;;
esac