#!/bin/bash
#
# iris3dviewd	Startup script for the IRIS 3DView background visualizer
#
# chkconfig: 35 99 1
# description: IRIS 3DView background visualizer. It is user to produce \
#              off-screen visualizations from IRIS radar data
# processname: iris3dviewd
# config:  /etc/default/iris3dviewd
#          /home/iris3d/.conf/Vaisala/iris3dviewd.conf
# pidfile: /var/run/iris3dviewd.pid

name=iris3dviewd

# read default settings
if [ -d /etc/sysconfig/ ]; then
    defdir=/etc/sysconfig
else
    defdir=/etc/default
fi

if [ -f $defdir/$name ]; then
	#echo "Sourcing $defdir/$name"
    . $defdir/$name
fi


desc="IRIS 3DView background visualizer"
if [ -x  /usr/bin/${name} ]; then
  daemon=/usr/bin/${name}
elif [ -x  /usr/local/bin/${name} ]; then
   daemon=/usr/local/bin/${name}
else
   echo $"Did not find ${name}. Exiting"
   exit 1
fi
pidfile=${PIDFILE-/var/run/${name}.pid}
lockfile=${LOCKFILE-/var/lock/${name}}
user=${BG_USER-iris3d}
group=${BG_GROUP-iris3d}
RETVAL=0

# check existence of a command
command_exists()
{
        if command -v $1 &>/dev/null;
        then
                return 0
        else
                return 1
        fi
}

# Generate platform dependent start/stop/reload commands
make_commands()
{
	if command_exists start-stop-daemon;
	then
		# debian/ubuntu
		# Load functions library
        . /lib/lsb/init-functions
		_cmd="start-stop-daemon"
		_opts="--chuid $user:$group --exec $daemon --oknodo"
		start_command="$_cmd  $_opts --start -- $OPTIONS";
		stop_command="$_cmd --retry=TERM/30/KILL/5 --stop $_opts";
		reload_command="$_cmd --stop --signal 1 $_opts";
		status_command="status_of_proc -p ${pidfile} $daemon $name"
	elif [ -f "/etc/init.d/functions" ];
	then
		# fedora/RHEL/centos
		# Load functions library
		. /etc/init.d/functions  # load common functions
		start_command="daemon --user $user --pidfile=${pidfile} $daemon $OPTIONS"
		stop_command="killproc -p ${pidfile} -d 10 $daemon"
		reload_command="killproc -p ${pidfile} $daemon -HUP"
		status_command="status -p ${pidfile} $daemon $name"

	else
		start_command="echo not supported"
		stop_command="echo not supported"
		reload_command="echo not supported"
	fi
}

# Start the service
start() {
    echo -n "Starting $desc ($name): "
    # echo ${start_command}
    # adjust pidfile privileges
    if [ ! -f ${pidfile} ]; then 
        touch ${pidfile}
    fi
    chown $user:$group ${pidfile}
    chmod 644 ${pidfile}
    #start daemon 
    if [ -f  "/home/$user/$name.log" ]; then
    	/usr/sbin/logrotate -f /home/$user/.config/Vaisala/logrotate.conf   
    fi
    ${start_command}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch ${lockfile}
    return $RETVAL
}

# Stop the service 
stop() {
    echo -n "Stopping $desc ($name): "
    ${stop_command}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

# Reload configuration - reopen logfile
reload() {
    :echo -n "Reloading $name configuration: "
    ${reload_command}
    RETVAL=$?
    echo
}


### main logic ###

make_commands

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    ${status_command}
	RETVAL=$?
	;;
  restart)
	stop
	start
	;;
  reload)
	reload
	;;
  condrestart)
	if [ -f ${pidfile} ] ; 
	then
		stop
		start
	fi
	;;
  help)
	$daemon --help $@
	RETVAL=$?
	;;
  *)
	echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|help|}"
	exit 1
esac

exit $RETVAL

