#!/usr/bin/perl
#  $Id: nph_sighup 6458 2006-11-20 00:55:13Z teetov $
# 
# 
# Purpose:  send SIGHUP to cam_mretr & multiple nph-mjpg to check 
#           fps_advance file
# Usage:    nph_sighup <devid>
#
# Env:      $APL_CONF has to be set
#           'ps' has to be in $PATH
#           runs /usr/bin/sudo
#           sudo has to be configured to start $APL/cam/bin/cam_sighup by apache
#
# Note:   ! No Error handling !
#         ! Script is started by apache and use sudo to start cam_sighup
#         ! use /tmp to find nph-mjpg.<pid>.<camid> files
#         ! use 'apl' as a unix name of aplication user
#         ! if sudo is not corretly configured then leaves zomby !
#          
#
# -----------------------------------------------------------------------------
#  Author:      Alex'A Titov
#  Edited by: 
#  Reviewed:
#  QA by:  
#  Copyright:   videoNEXT LLC
# -----------------------------------------------------------------------------
use strict;

my $TMP='/tmp';
my $APLUSR='apl';
#--------------------------------- parse command line
my $name='nph_sighup';
if(! ($ARGV[0] =~ m/^(\d+)$/)) {  # camid argument expected
  print "\n\nUsage:\t$name <camid>\n\n";
  exit 1;
}
my $id=$1+0;                      # remove leading zeros if exist

#--------------------------------- Find all nph-mjpg
opendir(PDIR,"/tmp") or die;
my @slist=map {/^nph-mjpg\.(\d+)\.0*$id/} readdir(PDIR);
closedir PDIR;
#--------------------------------- send SIGHUP
if (@slist) {                     # only if any proc is found
    unshift @slist,1;             # push SIGHUP 
    kill @slist;
}
#--------------------------------- call cam_sighup

alarm 1;                          # want to die if sudo hangs
system("sudo -u $APLUSR $ENV{APL}/cam/bin/cam_sighup $id");
