--- pgAgent-NG/unix.cpp	2013-09-11 14:02:26.343455661 +0200
+++ pgagent/unix.cpp	2013-09-11 12:02:08.414191462 +0200
@@ -14,139 +14,69 @@
 // *nix only!!
 #ifndef WIN32
 
+#include <wx/filename.h>
+#include <wx/ffile.h>
 #include <fcntl.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <time.h>
-#include <sys/stat.h>
 
-std::string CreateTempFileName(std::string prefix)
+void usage(const wxString &executable)
 {
-    std::ostringstream oss;
-    FILE * pFile;
+	wxFileName *fn = new wxFileName(executable);
 
-    char * pointer = tmpnam (NULL);
-    std::string temp(pointer);
-    std::size_t pos = temp.find_last_of('/');
-
-    oss << temp.substr(0, pos+1) << prefix << temp.substr(pos+1, temp.size()-pos-1);
-
-    LogMessage("TEMP: " + temp + " - " + oss.str(),LOG_DEBUG);
-    temp = oss.str();
-    pFile = fopen (temp.c_str(),"w");
-
-    if (pFile!=NULL) fclose(pFile);
-    else return std::string("");
-    return temp;
+	wxPrintf(_("Usage:\n"));
+	wxPrintf(fn->GetName() + _(" [options] <connect-string>\n"));
+	wxPrintf(_("options:\n"));
+	wxPrintf(_("-f run in the foreground (do not detach from the terminal)\n"));
+	wxPrintf(_("-t <poll time interval in seconds (default 10)>\n"));
+	wxPrintf(_("-r <retry period after connection abort in seconds (>=10, default 30)>\n"));
+	wxPrintf(_("-s <log file (messages are logged to STDOUT if not specified>\n"));
+	wxPrintf(_("-l <logging verbosity (ERROR=0, WARNING=1, DEBUG=2, default 0)>\n"));
 }
 
-int Mkdir(std::string dirname, mode_t rights)
+void LogMessage(wxString msg, int level)
 {
-    return mkdir(dirname.c_str(), rights);
-}
-
-int RemoveFile(std::string filename)
-{
-    return remove(filename.c_str());
-}
-
-/*
-int path_is_directory (const char* path)
-{
-    struct stat s_buf;
-
-    if (stat(path, &s_buf))
-        return 0;
-
-    return S_ISDIR(s_buf.st_mode);
-}
-*/
-int Rmdir(std::string dirname)
-{
-/*        DIR*            dp;
-        struct dirent*  ep;
-        char            p_buf[512] = {0};
-
-        dp = opendir(directory_name);
-
-        while ((ep = readdir(dp)) != NULL) {
-            sprintf(p_buf, "%s/%s", directory_name, ep->d_name);
-            if (path_is_directory(p_buf))
-                delete_folder_tree(p_buf);
-            else
-                unlink(p_buf);
-        }
-
-        closedir(dp);
-        rmdir(directory_name);
-*/
-    return rmdir(dirname.c_str());
-}
-
-int Chmod(std::string dirname, mode_t rights)
-{
-    return chmod(dirname.c_str(), rights);
-}
-
-void usage(const std::string &executable)
-{
-    std::cout << "Usage:\n" << std::endl;
-    std::cout << executable << " [options] <connect-string>\n"<< std::endl;
-    std::cout << "options:\n"<< std::endl;
-    std::cout << "-f run in the foreground (do not detach from the terminal)\n"<< std::endl;
-    std::cout << "-t <poll time interval in seconds (default 10)>\n"<< std::endl;
-    std::cout << "-r <retry period after connection abort in seconds (>=10, default 30)>\n"<< std::endl;
-    std::cout << "-s <log file (messages are logged to STDOUT if not specified>\n"<< std::endl;
-    std::cout << "-l <logging verbosity (ERROR=0, WARNING=1, DEBUG=2, default 0)>\n"<< std::endl;
-}
-
-//TODO - PHASE2 - Replace Func to OFSTREAMS like LOG / LOG_DEBUG / LOG_ERROR << MSG << std::endl;
-void LogMessage(std::string msg, int level)
-{
-    std::streambuf * buf;
-    std::ofstream of;
-
-    time_t rawtime;
-    struct tm * timeinfo;
-    char buffer [80];
-
-    time (&rawtime);
-    timeinfo = localtime (&rawtime);
-
-    strftime (buffer,80,"%F %H:%M:%S",timeinfo);
-
-    if(!logFile.empty()) {
-        of.open(logFile, std::ios::out | std::ios::app);
-        if(of.is_open()) {
-            buf = of.rdbuf();
-        } else {
-            std::cerr << "Can not open the logfile " << logFile << "! Log output goes to STDOUT" << std::flush << std::endl;
-            buf = std::cout.rdbuf();
-        }
-    } else {
-        buf = std::cout.rdbuf();
-    }
+	wxFFile file;
+	if (logFile.IsEmpty())
+	{
+		file.Attach(stdout);
+	}
+	else
+	{
+		file.Open(logFile.c_str(), wxT("a"));
+	}
 
-    std::ostream log(buf);
+	if (!file.IsOpened())
+	{
+		wxFprintf(stderr, _("Can not open the logfile!"));
+		return;
+	}
 
 	switch (level)
 	{
 		case LOG_DEBUG:
 			if (minLogLevel >= LOG_DEBUG)
-                log << buffer << "|DEBUG|" << msg  << std::flush << std::endl;
+				file.Write(_("DEBUG: ") + msg + wxT("\n"));
 			break;
 		case LOG_WARNING:
 			if (minLogLevel >= LOG_WARNING)
-                log << buffer << "|WARNING|" << msg  << std::flush << std::endl;
+				file.Write(_("WARNING: ") + msg + wxT("\n"));
 			break;
 		case LOG_ERROR:
-            log << buffer << "|ERROR|" << msg  << std::flush << std::endl;
+			file.Write(_("ERROR: ") + msg + wxT("\n"));
 			exit(1);
 			break;
 		case LOG_STARTUP:
-            log << buffer << "|WARNING|" << msg  << std::flush << std::endl;
+			file.Write(_("WARNING: ") + msg + wxT("\n"));
 			break;
 	}
+
+	if (logFile.IsEmpty())
+	{
+		file.Detach();
+	}
+	else
+	{
+		file.Close();
+	}
 }
 
 // Shamelessly lifted from pg_autovacuum...
@@ -157,7 +87,7 @@
 	pid = fork();
 	if (pid == (pid_t) - 1)
 	{
-        LogMessage(("Cannot disassociate from controlling TTY"), LOG_ERROR);
+		LogMessage(_("Cannot disassociate from controlling TTY"), LOG_ERROR);
 		exit(1);
 	}
 	else if (pid)
@@ -166,7 +96,7 @@
 #ifdef HAVE_SETSID
 	if (setsid() < 0)
 	{
-        LogMessage(("Cannot disassociate from controlling TTY"), LOG_ERROR);
+		LogMessage(_("Cannot disassociate from controlling TTY"), LOG_ERROR);
 		exit(1);
 	}
 #endif
@@ -176,25 +106,27 @@
 int main(int argc, char **argv)
 {
 	// Statup wx
+	wxInitialize();
+
+	wxString executable;
+	executable = wxString::FromAscii(argv[0]);
 
 	if (argc < 2)
 	{
-        usage(std::string(argv[0]));
+		usage(executable);
 		return 1;
 	}
 
 	argc--;
 	argv++;
 
-    connInfo* serviceConnInfo = new connInfo();
-    setOptions(argc, argv, serviceConnInfo);
+	setOptions(argc, argv, executable);
 
-    if (!runInForeground) daemonize();
+	if (!runInForeground)
+		daemonize();
 
-    LogMessage( std::string("MainLOOP"), LOG_DEBUG);
-    MainLoop(serviceConnInfo);
+	MainLoop();
 
-    delete serviceConnInfo;
 	return 0;
 }
 
