#include <windows.h>

void ReadFirstWord (LPTSTR pszOut, LPTSTR pszIn)
{
   while (*pszIn == ' ')
      ++pszIn;

   if (*pszIn == '\"')
   {
      *pszOut++ = *pszIn++;
      while (*pszIn && (*pszIn != '\"'))
         *pszOut++ = *pszIn++;
      *pszOut++ = '\"';
   }
   else
   {
      while (*pszIn && (*pszIn != ' '))
         *pszOut++ = *pszIn++;
   }
   *pszOut = 0;
}

void RunServer (LPSTR psz)
{
   TCHAR szModule[1024];
   ReadFirstWord (szModule, psz);

   STARTUPINFO si;
   memset (&si, 0x00, sizeof(si));
   si.cb = sizeof(si);
   si.wShowWindow = SW_HIDE;
   si.dwFlags = STARTF_USESHOWWINDOW;

   PROCESS_INFORMATION pi;
   memset (&pi, 0x00, sizeof(pi));

   CreateProcess (szModule, psz, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
   WaitForSingleObject (pi.hProcess, INFINITE);
   CloseHandle (pi.hThread);
   CloseHandle (pi.hProcess);
}

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pLineIn, int nCmdShow)
{
   HANDLE hm = CreateMutex (NULL, TRUE, TEXT("svnservd mutex"));
   if ((hm != NULL) && (hm != INVALID_HANDLE_VALUE) && (GetLastError() != ERROR_ALREADY_EXISTS))
   {
      RunServer (pLineIn);
      CloseHandle (hm);
   }
   return 0;
}



