From a9175705c1251ae183e1a41eeb32d79b255a9f2b Mon Sep 17 00:00:00 2001
From: Isaac Connor <iconnor@pseudo.connortechnology.com>
Date: Fri, 19 Jun 2015 08:59:49 -0400
Subject: [PATCH] reverse the if statement to reduce indenting

---
 scripts/zmwatch.pl.in | 112 +++++++++++++++++++++---------------------
 1 file changed, 55 insertions(+), 57 deletions(-)

diff --git a/scripts/zmwatch.pl.in b/scripts/zmwatch.pl.in
index 7d5193439..12f3c6987 100644
--- a/scripts/zmwatch.pl.in
+++ b/scripts/zmwatch.pl.in
@@ -85,15 +85,60 @@ while( 1 )
         or Fatal( "Can't execute: ".$sth->errstr() );
     while( my $monitor = $sth->fetchrow_hashref() )
     {
-        if ( $monitor->{Function} ne 'None' )
+        next if $monitor->{Function} eq 'None';
+        my $restart = 0;
+        if ( zmMemVerify( $monitor )
+             && zmMemRead( $monitor, "shared_data:valid" )
+        )
+        {
+            # Check we have got an image recently
+            my $image_time = zmGetLastWriteTime( $monitor );
+            next if ( !defined($image_time) ); # Can't read from shared data
+            next if ( !$image_time ); # We can't get the last capture time so can't be sure it's died.
+
+            my $max_image_delay = ( $monitor->{MaxFPS}
+                                    &&($monitor->{MaxFPS}>0)
+                                    &&($monitor->{MaxFPS}<1)
+                                  ) ? (3/$monitor->{MaxFPS})
+                                    : $Config{ZM_WATCH_MAX_DELAY}
+            ;
+            my $image_delay = $now-$image_time;
+            Debug( "Monitor $monitor->{Id} last captured $image_delay seconds ago, max is $max_image_delay\n" );
+            if ( $image_delay > $max_image_delay )
+            {
+                Info( "Restarting capture daemon for "
+                      .$monitor->{Name}.", time since last capture $image_delay seconds ($now-$image_time)\n"
+                );
+                $restart = 1;
+            }
+        }
+        else
+        {
+            #Info( "Restarting capture daemon for ".$monitor->{Name}.", shared data not valid\n" );
+            #$restart = 1;
+        }
+
+        if ( $restart )
+        {
+            my $command;
+            if ( $monitor->{Type} eq 'Local' )
+            {
+                $command = "zmdc.pl restart zmc -d $monitor->{Device}";
+            }
+            else
+            {
+                $command = "zmdc.pl restart zmc -m $monitor->{Id}";
+            }
+            runCommand( $command );
+        }
+        elsif ( $monitor->{Function} ne 'Monitor' )
         {
-            my $restart = 0;
             if ( zmMemVerify( $monitor )
                  && zmMemRead( $monitor, "shared_data:valid" )
             )
             {
                 # Check we have got an image recently
-                my $image_time = zmGetLastWriteTime( $monitor );
+                my $image_time = zmGetLastReadTime( $monitor );
                 next if ( !defined($image_time) ); # Can't read from shared data
                 next if ( !$image_time ); # We can't get the last capture time so can't be sure it's died.
 
@@ -104,67 +149,20 @@ while( 1 )
                                         : $Config{ZM_WATCH_MAX_DELAY}
                 ;
                 my $image_delay = $now-$image_time;
-                Debug( "Monitor $monitor->{Id} last captured $image_delay seconds ago, max is $max_image_delay\n" );
+                Debug( "Monitor $monitor->{Id} last analysed $image_delay seconds ago, max is $max_image_delay\n" );
                 if ( $image_delay > $max_image_delay )
                 {
-                    Info( "Restarting capture daemon for "
-                          .$monitor->{Name}.", time since last capture $image_delay seconds ($now-$image_time)\n"
+                    Info( "Restarting analysis daemon for "
+                          .$monitor->{Name}.", time since last analysis $image_delay seconds ($now-$image_time)\n"
                     );
-                    $restart = 1;
-                }
-            }
-            else
-            {
-                #Info( "Restarting capture daemon for ".$monitor->{Name}.", shared data not valid\n" );
-                #$restart = 1;
-            }
-
-            if ( $restart )
-            {
-                my $command;
-                if ( $monitor->{Type} eq 'Local' )
-                {
-                    $command = "zmdc.pl restart zmc -d $monitor->{Device}";
-                }
-                else
-                {
-                    $command = "zmdc.pl restart zmc -m $monitor->{Id}";
-                }
-                runCommand( $command );
-            }
-            elsif ( $monitor->{Function} ne 'Monitor' )
-            {
-                if ( zmMemVerify( $monitor )
-                     && zmMemRead( $monitor, "shared_data:valid" )
-                )
-                {
-                    # Check we have got an image recently
-                    my $image_time = zmGetLastReadTime( $monitor );
-                    next if ( !defined($image_time) ); # Can't read from shared data
-                    next if ( !$image_time ); # We can't get the last capture time so can't be sure it's died.
-
-                    my $max_image_delay = ( $monitor->{MaxFPS}
-                                            &&($monitor->{MaxFPS}>0)
-                                            &&($monitor->{MaxFPS}<1)
-                                          ) ? (3/$monitor->{MaxFPS})
-                                            : $Config{ZM_WATCH_MAX_DELAY}
-                    ;
-                    my $image_delay = $now-$image_time;
-                    Debug( "Monitor $monitor->{Id} last analysed $image_delay seconds ago, max is $max_image_delay\n" );
-                    if ( $image_delay > $max_image_delay )
-                    {
-                        Info( "Restarting analysis daemon for "
-                              .$monitor->{Name}.", time since last analysis $image_delay seconds ($now-$image_time)\n"
-                        );
-                        my $command = "zmdc.pl restart zma -m ".$monitor->{Id};
-                        runCommand( $command );
-                    }
+                    my $command = "zmdc.pl restart zma -m ".$monitor->{Id};
+                    runCommand( $command );
                 }
             }
         }
-        # Prevent open handles building up if we have connect to shared memory
-        zmMemInvalidate( $monitor );
     }
+    # Prevent open handles building up if we have connect to shared memory
+    zmMemInvalidate( $monitor );
     sleep( $Config{ZM_WATCH_CHECK_INTERVAL} );
 }
 Info( "Watchdog exiting\n" );