--- /tmp/pg_regress.c	2006-07-14 22:15:30.000000000 +0200
+++ pg_regress.c	2006-07-15 00:40:52.000000000 +0200
@@ -15,6 +15,9 @@
 #define INVALID_PID -1
 #endif
 
+
+#define DEFAULT_TEMP "./tmp_check"
+
 #define header(t, ...) do { char tmp[128];sprintf(tmp, t, ##__VA_ARGS__);printf("============== %-38s ==============\n",tmp); fflush(stdout); } while (0); 
 #define status(a, ...) do { printf(a, ##__VA_ARGS__); fflush(stdout); if(logfile) fprintf(logfile,a,##__VA_ARGS__); } while (0);
 #define status_end() do { printf("\n"); fflush(stdout); if (logfile) fprintf(logfile,"\n"); } while (0);
@@ -29,6 +32,9 @@
 
 static char *libdir = NULL;
 static char *bindir = "";
+static char *temp_install_bindir = "";
+static char *datadir = "";
+static char *temp_install_datadir = "";
 static char *inputdir = ".";
 static char *outputdir = ".";
 static char *host_platform = NULL;
@@ -175,6 +181,19 @@
 	putenv(newval);
 }
 
+static bool
+regress_exec_cmd(const char *cmd)
+{
+	int status;
+
+	if (debug)
+		printf("executing: %s\n", cmd);
+
+	status = WEXITSTATUS(system(cmd));
+
+	return status == 0;
+}
+
 static void
 initialize_environment(void)
 {
@@ -204,12 +223,23 @@
 	if (temp_install)
 	{
 		/* setup bin and lib dirs for temp install */
-		bindir = malloc(strlen(temp_install) + 32);
-		sprintf(bindir,"%s/install/usr/local/pgsql/bin/", temp_install);
+		temp_install_bindir = malloc(strlen(temp_install)
+									 + strlen("/install/")
+									 + strlen(bindir)
+									 + 1);
+		sprintf(temp_install_bindir, "%s/install/%s", temp_install, bindir);
+
+
+		temp_install_datadir = malloc(strlen(temp_install)
+									  + strlen("/install/")
+									  + strlen(datadir)
+									  + 1);
+		sprintf(temp_install_datadir, "%s/install/%s", temp_install, datadir);
+
+
 		libdir = malloc(strlen(temp_install) + 32);
 		sprintf(libdir,"%s/install/usr/local/pgsql/lib", temp_install);
 
-
 		add_to_path("LD_LIBRARY_PATH", ':', libdir);
 		add_to_path("DYLD_LIBRARY_PATH", ':', libdir);
 #ifdef WIN32
@@ -244,7 +274,8 @@
 	vsprintf(query2, query, args);
 	va_end(args);
 
-	sprintf(psql_cmd, "%spsql -X -c \"%s\" %s", bindir, query2, database);
+	sprintf(psql_cmd, "%s/psql -X -c \"%s\" %s", temp_install_bindir,
+												query2, database);
 
 	p = popen(psql_cmd, "r");
 	if (!p)
@@ -319,8 +350,8 @@
 	sprintf(infile, "%s/sql/%s.sql", inputdir, basefile);
 	sprintf(outfile, "%s/results/%s.out", outputdir, basefile);
 
-	sprintf(psql_cmd, "%spsql -X -a -q -d %s <%s >%s 2>&1",
-		bindir, dbname, infile, outfile);
+	sprintf(psql_cmd, "%s/psql -X -a -q -d %s <%s >%s 2>&1",
+			temp_install_bindir, dbname, infile, outfile);
 
 	return spawn_process(psql_cmd);
 }
@@ -625,7 +656,8 @@
 	fclose(scf);
 	if (temp_install)
 	{
-		sprintf(scbuf, "\"%spg_ctl\" -D \"%s/data\" stop", bindir, temp_install);
+		sprintf(scbuf, "\"%s/pg_ctl\" -D \"%s/data\" stop",
+					   temp_install_bindir, temp_install);
 		system(scbuf);
 	}
 
@@ -712,7 +744,7 @@
 		{"multibyte", required_argument, NULL, 6},
 		{"outputdir", required_argument, NULL, 7},
 		{"schedule", required_argument, NULL, 8},
-		{"temp-install", required_argument, NULL, 9},
+		{"temp-install", optional_argument, NULL, 9},
 		{"no-locale", no_argument, NULL, 10},
 		{"top-builddir", required_argument, NULL, 11},
 		{"temp-port", required_argument, NULL, 12},
@@ -721,6 +753,7 @@
 		{"user", required_argument, NULL, 15},
 		{"platform", required_argument, NULL, 16},
 		{"bindir", required_argument, NULL, 17},
+		{"datadir", required_argument, NULL, 18},
 		{NULL, 0, NULL, 0}
 	};
 
@@ -765,6 +798,9 @@
 				schedule = strdup(optarg);
 				break;
 			case 9:
+				if (!optarg)
+					/* no temp_install path specified */
+					optarg = DEFAULT_TEMP;
 				if (optarg[0] == '/')
 					temp_install = strdup(optarg);
 				else
@@ -804,6 +840,9 @@
 			case 17:
 				bindir = strdup(optarg);
 				break;
+			case 18:
+				datadir = strdup(optarg);
+				break;
 			default:
 				/* getopt_long already emitted a complaint */
 				fprintf(stderr,"\nTry \"%s -h\" for more information.\n",argv[0]);
@@ -846,33 +885,35 @@
 
 		header("creating temporary installation");
 		sprintf(buf,"make -C \"%s\" DESTDIR=\"%s/install\" install with_perl=no with_python=no > \"%s/log/install.log\" 2>&1",
-			top_builddir, temp_install, outputdir);
-		if (WEXITSTATUS(system(buf)) != 0)
+			top_builddir, temp_install, temp_install);
+		if (!regress_exec_cmd(buf))
 		{
-			fprintf(stderr, "installation failed.\nExamine %s/log/install.log for the reason.\n", outputdir);
+			fprintf(stderr, "installation failed.\nExamine %s/log/install.log for the reason.\n", temp_install);
 			exit(2);
 		}
 
 		header("initializing database system");
-		sprintf(buf,"\"%sinitdb\" -D \"%s/data\" -L \"%s/install/usr/local/pgsql/share\" --noclean %s %s > %s/log/initdb.log 2>&1",
-			bindir, temp_install, temp_install, nolocale?"--no-locale":"", debug?"--debug":"", outputdir);
-		if (WEXITSTATUS(system(buf)) != 0)
+		sprintf(buf,"\"%s/initdb\" -D \"%s/data\" -L \"%s\" --noclean %s %s > %s/log/initdb.log 2>&1",
+			temp_install_bindir, temp_install, temp_install_datadir,
+			nolocale?"--no-locale":"", debug?"--debug":"", temp_install);
+		if (!regress_exec_cmd(buf) != 0)
 		{
-			fprintf(stderr,"initdb failed.\nExamine %s/log/initdb.log for the reason.\n", outputdir);
+			fprintf(stderr,"initdb failed.\nExamine %s/log/initdb.log for the reason.\n", temp_install);
 			exit(2);
 		}
 
 		header("starting postmaster");
-		sprintf(buf,"\"%spostmaster\" -D \"%s/data\" -F %s -c listen_addresses=%s >\"%s/log/postmaster.log\" 2>&1",
-			bindir, temp_install, debug?"-d5":"", hostname?hostname:"", outputdir);
+		sprintf(buf,"\"%s/postmaster\" -D \"%s/data\" -F %s -c listen_addresses=%s >\"%s/log/postmaster.log\" 2>&1",
+			temp_install_bindir, temp_install,
+			debug?"-d5":"", hostname?hostname:"", temp_install);
 		if (spawn_process(buf) == INVALID_PID)
 		{
-			fprintf(stderr,"postmaster failed.\nExamine %s/log/postmaster.log for the reason\n", outputdir);
+			fprintf(stderr,"postmaster failed.\nExamine %s/log/postmaster.log for the reason\n", temp_install);
 			exit(2);
 		}
 		
-		sprintf(buf,"\"%spsql\" -X -c \"SELECT 1;\" postgres >%s 2>&1",
-			bindir, DEVNULL);
+		sprintf(buf,"\"%s/psql\" -X -c \"SELECT 1;\" postgres >%s 2>&1",
+			temp_install_bindir, DEVNULL);
 		for (i = 0; i < 60; i++)
 		{
 			int r = system(buf);
