#!/bin/sh
# $Id: osx-frob-service,v 1.2 2018/05/03 18:30:00 friedman Exp $

get_pl()
{
    for dir in /Library/LaunchDaemons /System/Library/LaunchDaemons; do
        for file in "$dir/$1" "$dir/$1.plist"; do
            if [ -f "$file" ]; then
                echo "$file"
                return 0
            fi
        done
    done
    return 1
}

get_label()
{
    /usr/libexec/PlistBuddy -c "print :Label" "$1"
}

do_enable()
{
    for arg in "$@"; do
        pl=`get_pl "$arg"`
        label=`get_label "$pl"`
        launchctl load -w "$pl"
        launchctl start "$label"
    done
}

do_disable()
{
    for arg in "$@"; do
        pl=`get_pl "$arg"`
        label=`get_label "$pl"`
        launchctl stop "$label"
        launchctl unload -w "$pl"
    done
}

main()
{
    local cmd=$1
    shift

    case $# in
        0 ) echo Usage: ${0##*/} [enable|disable]  com.apple.whatever
            exit 1 ;;
    esac

    do_$cmd "$@"
}

main "$@"

# eof
