#!/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
PROG=ez-ipupdate
LOCKFILE="/var/lock/subsys/$PROG"

# 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

start() {
    # Start daemons.
    start_daemon --lockfile "$LOCKFILE" -- $EZ_BIN --daemon --config-file $EZ_CONFIG --pid-file $EZ_PID
    RETVAL=$?
	return $RETVAL
}

stop() {
    # Stop daemons.
	stop_daemon --lockfile "$LOCKFILE" -SIGQUIT -- $EZ_BIN --pidfile $EZ_PID 
    RETVAL=$?
	return $RETVAL
}

reload() {
    # Reload config by sending a SIGHUP.
	stop_daemon --pidfile $EZ_PID -SIGHUP -- $EZ_BIN
    RETVAL=$?
}

restart() {
    stop
    start
    RETVAL=$?
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  reload)
    reload
    ;;
  condstop)
    if [ -e "$LOCKFILE" ]; then
      stop
    fi
  ;;
  condrestart)
    if [ -e "$LOCKFILE" ]; then
      restart
    fi
  ;;
  condreload)
    if [ -e "$LOCKFILE" ]; then
      reload
    fi
  ;;
  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
