#!/bin/sh

##########################################################################
#   Synopsis:
#       auto-restart-services
#
#   Description:
#       Restart all services currently enabled.
#
#   Arguments:
#       None.
#
#   Returns:
#
#   See also:
#       
#   History:
#   Date        Name        Modification
#   2022-12-28  J Bacon     Begin
##########################################################################

usage()
{
    printf "Usage: $0\n"
    exit 1
}


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

if [ $# != 0 ]; then
    usage
fi
	
case $(auto-ostype) in
FreeBSD)
    localbin=/usr/local/sbin
    usrbin=/usr/sbin
    localrc=/usr/local/etc/rc.d
    rc=/etc/rc.d
    
    # FIXME: Make sure prereqs are restarted first (munge before lpjs_*)
    for var in $(grep -v '^#' /etc/rc.conf | grep -i '^.*_enable=.*yes' | cut -d = -f 1); do
	service=${var%_enable}
	if [ $service = nfs_server ]; then
	    auto-nfs-restart
	elif [ $service = nfsuserd ]; then
	    # Already handled by auto-nfs-restart
	    # elif needs a command
	    ls > /dev/null
	elif [ $service = ntpdate ]; then
	    auto-fix-date
	elif [ $service = ntpd ]; then
	    printf "Skipping ntpd, done by auto-fix-date...\n"
	elif [ $service = sddm ]; then
	    printf "Skipping sddm to avoid terminating login sessions...\n"
	    printf "Restart sddm manually when ready.\n"
	elif [ $service = gdm ]; then
	    printf "Skipping gdm to avoid terminating login sessions...\n"
	    printf "Restart gdm manually when ready.\n"
	elif [ -x $localbin/auto-$service-restart ]; then
	    # If a service requires special handling to restart, simply
	    # create an auto-$service-restart script
	    printf "Using auto-$service-restart...\n"
	    auto-$service-restart
	elif [ -e $localrc/$service ] || [ -e $rc/$service ]; then
	    service $service restart
	fi
    done
    ;;

*)
    # FIXME: Make auto-unsupported-os return 1?
    auto-unsupported-os $0
    exit 1
    ;;

esac
