#!/sbin/sh

# $Id: ntpd.solaris,v 1.5 2004/01/31 19:58:15 alban Exp alban $

# simple ntpd start up script for solaris (tested with solaris 9)

# Copyright (C) 2004  David Alban
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

######################################################
### change the following definitions as needed
######################################################

     # directory in which the ntp daemon and commands reside
bindir=/usr/local/bin

     # the basename(1) of the daemon
daemon_name=ntpd

     # the path to the daemon executable
daemon=$bindir/$daemon_name

cfgfile=/etc/ntp.conf
logfile=/var/log/$daemon_name

     # Options with which the ntp daemon will be invoked.  Remove "-l
     # $logfile" if you want log messages to go to the system log.  Also,
     # don't use -g if you have applications which cannot tolerate the
     # "stepping back" of system time.
daemon_options="-c $cfgfile -g -l $logfile -N high"

######################################################
### shouldn't have to change anything below here
######################################################

PATH=/sbin/bin:/usr/bin:$bindir
export PATH

daemon_is_running () {
  pid_and_procname=` ps -e -o pid,comm | grep $daemon'$' ` \
    && pid=` echo "$pid_and_procname" | sed 's/^ *//' | cut -f1 -d' ' `
} # daemon_is_running

case "$1" in
  start)   if daemon_is_running; then
             echo "$daemon_name already running"
             exit 1
           fi
           if [ -f $cfgfile -a -x $daemon ]; then
             if $daemon $daemon_options; then
               echo $daemon_name started
             else
               echo 1>&2 "$0: $daemon_name returned $? ; not started"
               exit 1
             fi
           fi
           ;;

  stop)    if daemon_is_running; then
             kill $pid && echo 1>&2 "killed $daemon_name($pid)"
           else
             echo 1>&2 "$daemon_name not running"
           fi
           ;;

  stat|status)
           if daemon_is_running; then
             ps -f -p $pid
             if [ -x $bindir/ntpdc ]; then
               echo
               ntpdc -p
             fi
           else
             echo cannot find $daemon in process table
           fi
           ;;

  restart) sh $0 stop && sh $0 start
           ;;

  *)       echo "usage: $0 { start | stop | status | restart }"
           ;;
esac