#!/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
#   2012-01-08  Jason Bacon Begin
#   2024-10-21  izzy Meyer  Start OpenBSD support
#   2024-11-11  Jason Bacon Add man page template and usage
##########################################################################
    
stop_service()
{
    # FIXME: Don't do OS checks inside functions.  Less confusing if
    # OS separation is done in one place in each script.
    case $(auto-ostype) in
	FreeBSD|NetBSD)
	    if auto-service-enabled $service; then
		printf "$service is enabled.\n"
		service $script stop
	    else
		printf "$service is not enabled.\n"
		service $script onestop
	    fi
	    # FIXME: Does not work for rpc.statd
	    if pkill $service; then
		printf "Killed rogue $service.\n"
	    fi
	    rm -f /var/run/$service.pid
	    return 0
	    ;;

	OpenBSD)
	    if auto-service-enabled $service; then
		printf $service is enabled.\n
		rcctl stop $script
	    else
		printf "$service is not enabled.\n"
		rcctl -f stop $script
	    fi

	    return 0
	    ;;
	
    esac
}


usage()
{
    # FIXME: Tailor usage message to OS.  Currently BSD-centric.
    cat << EOM

Usage: $0 [-s script-name] service-name calling-program-name|nocomment

Examples:
    $0 -s kerberos5_server kerberos my-setup-script
    $0 -s /usr/pkg/share/lpjs/Systemd/lpjs_compd.service lpjs_compd nocomment
    $0 -s /usr/pkg/share/lpjs/Launchd/org.pkgsrc.lpjs_compd.plist org.pkgsrc.lpjs_compd nocomment

"script-name" may be an absolute pathname, or just the basename of
the RC/init/systemd script or launchd .plist file.  If only the
basename is given, and the script does not exist in the current
working directory, standard directories such as /etc/rc.d, /etc/systemd,
and /Library/LaunchDaemons are checked.

EOM
    
    exit 1
}

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

if [ $# = 4 ] && [ $1 = '-s' ]; then
    script=$2
    shift
    shift
elif [ $# = 2 ]; then
    script=$1
else
    usage
fi
service=$1
caller=$2

case $(auto-ostype) in
DragonFly|FreeBSD)
    if auto-service-enabled $service; then
	printf "$service already enabled, restarting.\n"
	service $service restart || true
	exit 0
    fi
    
    if grep -q "^${service}_enable=\"NO\"" /etc/rc.conf; then
	sed -i '' "s|${service}_enable=\"NO\"|${service}_enable=\"YES\"|g" /etc/rc.conf
    fi

    auto-set-conf-var ${service}_enable YES /etc/rc.conf $caller
    stop_service || true
    if [ -e /etc/rc.d/$script ] || [ -e $(auto-localbase)/etc/rc.d/$script ]; then
	# Use restart instead of start to prevent desktop-installer
	# from exiting after killing rogue devd?
	service $(basename $script) restart || true
    else
	printf "Warning: No service startup script found for $service.\n"
    fi
    ;;
    
NetBSD)
    if grep -q "^${service}=YES" /etc/rc.conf; then
	printf "$service already enabled, restarting.\n"
	service $service restart
	exit 0
    fi
    
    set -x
    if grep -q "^${service}=NO" /etc/rc.conf; then
	sed -i'' -e "s|${service}=NO|${service}=YES|g" /etc/rc.conf
    else
	auto-set-conf-var $service YES /etc/rc.conf $caller
    fi

    stop_service
    if [ -e /etc/rc.d/$script ]; then
	service $(basename $script) restart
    # If user supplied a full or relative pathname
    elif [ -e $script ]; then
	cp $script /etc/rc.d
	service $(basename $script) restart
    # This is the canonical location for pkgsrc RC scripts
    elif [ -e $(auto-localbase)/share/examples/rc.d/$script ]; then
	cp $(auto-localbase)/share/examples/rc.d/$script /etc/rc.d
	service $script restart
    else
	printf "Warning: No service startup script found for $service.\n"
    fi
    ;;

OpenBSD)
    if rcctl ls on | grep -q "${service}"; then
	printf "$service already enabled, restarting.\n"
	rcctl restart $service
	exit 0
    else
	rcctl enable $service
    fi

    stop_service
    if [ -e /etc/rc.d/$script ]; then
	rcctl restart $(basename $script)
    fi
    ;;

Debian|RHEL)
    if [ -d /etc/systemd/system ]; then
	pkgsrc_dir1="/usr/pkg/share/$service/Systemd"
	pkgsrc_dir2="/usr/pkg/lib/systemd/system"
	systemd_dir='/etc/systemd/system'
	
	found=false
	if echo $script | grep -q '^/'; then
	    # Absolute path provided
	    path=$script
	    found=true
	else
	    # Relative path provided, check common locations
	    # Prefer pkgsrc, since it's an add-on.
	    for dir in $pkgsrc_dir1 $pkgsrc_dir2 $systemd_dir $systemd_dir/*.wants; do
		path=$dir/$script.service
		printf "Checking $path...\n"
		if [ -e $path ]; then
		    found=true
		    break
		fi
	    done
	fi
	
	systemctl daemon-reload
	systemctl stop $service || true
	printf "Enabling $path...\n"
	systemctl enable $path
	printf "Starting $service...\n"
	systemctl start $service
	
	if [ $found = false ]; then
	    printf "$script: No such service found.\n" >> /dev/stderr
	    exit 1
	fi
    else
	printf "$0: Only systemd-based Linux systems are supported at this time.\n"
	auto-unsupported-os $0
	exit 1
    fi
    ;;

Darwin)
    if [ ! -e $script ]; then
	printf "$script: File not found.\n" >> /dev/stderr
	exit 1
    fi
    
    dir=/Library/LaunchDaemons
    installed_script=$dir/$(basename $script)
    
    printf "Stopping service $service...\n"
    launchctl stop $service || true
    
    if [ -e $installed_script ]; then
	printf "Disabling service $service...\n"
	launchctl bootout system $installed_script || true
    fi
    
    cp -f $script /Library/LaunchDaemons/
    
    printf "Enabling $installed_script...\n"
    launchctl bootstrap system $installed_script
    
    printf "Starting service $service...\n"
    launchctl start $service || true
    ;;
    
*)
    auto-unsupported-os $0
    exit 1
    ;;

esac
