#!/bin/sh -e

##########################################################################
#   Title:
#       Optional, defaults to the name of the script sans extention
#
#   Section:
#       8
#
#   Synopsis:
#       
#   Description:
#       
#   Arguments:
#       
#   Returns:
#
#   Examples:
#
#   Files:
#
#   Environment:
#
#   See also:
#       
#   History:
#   Date        Name        Modification
#   2024-09-23  Jason Bacon Add man page template and usage
##########################################################################

usage()
{
    if [ $(uname) = Darwin ]; then
	printf "Usage: $0 plist-name[.plist]\n"
    else
	printf "Usage: $0 rc.conf-name\n"
    fi
    exit 1
}


##########################################################################
#   Main
##########################################################################

if [ $# != 1 ]; then
    usage
fi
    
case $(auto-ostype) in
FreeBSD)
    service=$1
    service $service stop || true
    if auto-service-enabled $service; then
	sysrc ${service}_enable=NO
    fi
    ;;

NetBSD)
    service=$1
    service $service stop || true
    if auto-service-enabled $service; then
	sed -I'.bak' -e "s|^${service}=.*$|${service}=NO|" /etc/rc.conf
    fi
    ;;
    
Darwin)
    service="$1"
    dir=/Library/LaunchDaemons
    # Full name of plist file was provided, e.g. org.pkgsrc.lpjs_compd.plist
    if [ -e $dir/$service ]; then
	plist=$dir/$service
    # Just the service name, e.g. lpjs_compd
    elif [ -e $dir/org.pkgsrc.$service.plist ]; then
	plist=$dir/org.pkgsrc.$service.plist
    else
	printf "$service is not installed in $dir.\n"
	exit 0
    fi
    
    printf "Stopping service $plist...\n"
    launchctl stop $plist || true
    
    if [ -e $plist ]; then
	printf "Disabling service $plist...\n"
	if ! launchctl bootout system $plist; then
	    printf "Is $plist already disabled?\n" >> /dev/stderr
	fi
    else
	printf "$install_script not found.\n" >> /dev/stderr
    fi
    ;;

RHEL)
    service="$1"
    printf "Disabling $service...\n"
    systemctl stop $service
    systemctl disable $service
    ;;

*)
    auto-unsupported-os $0
    exit 1
    ;;

esac
