#!/bin/sh
#
# ez-ipupdate     Starts and stops the ez-ipupdate daemon
#
# chkconfig: - 55 45
#
# processname: ez-ipupdate
# description: Check and update your IP to dynamic DNS Server.
# pidfile: /var/run/ez-ipupdate/ez-ipudpate.pid
# config: /etc/ez-ipupdate.conf

ez_config=/etc/ez-ipupdate.conf
ez_bin=/usr/sbin/ez-ipupdate
ez_pid=/var/run/ez-ipupdate/ez-ipupdate.pid

# Make sure relevant files exist
[ -x "$ez_bin" -a -s "$ez_config" ] || exit 0

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

RETVAL=0
prog=ez-ipupdate

start() {
    # Start daemons.
    echo -n $"Starting $prog: "
    daemon $ez_bin --daemon --config-file $ez_config --pid-file $ez_pid
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}

stop() {
    # Stop daemons.
    echo -n $"Shutting down $prog: "
    killproc $prog -QUIT
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog && rm -f $ez_pid
}

reload() {
    # Reload config by sending a SIGHUP.
    echo -n $"Reloading $prog: "
    killproc $prog -HUP
    RETVAL=$?
    echo
}

restart() {
    stop
    start
    RETVAL=$?
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  reload)
    reload
    ;;
  condrestart)
    [ -e /var/lock/subsys/$prog ] && restart || :
    ;;
  status)
    status $prog
    RETVAL=$?
    if [ -r $ez_config ]; then
      ez_cache=`grep -E '^[[:space:]]*cache-file' $ez_config | cut -d "=" -f2`
      [ -n "$ez_cache" ] && \
        echo "Last IP update: "`cat $ez_cache | cut -d "," -f2`
    fi
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|condrestart|reload|status}"
    exit 1
esac

exit $RETVAL
