$NetBSD$

Handle discovery/checking of external commands uniformly

--- fgallery.orig	2015-10-11 11:16:27.000000000 +0200
+++ fgallery	2015-10-11 11:11:52.000000000 +0200
@@ -233,6 +233,12 @@
   return int($value);
 }
 
+# see if our environment has a given command installed
+sub cmd_exists
+{
+  my ($c) = @_;
+  return qx{/bin/sh -c "command -v $c"};
+}
 
 sub print_help()
 {
@@ -299,41 +305,32 @@
   fatal("output directory already exists, but doesn't look like a template copy");
 }
 
-# check tools
-if(system("identify -version >/dev/null 2>&1")
-|| system("convert -version >/dev/null 2>&1")) {
-  fatal("cannot run \"identify\" or \"convert\" (check if ImageMagick is installed)");
-}
-if(system("7za -h >/dev/null 2>&1"))
-{
+# check for existence of external commands
+unless(cmd_exists('7z')) {
   $p7zip = 0;
-  if(system("zip -h >/dev/null 2>&1")) {
-    fatal("cannot run \"zip\" (check if 7za or zip is installed)");
-  }
-}
-if(system("jpegoptim -V >/dev/null 2>&1")) {
-  $jpegoptim = 0;
+  cmd_exists('zip') || fatal('Missing 7z or zip command');
 }
-if(system("pngcrush -h >/dev/null 2>&1")) {
-  $pngoptim = 0;
-}
-if($facedet && system("facedetect -h >/dev/null 2>&1")) {
-  fatal("cannot run \"facedetect\" (see http://www.thregr.org/~wavexx/hacks/facedetect/)");
-}
-if($sRGB && system("tificc >/dev/null 2>&1")) {
-  fatal("cannot run \"tificc\" (check if liblcms2-utils is installed)");
-}
-
+fatal 'Missing identify or convert executable (from ImageMagick)'
+  unless cmd_exists('identify') || cmd_exists('convert');
+$jpegoptim = 0 unless cmd_exists('jpegoptim');
+$pngoptim = 0 unless cmd_exists('pngcrush');
+fatal 'Missing facedetect (see http://www.thregr.org/~wavexx/hacks/facedetect/'
+  if $facedet && !cmd_exists('facedetect');
+fatal 'Missing tificc executable (from lcms2 library)'
+  if $sRGB && !cmd_exists('tificc');
 my $exiftrancmd;
-if($orient)
+while($orient)
 {
-  if(!system("exiftran -h >/dev/null 2>&1")) {
+  if(cmd_exists('exiftran')) {
     $exiftrancmd = "exiftran -aip";
-  } elsif(!system("exifautotran >/dev/null 2>&1")) {
+    last;
+  }
+  if(cmd_exists('exifautotran')) {
     $exiftrancmd = "exifautotran";
-  } else {
-    fatal("cannot execute exiftran or exifautotran for lossless JPEG autorotation");
+    last;
   }
+  fatal 'Missing exiftran or exifautotran executable for JPEG autorotation'
+    unless $exiftrancmd;
 }
 
 # list available files
