$OpenBSD: patch-kde-dev-scripts_colorsvn,v 1.2 2013/07/05 14:32:50 zhuk Exp $
Cleanup development scripts, making them more portable and secure.
Upstream review 110858: http://git.reviewboard.kde.org/r/110858/
--- kde-dev-scripts/colorsvn.orig	Fri Jun 28 22:03:13 2013
+++ kde-dev-scripts/colorsvn	Fri Jul  5 18:14:32 2013
@@ -51,10 +51,11 @@
 
 use Term::ANSIColor;
 use IPC::Open3;
+use File::Spec::Functions qw/catfile file_name_is_absolute path/;
 
 sub initDefaults
 {
-	$svnPath = "/usr/bin/svn";
+	$svnCmd = "svn";
 
 	$nocolor{"dumb"} = "true";
 
@@ -97,7 +98,7 @@ sub loadPreferences
 
 		if ($option =~ /svn/)
 		{
-			$svnPath = $value;
+			$svnCmd = $value;
 		}
 		elsif ($option eq "nocolor")
 		{
@@ -156,16 +157,35 @@ foreach (@ARGV)
 # we're writing to something that's not a tty, don't do color.
 if (! -t STDOUT || $commit == 1 || $nocolor{$terminal})
 {
-	exec $svnPath, @ARGV
-		or die("Couldn't exec");
+	exec $svnCmd, @ARGV;
+	die("Couldn't exec");
 }
 
--f $svnPath or die ("$svnPath not found, add svn=/full/path/to/svn to ~/.colorcvsrc");
+sub svn_not_found() {
+	die ("$svnCmd not found, add svn=/full/path/to/svn to ~/.colorcvsrc");
+}
 
+# Check if we have SVN binary accessible. Of course, there could
+# be a race, but we don't care - all we want is to print out
+# nice error if executable SVN binary could not be found.
+if (file_name_is_absolute($svnCmd)) {
+	svn_not_found unless -f $svnCmd and -x $svnCmd;
+} else {
+	my $found = 0;
+	foreach (path()) {
+		my $path = catfile($_, $svnCmd);
+		if (-f $path and -x $path) {
+			$found = 1;
+			last;
+		}
+	}
+	svn_not_found unless $found;
+}
+
 # Keep the pid of the svn process so we can get its return
 # code and use that as our return code.
-$svn_pid = open3('<&STDIN', \*SVNOUT, \*SVNOUT, $svnPath, @ARGV);
-$svnName = $svnPath;
+$svn_pid = open3('<&STDIN', \*SVNOUT, \*SVNOUT, $svnCmd, @ARGV);
+$svnName = $svnCmd;
 $svnName =~ s,.*/(.*)$,\1,;
 
 # Colorize the output from the svn program.
