CHANGE mod_netconf renamed to netopeerguid

Now installing as a service netopeerguid.rc.
diff --git a/src/Makefile.am b/src/Makefile.am
index 79dcd05..a891ffd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,28 +1,27 @@
-SRCS=mod_netconf.c \
-     notification-server.c
+SRCS=netopeerguid.c \
+     notification_server.c
 
 HDRS=message_type.h \
-     notification_module.h \
-     mod_netconf.h
-
-noinst_PROGRAMS=webgui-backend
-webgui_backend_SOURCES=mod_netconf.c notification-server.c message_type.h notification_module.h mod_netconf.h
+     notification_server.h \
+     netopeerguid.h
 
 EXTRA_DIST=$(SRCS) $(HDRS)
 
+bin_PROGRAMS=netopeerguid
+
 dist-hook:
 	cp $(SRC) $(HDRS) $(distdir)
 
-all: mod_netconf.so webgui-backend
+all: netopeerguid
 
-mod_netconf.so: mod_netconf.c notification-server.c
-	$(CC) $(CFLAGS) -o $@ $(srcdir)/mod_netconf.c $(srcdir)/notification-server.c $(LIBS)
+netopeerguid$(EXEEXT): netopeerguid.c notification_server.c netopeerguid.h
+	$(CC) $(CFLAGS) -o $@ $(srcdir)/netopeerguid.c $(srcdir)/notification_server.c $(LIBS)
 
-webgui-backend$(EXEEXT): mod_netconf.c notification-server.c mod_netconf.h
-	$(CC) $(CFLAGS) -o $@ $(srcdir)/mod_netconf.c $(srcdir)/notification-server.c $(LIBS)
-
+install-exec-hook:
+	$(INSTALL) -d $(DESTDIR)/etc/init.d/;
+	$(INSTALL_PROGRAM) -m 755 netopeerguid.rc $(DESTDIR)/etc/init.d/
 clean-local:
-	rm -rf .libs *.la *.lo *.slo webgui-backend
+	rm -rf netopeerguid
 
 distclean-local:
 	rm -rf $(RPMDIR)
diff --git a/src/mod_netconf.c b/src/netopeerguid.c
similarity index 98%
rename from src/mod_netconf.c
rename to src/netopeerguid.c
index e40481e..1e7161b 100644
--- a/src/mod_netconf.c
+++ b/src/netopeerguid.c
@@ -3,12 +3,14 @@
  * \brief NETCONF Apache modul for Netopeer
  * \author Tomas Cejka <cejkat@cesnet.cz>
  * \author Radek Krejci <rkrejci@cesnet.cz>
+ * \author Michal Vasko <mvasko@cesnet.cz>
  * \date 2011
  * \date 2012
  * \date 2013
+ * \date 2015
  */
 /*
- * Copyright (C) 2011-2013 CESNET
+ * Copyright (C) 2011-2015 CESNET
  *
  * LICENSE TERMS
  *
@@ -67,15 +69,15 @@
 #include "../config.h"
 
 #ifdef WITH_NOTIFICATIONS
-#include "notification_module.h"
+#include "notification_server.h"
 #endif
 
 #include "message_type.h"
-#include "mod_netconf.h"
+#include "netopeerguid.h"
 
 #define SCHEMA_DIR "/tmp/yang_models"
 #define MAX_PROCS 5
-#define SOCKET_FILENAME "/var/run/mod_netconf.sock"
+#define SOCKET_FILENAME "/var/run/netopeerguid.sock"
 #define MAX_SOCKET_CL 10
 #define BUFFER_SIZE 4096
 #define ACTIVITY_CHECK_INTERVAL 10  /**< timeout in seconds, how often activity is checked */
@@ -1273,7 +1275,7 @@
 
         session->ctx = ly_ctx_new(SCHEMA_DIR);
     } else {
-        /* TODO */
+        /* TODO try to load models from a local directory */
         session->ctx = ly_ctx_new(NULL);
     }
 
@@ -1431,6 +1433,8 @@
 static int
 close_and_free_session(struct session_with_mutex *locked_session)
 {
+    int i;
+
     DEBUG("lock private lock.");
     DEBUG("LOCK mutex %s", __func__);
     if (pthread_mutex_lock(&locked_session->lock) != 0) {
@@ -1454,7 +1458,9 @@
     usleep(500000); /* let notification thread stop */
 
     /* session shouldn't be used by now */
-    /** \todo free all notifications from queue */
+    for (i = 0; i < locked_session->notif_count; ++i) {
+        free(locked_session->notifications[i].content);
+    }
     free(locked_session->notifications);
     pthread_mutex_destroy(&locked_session->lock);
     if (locked_session->hello_message != NULL) {
@@ -2023,6 +2029,8 @@
     nc_rpc* rpc = NULL;
     json_object *res = NULL;
 
+    assert(!data || !*data);
+
     /* create requests */
     rpc = nc_rpc_generic(content);
     if (rpc == NULL) {
@@ -2030,11 +2038,6 @@
         return create_error_reply("Internal: Creating rpc request failed");
     }
 
-    if (data != NULL) {
-        // TODO ?free(*data);
-        (*data) = NULL;
-    }
-
     /* get session where send the RPC */
     res = netconf_op(session_key, rpc, data);
     nc_rpc_free (rpc);
@@ -4042,11 +4045,28 @@
 {
     struct sigaction action;
     sigset_t block_mask;
+    int daemonize = 0, i;
 
-    if (argc > 1) {
-        sockname = argv[1];
-    } else {
-        sockname = SOCKET_FILENAME;
+    if (argc > 3) {
+        printf("Usage: [--(h)elp] [--(d)aemon] [socket-path]\n");
+        return 1;
+    }
+
+    sockname = SOCKET_FILENAME;
+    for (i = 1; i < argc; ++i) {
+        if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
+            printf("Usage: [--(h)elp] [--(d)aemon] [socket-path]\n");
+            return 0;
+        } else if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--daemon")) {
+            daemonize = 1;
+        } else {
+            sockname = argv[i];
+        }
+    }
+
+    if (daemonize && (daemon(0, 0) == -1)) {
+        ERROR("daemon() failed (%s)", strerror(errno));
+        return 1;
     }
 
     sigfillset(&block_mask);
diff --git a/src/mod_netconf.h b/src/netopeerguid.h
similarity index 97%
rename from src/mod_netconf.h
rename to src/netopeerguid.h
index f8c90ca..45bca2a 100644
--- a/src/mod_netconf.h
+++ b/src/netopeerguid.h
@@ -3,13 +3,15 @@
  * \brief NETCONF daemon for Netopeer
  * \author Tomas Cejka <cejkat@cesnet.cz>
  * \author Radek Krejci <rkrejci@cesnet.cz>
+ * \author Michal Vasko <mvasko@cesnet.cz>
  * \date 2011
  * \date 2012
  * \date 2013
  * \date 2014
+ * \date 2015
  */
 /*
- * Copyright (C) 2011-2014 CESNET
+ * Copyright (C) 2011-2015 CESNET
  *
  * LICENSE TERMS
  *
diff --git a/src/netopeerguid.rc.in b/src/netopeerguid.rc.in
new file mode 100644
index 0000000..40f76ff
--- /dev/null
+++ b/src/netopeerguid.rc.in
@@ -0,0 +1,102 @@
+#!/bin/sh
+#
+# netopeerguid.rc: netopeerguid init script
+# Copyright (c) 2006-2015 CESNET, z.s.p.o.
+# Author(s): David Kupka <dkupka@cesnet.cz>, Michal Vasko <mvasko@cesnet.cz>
+#
+# This script is written according to SysV init principles.
+#
+# To activate this script at the boot time simple copy this file (and
+# rename it to 'netopeer' - without any suffix) into the /etc/rc.d/init.d
+# directory. Then run chkconfig:
+#
+#    chkconfig --add netopeerguid
+#
+# Output is logged in /var/log/netopeerguid file.
+#
+# Two lines below this clause are used by chkconfig(8) to set it to run
+# in the boot time. The first of those two lines tells chkconfig what
+# runlevels the service should be started in by default, as well as the
+# start and stop priority levels. The second line contains a description
+# for the service.
+#
+# chkconfig: 345 50 80
+# description: netopeerguid startup script
+
+BINDIR="@prefix@/bin/"
+LOGFILE="/var/log/netopeerguid"
+NPGUID=netopeerguid
+
+case "$1" in
+start )
+	echo "== Starting netopeerguid ==" >> $LOGFILE
+	date >> $LOGFILE
+
+	if [ `ps -C netopeerguid | wc -l` -eq 2 ]; then
+		echo "Failed: netopeerguid is already running." >> $LOGFILE;
+		echo "netopeerguid is already running.";
+		exit 1;
+	fi
+
+	$BINDIR$NPGUID -d
+
+	# for netopeerguid to start
+	sleep 1;
+
+	if [ `ps -C netopeerguid | wc -l` -lt 2 ]; then
+		echo "Failure: netopeerguid failed to start." >> $LOGFILE;
+		echo "netopeerguid failed to start.";
+		exit 1;
+	fi
+
+	echo "== netopeerguid start ==";
+	echo "Success: netopeerguid started ==" >> $LOGFILE;
+	;;
+
+stop )
+	echo "== Stopping netopeerguid ==" >> $LOGFILE
+	date >> $LOGFILE
+
+	if [ `ps -C netopeerguid | wc -l` -lt 2 ]; then
+		echo "Failure: netopeerguid is not running." >> $LOGFILE;
+		echo "netopeerguid is not running.";
+		exit 1;
+	fi
+
+	killall -15 netopeerguid
+
+	sleep 2;
+
+	if [ `ps -C netopeerguid | wc -l` -eq 2 ]; then
+		killall -9 netopeerguid
+		sleep 1;
+
+
+		if [ `ps -C netopeerguid | wc -l` -eq 2 ]; then
+			echo "Failure: netopeerguid is still running." >> $LOGFILE;
+			echo "netopeerguid is still running.";
+			exit 1;
+		fi
+	fi
+
+	echo "== netopeerguid stop ==";
+	echo "Success: netopeerguid stopped ==" >> $LOGFILE;
+	;;
+
+restart|reload )
+	$0 stop
+	$0 start
+	;;
+
+status )
+	if [ `ps -C netopeerguid | wc -l` -eq 2 ]; then
+		echo "netopeerguid is running.";
+	else
+		echo "netopeerguid is stopped.";
+	fi
+	;;
+* )
+	# Display usage of this script
+	echo "Usage: $0 {start|stop|restart|reload|status}"
+	;;
+esac
diff --git a/src/notification-server.c b/src/notification_server.c
similarity index 99%
rename from src/notification-server.c
rename to src/notification_server.c
index eaa3d9f..19d8934 100644
--- a/src/notification-server.c
+++ b/src/notification_server.c
@@ -33,8 +33,8 @@
 #include <libnetconf.h>
 #include <libwebsockets.h>
 
-#include "notification_module.h"
-#include "mod_netconf.h"
+#include "notification_server.h"
+#include "netopeerguid.h"
 #include "../config.h"
 
 #ifdef TEST_NOTIFICATION_SERVER
diff --git a/src/notification_module.h b/src/notification_server.h
similarity index 100%
rename from src/notification_module.h
rename to src/notification_server.h
diff --git a/src/test-client.c b/src/test-client.c
index 51543ae..ddc433a 100644
--- a/src/test-client.c
+++ b/src/test-client.c
@@ -57,7 +57,7 @@
 #include <ctype.h>
 #include "message_type.h"
 
-#define SOCKET_FILENAME "/var/run/mod_netconf.sock"
+#define SOCKET_FILENAME "/var/run/netopeerguid.sock"
 #define BUFFER_SIZE 40960
 
 void print_help(char* progname)