#!/bin/sh -e
    
##########################################################################
#   Script description:
#       
#   Arguments:
#       
#   Returns:
#       
#   History:
#   Date        Name        Modification
#   2012-01-08  Jason Bacon Begin
##########################################################################

stop_service()
{
    if [ -e /etc/rc.d/$script ] || [ -e $(auto-localbase)/etc/rc.d/$script ]; then
	service $script stop || true
    fi
    if pkill $service; then
	printf "Killed rogue $service.\n"
    fi
    rm -f /var/run/$service.pid
    return 0
}


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

if [ $# = 4 ] && [ $1 = '-s' ]; then
    script=$2
    shift
    shift
elif [ $# = 2 ]; then
    script=$1
else
    # 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
fi
service=$1
caller=$2

case $(auto-ostype) in
DragonFly|FreeBSD)
    if grep -q "^${service}_enable=\"YES\"" /etc/rc.conf; then
	printf "$service already enabled.\n"
	printf "If you need the service restarted, you must do it manually.\n"
	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
    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.\n"
	printf "If you need the service restarted, you must do it manually.\n"
	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) start
    elif [ -e $script ]; then
	cp $script /etc/rc.d
	service $(basename $script) start
    # 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 start
    else
	printf "Warning: No service startup script found for $service.\n"
    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
	
	printf "Enabling $path...\n"
	systemctl enable $path
	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...\n"
    launchctl stop $service || true
    
    if [ -e $installed_script ]; then
	printf "Disabling 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...\n"
    launchctl start $service || true
    ;;
    
*)
    auto-unsupported-os $0
    exit 1
    ;;

esac
