#!/bin/sh
###############################################################################
# Domain Services for Windows Service Management Tool
#
# See the file LICENSE for redistribution information.
#
# Copyright (c)
#       Novell, Inc.  All rights reserved.
################################################################################


DEPENDENT_SERVICE_LIST='novell-xregd sshd rsyncd'
XAD_SERVICE_LIST='ndsd novell-named nscd rpcd xad-krb5kdc xad-kpasswdd xadsd nmb winbind smb w32tm-client'
_LIB=`/opt/novell/xad/share/dcinit/printConfigKey.pl "_Lib"`
export _LIB

TEXTDOMAINDIR=/opt/novell/xad/share/locale
TEXTDOMAIN=xad
export TEXTDOMAINDIR TEXTDOMAIN
SASL_PATH=/opt/novell/xad/$_LIB/sasl2
export SASL_PATH

VARDIR=/var/opt/novell/xad/log/
LOG=$VARDIR/novell-xad.log

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

# Local Housekeeping
FORCE=0
###############################################################################
Usage() {
	usage=`gettext "Usage:"`
	echo "xadcntrl start | stop | status | validate | reload [options]"
	echo "        Options: -f Force shutdown of services."
	exit 0
}

###############################################################################
force_shutdown() {
	if [ $FORCE -eq 1 ]; then
		if [ ! `pgrep $1 2>&1 > /dev/null` ]; then
			echo "Forced shutdown: $1" >> $LOG
			/usr/bin/kill -KILL $1 2>&1 >> $LOG
		fi
	fi
}

###############################################################################
ndsd() {
	ret=0
	servicename="ndsd.service"
	case $1 in
		start)	systemctl start $servicename 2>&1 >> $LOG;;
		stop)	systemctl stop $servicename 2>&1 >> $LOG
			force_shutdown ndsd
			;;
		restart) systemctl restart $servicename 2>&1 >> $LOG;;
		status)
			header_print "Checking for eDirectory Server"
			systemctl_status $servicename
			;;
		validate)
			header_print "Checking for eDirectory Server"
			systemctl_status $servicename
			test `pgrep ndsd` && ret=1
			/opt/novell/eDirectory/bin/ndsstat --config-file /etc/opt/novell/eDirectory/conf/nds.conf 2>&1 >> $LOG
			if [ $? -ne 0 ]; then
				ret=1
			fi
			test ! -S /var/opt/novell/xad/run/ldapi && ret=1
			;;
	esac
	return $ret
}

###############################################################################
novellnamed() {
	ret=0
	servicename="novell-named"
	`/opt/novell/xad/share/dcinit/printConfigKey.pl "DNS Master" | grep -q TRUE`
	if [ $? -ne 0 ]; then
		return 1
	fi
	case $1 in
		start)	systemctl start $servicename 2>&1 >> $LOG;;
		restart) systemctl restart $servicename 2>&1 >> $LOG;;
		stop)	systemctl stop $servicename 2>&1 >> $LOG
			force_shutdown novell-named
			;;
		status | validate)
			header_print "Checking for nameserver BIND"
			systemctl_status $servicename
			/usr/bin/nslookup `dnsdomainname` 2>&1 >> $LOG
			ret=$?
			;;

	esac
	return $ret
}

###############################################################################
nscd() {
	ret=0
	servicename="nscd.service"
	case $1 in
		start)	systemctl start $servicename 2>&1 >> $LOG;;
		restart) systemctl restart $servicename 2>&1 >> $LOG;;
		stop)	systemctl stop $servicename 2>&1 >> $LOG
			force_shutdown nscd
			;;
		status)
			header_print "Checking for Name Service Cache Daemon"
			systemctl_status $servicename
			;;
		validate)
			header_print "Checking for Name Service Cache Daemon"
			systemctl_status $servicename
			;;
	esac
		return $ret
}

###############################################################################
rpcd() {
	ret=0
	servicename="rpcd.service"
	case $1 in
		start)	systemctl start $servicename 2>&1 >> $LOG;;
		restart) systemctl restart $servicename 2>&1 >> $LOG;;
		stop)	systemctl stop $servicename 2>&1 >> $LOG
			force_shutdown rpcd
			;;
		status)
			header_print "Checking for RPC Endpoint Mapper Service"
			systemctl_status $servicename
			;;
		validate)
			header_print "Checking for RPC Endpoint Mapper Service"
			systemctl_status $servicename
			;;
	esac
	return $ret
}

###############################################################################
rsyncd() {
	ret=0
	servicename="rsyncd.service"
	case $1 in
		start)	systemctl start $servicename 2>&1 >>$LOG;;
		restart) systemctl restart $servicename 2>&1 >> $LOG;;
		status)	
			header_print "Checking for rsync daemon"
			systemctl_status $servicename
			;;
		validate)
			header_print "Checking for rsync daemon"
			systemctl_status $servicename
			;;
	esac
		return $ret
}

###############################################################################
header_print(){
	pad=$(printf '%0.1s' " "{1..80})
	maxpadlength=55
	heading=$1

	printf '%s' "$heading"
	printf '%*.*s' 0 $((maxpadlength - ${#heading})) "$pad"
}

###############################################################################
systemctl_status(){
	servicename=$1
	servicestatus=$(systemctl is-active $servicename)

	if [ ${servicestatus} == "active" ]
	then
		printf "${GREEN}%s${NC}\n" $servicestatus
	else
		printf "${RED}%s${NC}\n" $servicestatus
	fi
}
###############################################################################
sshd() {
	ret=0
	servicename="sshd.service"
	case $1 in
		start)	systemctl start $servicename 2>&1 >>$LOG;;
		restart) systemctl restart $servicename 2>&1 >> $LOG;;
		status)
			header_print "Checking for service sshd"
			systemctl_status $servicename
			;;
		validate)
			header_print "Checking for service sshd"
			systemctl_status $servicename
			;;
	esac
	return $ret
}

#############################################################################
xadkrb5kdc() {
	ret=0
	servicename="xad-krb5kdc.service"
	case $1 in
		start)	systemctl start $servicename 2>&1 >> $LOG;;
		restart) systemctl restart $servicename 2>&1 >> $LOG;;
		stop)	systemctl stop $servicename 2>&1 >> $LOG
			force_shutdown krb5kdc
			;;
		status)
			header_print "Checking for Kerberos KDC Service"
			systemctl_status $servicename
			;;
		validate)
			header_print "Checking for Kerberos KDC Service"
			systemctl_status $servicename
			;;
	esac
	return $ret
}

###############################################################################
xadkpasswdd() {
	ret=0
	servicename="xad-kpasswdd"
	case $1 in
		start)	systemctl start $servicename 2>&1 >> $LOG;;
		restart) systemctl restart $servicename 2>&1 >> $LOG;;
		stop)	systemctl stop $servicename 2>&1 >> $LOG
			force_shutdown kpasswdd
			;;
		status)
			header_print "Checking for Kerberos Password Change Server"
			systemctl_status $servicename
			;;
		validate)
			header_print "Checking for Kerberos Password Change Server"
			systemctl_status $servicename
			;;
	esac
	return $ret
}

###############################################################################
xadsd() {
	ret=0
	servicename="xadsd.service"
	case $1 in
		start)	systemctl start xadsd.service 2>&1 >> $LOG;;
		restart) systemctl restart $servicename 2>&1 >> $LOG;;
		stop)	systemctl stop xadsd.service 2>&1 >> $LOG
			force_shutdown xadsd
			;;
		status)
			header_print "Checking for Domain Services Daemon"
			systemctl_status $servicename
			;;
		validate)
			header_print "Checking for Domain Services Daemon"
			systemctl_status $servicename
			;;
	esac
	return $ret

}

###############################################################################
#TODO Remove extra outputs from systemctl
nmb() {
	ret=0
	servicename="nmb.service"
	case $1 in
		start)	systemctl start $servicename 2>&1 >> $LOG;;
		restart) systemctl restart $servicename 2>&1 >> $LOG;;
		stop)	systemctl stop $servicename 2>&1 >> $LOG
			force_shutdown nmbd
			;;
		status)
			header_print "Checking for Samba NMB daemon"
			systemctl_status $servicename
			;;
		validate)
			header_print "Checking for Samba NMB daemon"
			systemctl_status $servicename
			;;
	esac
	return $ret

}

###############################################################################
#TODO Remove extra outputs from systemctl
winbind() {
	ret=0
	servicename="winbind.service"
	case $1 in
		start)	systemctl start $servicename 2>&1 >> $LOG;;
		restart) systemctl restart $servicename 2>&1 >> $LOG;;
		stop)	systemctl stop $servicename 2>&1 >> $LOG
			force_shutdown winbindd
			;;
		status)
			header_print "Checking for Samba WINBIND daemon"
			systemctl_status $servicename
			;;
		validate)
			header_print "Checking for Samba WINBIND daemon"
			systemctl_status $servicename
			;;
	esac
	return $ret

}

###############################################################################
smb() {
	ret=0
	servicename="smb.service"
	case $1 in
		start)	systemctl start $servicename 2>&1 >> $LOG;;
		restart) systemctl restart $servicename 2>&1 >> $LOG;;
		stop)	systemctl stop $servicename 2>&1 >> $LOG
			force_shutdown smbd
			;;
		status)
			header_print "Checking for Samba SMB daemon"
			systemctl_status $servicename
			;;
		validate)
			header_print "Checking for Samba SMB daemon"
			systemctl_status $servicename
			admin_sam_name=`/opt/novell/xad/share/dcinit/provisionTools.sh get-admin-sam-name`
			#Below tools has some error hence commented for now, but has to be fixed
			#in the next cycle.
			#/opt/novell/xad/samba/bin/wbinfo -i "$admin_sam_name" 2>&1 >> $LOG
			#/opt/novell/xad/samba/bin/rpcclient -k ncalrpc: -c dsroledominfo 2>&1 >> $LOG
			;;
	esac
	return $ret

}

###############################################################################
w32tmclient() {
        ret=0
        servicename="w32tm_client.service"
        case $1 in
                start)  systemctl start $servicename 2>&1 >> $LOG;;
                restart)
                        systemctl restart $servicename 2>&1 >> $LOG;;
                stop)   systemctl stop $servicename 2>&1 >> $LOG
                        force_shutdown w32tm_client
                        ;;
                status | validate)
                        header_print "Checking for w32tm client service"
                        systemctl_status $servicename
                        ;;
        esac
        return $ret

}

###############################################################################
novellxregd() {
	ret=0
	servicename="novell-xregd"
	case $1 in
		start)	systemctl start $servicename 2>&1 >> $LOG
			;;
		restart) systemctl restart $servicename 2>&1 >> $LOG;;
		status | validate)
			header_print "Checking for novell-xregd daemon"
			systemctl_status $servicename
			;;
	esac
	return $ret

}

###############################################################################
start_services() {
	echo -e "\e[1;34mChecking dependent services\e[0m"
	for service in $DEPENDENT_SERVICE_LIST
	do
		gettext -s ">> Checking $service"
		$(echo "$service" | sed 's/-//g') start >/dev/null 2>&1
	done

	echo  -e "\e[1;34m\nStarting DSfW services\e[0m"
	for service in $XAD_SERVICE_LIST
	do
		gettext -s ">> Starting $service"
		$(echo "$service" | sed 's/-//g') start >/dev/null 2>&1
	done
#	/opt/novell/xad/sbin/w32tm_client >> /var/opt/novell/xad/log/w32tm_client.log 2>&1 &
}


###############################################################################
stop_services() {
	echo  -e "\e[1;34mStopping DSfW services\e[0m"
	for service in `echo $XAD_SERVICE_LIST | tr ' ' '\n' | tac`
	do
		gettext -s ">> Stopping $service"
		$(echo "$service" | sed 's/-//g') stop >/dev/null 2>&1
	done
#	killall -9 w32tm_client >> /var/opt/novell/xad/log/w32tm_client.log 2>&1 &

}

###############################################################################
status_services() {
	echo -e "\e[1;34mChecking status of ...\e[0m"
	for service in $DEPENDENT_SERVICE_LIST
	do
		$(echo $service|sed 's/-//g') status
	done

	echo  -e "\e[1;34m\nChecking status of ...\e[0m"
	for service in $XAD_SERVICE_LIST
	do
		$(echo $service|sed 's/-//g') status
	done
}

###############################################################################
validate_services() {
	echo -e "\e[1;34mValidating dependent services\e[0m"
	for service in $DEPENDENT_SERVICE_LIST
	do
		$(echo $service|sed 's/-//g') validate
	done

	echo  -e "\e[1;34m\nValidating DSfW\e[0m"
	for service in $XAD_SERVICE_LIST
	do
		$(echo $service|sed 's/-//g') validate
	done
}

###############################################################################
reload_services() {
	echo -e "\e[1;34mChecking dependent services\e[0m"
	for service in $DEPENDENT_SERVICE_LIST
	do
		gettext -s ">> Re-Starting dependent $service"
		$(echo "$service" | sed 's/-//g') restart >/dev/null 2>&1
	done

	echo  -e "\e[1;34m\nRe-Starting DSfW services\e[0m"
	for service in $XAD_SERVICE_LIST
	do
		gettext -s ">> Re-Starting $service"
		$(echo "$service" | sed 's/-//g') restart >/dev/null 2>&1
	done
#	killall -9 w32tm_client >> /var/opt/novell/xad/log/w32tm_client.log 2>&1 &
#	sleep 3
#	/opt/novell/xad/sbin/w32tm_client >> /var/opt/novell/xad/log/w32tm_client.log 2>&1 &

	sleep 10
	validate_services
}

###############################################################################
main() {
	action=$1
	shift

	grep "PROVISIONED = YES" /etc/opt/novell/xad/xad.ini > /dev/null 2>&1
	if [ $? != "0" ]; then
		echo -e "DSfW services are not yet configured. Run provision_dsfw.sh to configure DSfW services."
		exit 0
		fi


	while getopts f OPTION;
	do
		case $OPTION in
			f) FORCE=1 ;;
			*) Usage ;;
		esac
	done

	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" >> $LOG
	date 2>&1 >> $LOG

	case $action in
		start)		start_services ;;
		stop)		stop_services  ;;
		status)		status_services ;;
		reload)		reload_services ;;
		validate)	validate_services ;;
		*)		Usage ;;
	esac
}

main $@
