#!/bin/sh
#
# /etc/rc.d/rc.noip2
#
# start/stop/restart the no-ip.com Dynamic dns client daemon
#
# To make noip2 client start automatically at boot, make this
# file executable: chmod 755 /etc/rc.d/rc.noip2
# and add this lines to /etc/rc.d/rc.local
#
# if [ -x /etc/rc.d/rc.noip2 ]; then
#  . /etc/rc.d/rc.noip2 start
# fi
#
# Written by slack.dhabyx@gmail.com and tested on Slackware 12.1
#

NOIP_PATH='/usr/bin'
NOIPCONFIG='/etc/no-ip2.conf'

config_exist() {
	if [ ! -f $NOIPCONFIG ] ; then
		echo "Please create the configuration file"
		echo "$NOIP_PATH/noip2 -C -c $NOIPCONFIG"
		exit 0;
	fi
}

start() {
	config_exist
	if ! /sbin/route -n | grep "^0.0.0.0" 1> /dev/null ; then
		echo "Gateway not defined yet, please init the network services."
		exit 0;
	fi
	echo "Starting no-ip client daemon: "
	/usr/bin/noip2 -c $NOIPCONFIG
}

stop() {
	config_exist
	if $NOIP_PATH/noip2 -S -c $NOIPCONFIG 2>&1 | grep Process 1> /dev/null ; then
		echo "Stopping no-ip client daemon: ";
		for i in `$NOIP_PATH/noip2 -S -c $NOIPCONFIG 2>&1 | grep Process | awk '{print $2}' | tr -d ','`
		do
			$NOIP_PATH/noip2 -c $NOIPCONFIG -K $i
		done
	else
		echo "no-ip client daemon is not running" && exit 0
	fi
}

status() {
	config_exist
	if $NOIP_PATH/noip2 -S -c $NOIPCONFIG 2>&1 | grep Process 1>/dev/null ; then
		echo "no-ip client daemon is running"
	else
		echo "no-ip client daemon is not runnig"
	fi
}

restart() {
	stop
	start
}

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  status)
	status
	;;
  restart)
  	restart
	;;
  *)
	echo "Usage: $0 {start|stop|status|restart}"
	exit 1
esac