#!/bin/sh
#
# dictd:	Starts the Dictionary Daemon
#
# chkconfig:	- 90 10
# description:	This is a daemon for the Dictionary Server Protocol (DICT), \
#               a TCP transaction based query/response protocol that allows \
#               a client to access dictionary definitions from a set of \
#		natural language dictionary databases.
# processname:	dictd
# config:	/etc/dictd.conf 
# config:       /etc/dictd/*

DAEMON_FILE=dictd
DAEMON_NAME="Dictionary Daemon"  
DAEMON_CONF=/etc/dictd.conf

# from /etc/init.d/functions of rc-scripts.spec by PLD Linux
is_yes()
{
	# Test syntax	
	if [ $# = 0 ] ; then
		msg_usage " is_yes {value}"
		return 2
	fi

	# Check value
	case "$1" in
		yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
			# true returns zero
			return 0
		;;
		*)
			# false returns one
			return 1
		;;
	esac
}

is_no()
{
	# Test syntax
	if [ $# = 0 ] ; then
		msg_usage " is_no {value}"
		return 2
	fi

	case "$1" in
		no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
			# true returns zero
			return 0
			;;
		*)
			# false returns one
			return 1
		;;
	esac
}

# Colors workaround
termput() 
{
	if [ ! -d /usr/share/terminfo ] || \
	     [ ! -x /usr/bin/tput -a ! -x /bin/tput ]; then
		case "$1" in
		  hpa)
		  	echo -ne "\033[$(($2+1))G"
			;;
		  cuu*)
		  	echo -ne "\033[${2}A"
			;;
		  el)
		  	echo -ne "\033[0K"
			;;
		  setaf)
		  	is_yes "$COLOR_INIT" && echo -ne "\033[0;3${2}m"
		  	;;
		  op)
		  	termput setaf 9
			;;
		  esac
	else
		# check if we are on proper terminal
		tput longname > /dev/null 2>&1 /dev/null || return

	        case "$1" in
		  hpa | cuu* | el)
		  	tput "$@"
			;;
		  setaf)
		  	is_yes "$COLOR_INIT" && tput "$@"
			;;
		  op)
		  	tput setaf 9
			;;
		 esac
	fi
}

# printf equivalent
printf_()
{
	typeset text m
	text="$1" ;
	shift ;
	if [ $# -gt 0 ]; then
		m="$1";
        	shift;
		while [ $# -gt 0 ]; do
			m="$m\",\"$1" ;
			shift ;
		done
	fi
	awk "BEGIN {printf \"$text\", \"$m\"; }"
}

# National language support function
nls()
{
	typeset msg_echo old_nls_domain text message
	msg_echo='\n'
	old_nls_domain="$NLS_DOMAIN"
	# parse command line
	# don't use -o instead || here - this will broke ksh --misiek
	while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
		case "$1" in
			--nls-domain)
				shift
				NLS_DOMAIN="$1"
				shift
				;;
			-n)
				msg_echo=''
				shift
				;;
		esac
	done
	message="$1"
	shift
	# empty message, so we return --misiek
	if [ -z "$message" ]; then
		NLS_DOMAIN="$old_nls_domain"
		echo -en "$msg_echo"
		return
	fi
			
	if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
		text=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${NLS_DOMAIN:-rc-scripts}" "$message")
		printf_ "$text" "$@"
	else
		printf_ "$message" "$@"
	fi
	
	echo -en "$msg_echo"
	NLS_DOMAIN="$old_nls_domain"
}

msg_network_down()
{
	nls "ERROR: Networking is down. %s can't be run." "$1"
}

msg_starting()
{
	show "Starting %s service" "$1"
}

msg_already_running()
{
	nls "%s service is already running." "$1"
}

msg_stopping()
{
	show "Stopping %s service" "$1"
}

msg_not_running()
{
	nls "%s service is not running." "$1"
}

msg_reloading()
{
	show "Reloading %s service" "$1"
}

msg_usage()
{
	nls "Usage: %s" "$*"
}
# Some functions to handle PLD-style messages
show() 
{	
	typeset text
	text=$(nls "$@")
	echo "$text"
#	awk "BEGIN { for (j=length(\"$text\"); j<$INIT_COL; j++) printf \".\" }"
}

deltext() 
{
	termput hpa $INIT_COL
}

# Displays message in square brackests ("[ DONE ]"). Takes two arguments.
# First is the text to display, second is color number to use (argument to
# tput setaf). If second argument is not given, default (2, green) will be
# used).
progress()
{
	typeset COLOR
	if [ -n "$2" ]; then COLOR="$2"; else COLOR="2"; fi
	deltext
	echo -n "$(termput setaf 6)[$(termput setaf "$COLOR") $(nls --nls-domain rc-scripts "$1") $(termput setaf 6)]$(termput op)"
}

busy() 
{
	progress "BUSY" 5
}

generate_dictdconf() {
    umask 022
    if ls /etc/dictd/*.dictconf >/dev/null 2>&1; then 
	echo "# DO NOT EDIT! This file is autogenerated by $0." >$DAEMON_CONF
	echo "# To configure dictd edit /etc/dictd/* files and restart daemon"\
          >>$DAEMON_CONF
	cat /etc/dictd/dictd-main.conf /etc/dictd/*.dictconf >>$DAEMON_CONF
	return 0
    fi 

    echo "$0: no dictionaries found"
    return 1	
}

# Source function library.
. /etc/init.d/functions

# Get network config
. /etc/sysconfig/network

if is_no "${NETWORKING}"; then
	msg_network_down ${DAEMON_FILE}
        exit 1
fi

# Get sysconfig
[ -f /etc/sysconfig/${DAEMON_FILE} ] && . /etc/sysconfig/${DAEMON_FILE}

RETVAL=0
# See how we were called.
case "$1" in
    start)
	[ -x /usr/sbin/${DAEMON_FILE} ] || exit 0
	if [ ! -f /var/lock/subsys/${DAEMON_FILE} ]; then
		if generate_dictdconf; then 
		    msg_starting ${DAEMON_NAME}
		    daemon ${DAEMON_FILE} ${DICTD_OPTS}
		    pidofproc ${DAEMON_FILE} >/dev/null 2>&1
		    RETVAL=$?
		    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/${DAEMON_FILE}
		else  
		    exit 1
		fi    
	else
		msg_already_running ${DAEMON_NAME}
		exit 1
	fi
        ;;
    stop)
	if [ -f /var/lock/subsys/${DAEMON_FILE} ]; then
		msg_stopping ${DAEMON_NAME}
		busy
		killproc /usr/sbin/${DAEMON_FILE} 
		rm -f /var/lock/subsys/${DAEMON_FILE} >/dev/null 2>&1
	else
		msg_not_running ${DAEMON_NAME}
		exit 1
	fi	
	;;
  status)
        status ${DAEMON_FILE}
        ;;
  restart|reload)
        $0 stop
        $0 start
        ;;
    *)
	msg_usage "$0 {start|stop|status|restart|reload}"
	exit 1
	;;
esac

exit $RETVAL
