$NetBSD$

--- res/res_monitor.c.orig	2008-11-18 16:45:50.000000000 +0300
+++ res/res_monitor.c
@@ -57,7 +57,7 @@
 static void *monitor_app;
 static const char *monitor_name = "Monitor";
 static const char *monitor_synopsis = "Monitor a channel";
-static const char *monitor_syntax = "Monitor([file_format[:urlbase]][, [fname_base][, [options]]])";
+static const char *monitor_syntax = "Monitor([file_format[:urlbase]][, [fname_base][, [options][, [syscmd]]]])";
 static const char *monitor_descrip =
 "Used to start monitoring a channel. The channel's input and output\n"
 "voice packets are logged to files until the channel hangs up or\n"
@@ -78,6 +78,7 @@
 "          administrator interface\n"
 "\n"
 "    b   - Don't begin recording unless a call is bridged to another channel\n"
+"  syscmd		optional, if set, monitor will execute system command after recording.\n"
 "\nReturns -1 if monitor files can't be opened or if the channel is already\n"
 "monitored, otherwise 0.\n"
 ;
@@ -230,6 +231,7 @@
 {
 	char *execute;
 	char *execute_args;
+	char *syscmdfl;
     int explicit_file_type;
 	int delfiles = 0;
 
@@ -321,6 +323,14 @@
 				cw_log(LOG_WARNING, "Execute of %s failed.\n",tmp);
 		}
 		
+			syscmdfl = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR_SYSCMD");
+			if (syscmdfl != NULL && syscmdfl[0] != '\0')
+			{
+				cw_log(LOG_DEBUG,"monitor executing %s\n",syscmdfl);
+				if (cw_safe_system(syscmdfl) == -1)
+					cw_log(LOG_WARNING, "Execute of %s failed.\n",syscmdfl);
+			}
+		
 		free(chan->monitor->format);
 		free(chan->monitor);
 		chan->monitor = NULL;
@@ -375,6 +385,8 @@
 	int waitforbridge = 0;
 	int res = 0;
 
+	pbx_builtin_setvar_helper(chan, "AUTO_MONITOR_SYSCMD", (argc > 3 ? argv[3] : ""));
+
 	if (argc > 2) {
 		for (; argv[2][0]; argv[2]++) {
 			switch (argv[2][0]) {
