#ifdef MEWEL
#include "window.h"
#define MEMHANDLE    BYTE *
#else
#include <windows.h>
#define MEMHANDLE    HANDLE
#endif

#include <fcntl.h>
#include <io.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <string.h>
#include "pcmove.h"

#define COPYBUFSIZE  1024

char   szBackupDir[128];
char   szBackupFile[128];


BOOL PASCAL CopyToInbox(HWND hWnd, PSTR pSrcFile, PSTR pDestFile)
{
  int     rc;
  OFSTRUCT OFSrc, OFDest;
  int     fdSrc, fdDest;
  char    szActivity[256];
  char    *pDot;
  DWORD   dwTotal;

#if 0
  /*
    TEST
  */
  MessageBox(hWnd, pSrcFile,  "Source", MB_OK);
  MessageBox(hWnd, pDestFile, "Dest", MB_OK);
  return TRUE;
#endif

  /*
    1) Open the source file
  */
#ifdef MEWEL
  fdSrc = open(pSrcFile, O_RDONLY);
#else
  fdSrc = OpenFile(pSrcFile, &OFSrc, OF_READ);
#endif
  if (fdSrc < 0)
  {
    sprintf(szActivity, "Couldn't open the source file %s [Code %d]", 
                         pSrcFile, OFSrc.nErrCode);
    MessageBox(hWnd, szActivity, "Open Error", MB_OK);
    return FALSE;
  }

  /*
    2) Create the destination file
  */
#ifdef MEWEL
  fdDest = open(pDestFile, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,S_IREAD|S_IWRITE);
#else
  fdDest = OpenFile(pDestFile, &OFDest, OF_CREATE | OF_WRITE);
#endif
  if (fdDest < 0)
  {
    sprintf(szActivity, "Couldn't open the destination file %s [Code %d]", 
                         pDestFile, OFDest.nErrCode);
    MessageBox(hWnd, szActivity, "Open Error", MB_OK);
    _lclose(fdSrc);
    return FALSE;
  }

  /*
    3) Do the copy
  */
  dwTotal = _Copy(hWnd, pSrcFile, pDestFile, fdSrc, fdDest);

  /*
    4) Rename the source file to a BAK file
  */
  strcpy(szBackupFile, pSrcFile);
  if ((pDot = strrchr(szBackupFile, '.')) != NULL)
    *pDot = '\0';
  strcat(szBackupFile, ".BAK");
  rename(pSrcFile, szBackupFile);

  /*
    5) Write the activity string to the logfile
  */
  WriteLogfile(pSrcFile, pDestFile, dwTotal);

  return TRUE;
}



BOOL PASCAL CopyFromInbox(HWND hWnd, PSTR pSrcFile, PSTR pDestFile)
{
  int    rc;
  int    fdSrc, fdDest;
  char   szActivity[256];
  OFSTRUCT OFSrc, OFDest;
  char   *pDot;
  DWORD  dwTotal;

#if 0
  /*
    TEST
  */
  MessageBox(hWnd, pSrcFile,  "Source", MB_OK);
  MessageBox(hWnd, pDestFile, "Dest", MB_OK);
  return TRUE;
#endif

  /*
    1) Open the source file
  */
#ifdef MEWEL
  fdSrc = open(pSrcFile, O_RDONLY);
#else
  fdSrc = OpenFile(pSrcFile, &OFSrc, OF_READ);
#endif
  if (fdSrc < 0)
  {
    sprintf(szActivity, "Couldn't open the source file %s [Code %d]", 
                         pSrcFile, OFSrc.nErrCode);
    MessageBox(hWnd, szActivity, "Open Error", MB_OK);
    return FALSE;
  }

  /*
    2) Create the destination file
  */
#ifdef MEWEL
  fdDest = open(pDestFile, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,S_IREAD|S_IWRITE);
#else
  fdDest = OpenFile(pDestFile, &OFDest, OF_CREATE | OF_WRITE);
#endif
  if (fdDest < 0)
  {
    sprintf(szActivity, "Couldn't open the destination file %s [Code %d]", 
                         pDestFile, OFDest.nErrCode);
    MessageBox(hWnd, szActivity, "Open Error", MB_OK);
    _lclose(fdSrc);
    return FALSE;
  }

  /*
    3) Do the copy
  */
  dwTotal = _Copy(hWnd, pSrcFile, pDestFile, fdSrc, fdDest);

  /*
    4) Rename the source file to a BAK file. The backup file should
       be placed somewhere on the network.
  */
  GetProfileString("PCMOVE", "BackupDirectory", "", szBackupDir, sizeof(szBackupDir));
  if (szBackupDir[0])
  {
    sprintf(szBackupFile, "%s%s", szBackupDir, strrchr(pSrcFile, '\\'));
    if ((pDot = strrchr(szBackupFile, '.')) != NULL)
      *pDot = '\0';
    strcat(szBackupFile, ".BAK");     /* Tack on a BAK extension */
    unlink(szBackupFile);             /* Get rid of the old one  */
    rename(pSrcFile, szBackupFile);   /*   and make a new one    */
  }

  /*
    5) Write the activity string to the logfile
  */
  WriteLogfile(pSrcFile, pDestFile, dwTotal);

  return TRUE;
}


DWORD PASCAL _Copy(HWND hWnd, PSTR pszSrc, PSTR pszDest, int fdSrc, int fdDest)
{
  MEMHANDLE hBuf;
  LPSTR   lpBuf;
  HCURSOR hOldCursor;
  HDC     hDC;
  HWND    hWndStatus;
  RECT    r;
  int     nBytes;
  DWORD   dwTotal;
  char    szActivity[128];

  hBuf = GlobalAlloc(GMEM_MOVEABLE, (DWORD) COPYBUFSIZE);
  lpBuf = GlobalLock(hBuf);

  hOldCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  hWndStatus = GetDlgItem(hWnd, ID_FINDPATH);
  hDC = GetDC(hWndStatus);
  GetClientRect(hWndStatus, &r);

  dwTotal = 0;
  while ((nBytes = _lread(fdSrc, lpBuf, COPYBUFSIZE)) > 0)
  {
    dwTotal += nBytes;
    _lwrite(fdDest, lpBuf, nBytes);

    FillRect(hDC, &r, GetStockObject(WHITE_BRUSH));
    sprintf(szActivity, "Writing %ld bytes...", dwTotal);
    TextOut(hDC, 0, 0, szActivity, strlen(szActivity));
  }

  ReleaseDC(hWndStatus, hDC);
  SetCursor(hOldCursor);

  GlobalUnlock(hBuf);
  GlobalFree(hBuf);
  _lclose(fdSrc);
  _lclose(fdDest);

  sprintf(szActivity, "%ld bytes were copied from %s copied to %s", 
          dwTotal, pszSrc, pszDest);
  MessageBox(hWnd, szActivity, "Copy", MB_OK);
  return dwTotal;
}


void PASCAL WriteLogfile(PSTR pSrcFile, PSTR pDestFile, DWORD dwTotal)
{
  int fd;
  OFSTRUCT OFSrc;
  char szBuf[192];
  char szDate[9], szTime[9];

  GetProfileString("PCMOVE", "LogFile", "LOGFILE", szBuf, sizeof(szBuf));

#ifdef MEWEL
  fd = open(szBuf, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,S_IREAD|S_IWRITE);
#else
  fd = OpenFile(szBuf, &OFSrc, OF_WRITE);
#endif
  if (fd >= 0)
  {
    _llseek(fd, 0L, 2);
    sprintf(szBuf, "%s %s Copied file %s to %s, %ld bytes.\r\n",
            _strdate(szDate), _strtime(szTime), pSrcFile, pDestFile, dwTotal);
    _lwrite(fd, (LPSTR) szBuf, strlen(szBuf));
    _lclose(fd);
  }
}
