--- beautify.module.orig	2009-01-13 02:15:11.000000000 -0800
+++ beautify.module	2009-01-14 10:44:53.000000000 -0800
@@ -370,7 +370,7 @@ function beautify_set_cache($input) {
  *   the tidied string
  */
 function beautify_htmltidy_command($input, &$errors, &$warnings) {
-  $path = variable_get('beautify_htmltidy_path', '/usr/bin/tidy');
+  $path = variable_get('beautify_htmltidy_path', drupal_get_path('module', 'beautify') . '/bin/tidy' . (strpos(PHP_OS, 'WIN' === 0) ? '.exe' : ''));
   if (!file_exists($path)) {
     $message = t("Couldn't find the Tidy binary at '%path', not using tidy.", array('%path' => $path));
     watchdog('beautify', $message, WATCHDOG_WARNING);
@@ -466,7 +466,7 @@ function beautify_htmltidy_command($inpu
 }
 
 function beautify_htmltidy_run($input, $args, &$output, &$errors, &$warnings) {
-  $tidypath = variable_get('beautify_htmltidy_path', '/usr/bin/tidy');
+  $tidypath = variable_get('beautify_htmltidy_path', drupal_get_path('module', 'beautify') . '/bin/tidy' . (strpos(PHP_OS, 'WIN' === 0) ? '.exe' : '');
   if (!file_exists($tidypath)) {
     watchdog('beautify', 'Failed to find HTML Tidy executable at %beautify_htmltidy_path, not using tidy', array('%beautify_htmltidy_path' => $tidypath), WATCHDOG_WARNING);
     $output = '';
@@ -531,54 +531,45 @@ function beautify_htmltidy_run($input, $
  *   TRUE if found,
  *   FALSE if error.
  */
-function beautify_htmltidy_test(&$message = '', &$version = '') {
-  $path = variable_get('beautify_htmltidy_path', '/usr/bin/tidy');
-  if (!file_exists($path)) {
-    $pattern = '@\\\\+@'; // one or more backslashes
-    // Windows paths
-    if (substr(PHP_OS, 0, 3) == 'WIN') {
-      $possible_paths = array(
-        preg_replace($pattern, '/', dirname(__FILE__)) .'/bin/tidy.exe'
-      );
-    }
-    // Unix paths
-    else {
-      $possible_paths = array(
-        '/bin/tidy',
-        '/usr/bin/tidy',
-        '/usr/local/bin/tidy',
-        preg_replace($pattern, '/', dirname(__FILE__)) .'/bin/tidy',
-      );
-    }
-
-    $message = t('Searching for HTML Tidy in:');
-    $message .= '<ul>';
-    foreach ($possible_paths as $path) {
-      $message .= '<li>'. $path .'</li>';
-      if (file_exists($path)) {
-        break;
-      }
-    }
-    $message .= '</ul>';
 
-    if (!file_exists($path)) {
-      $message .= t('Could not find HTML Tidy binary.');
-      return FALSE;
-    }
-    variable_set('beautify_htmltidy_path', $path);
-  }
-
-  // Test the HTML Tidy binary by running a shell command to return the version.
-  $command = escapeshellcmd($path .' -v');
-  if (exec($command, $response)) {
-    $version = $response[0];
-    return TRUE;
-  }
-  else {
-    $message .= t('Found an HTML Tidy binary but it didn\'t seem to run properly. !command failed to respond correctly.', 
-                  array('!command' => $command));
-    return FALSE;
-  }
+function beautify_htmltidy_test(&$message = '', &$version = '') {
+	$fail = strpos(PHP_OS, 'WIN') === 0;
+	$sub = drupal_get_path('module', 'beautify') . '/bin/tidy' . ($fail ? '.exe' : '');
+	$path = variable_get('beautify_htmltidy_path', $sub);
+	$test = FALSE;
+	if (!@file_exists($path)) {
+		// Test for binary in subdirectory first
+		if (@file_exists($sub)) {
+			$new_path = $sub;
+		}
+		elseif (!$fail) {
+			// Not on Windows, so let's try which
+			$which = shell_exec('which tidy');
+			if (strpos($which, '/') === 0) {
+				$new_path = $which;
+			}
+		}
+		if (isset($new_path)) {
+			variable_set('beautify_htmltidy_path', $new_path);
+			$path = $new_path;
+			$test = TRUE;
+		}
+		else {	
+			$message .= t('Could not find HTML Tidy binary.');
+		}
+	}
+	if ($test) {
+	  $command = escapeshellcmd($path .' -v');
+	  if (exec($command, $response)) {
+	    $version = $response[0];
+	    return TRUE;
+	  }
+	  else {
+	    $message .= t('Found an HTML Tidy binary but it didn\'t seem to run properly. !command failed to respond correctly.', 
+	                  array('!command' => $command));
+	    return FALSE;
+	  }
+	}
 }
 
 /**
