/*********************************************************************
 *
 *            EZFM---EZWGL based file manager for X11
 *
 *             Copyright (C) 1998  Maorong Zou
 *
 *
 *  This file is part of EZFM.
 *
 *  EZFM is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * EZFM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with EZFM; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 *************************************************************************/



/***************************************************************************
 *
 *  Given a dest directory and an encoded message, decode the file from
 *  message and move/copy/link the file to the dest directory.
 */
#define _DNDB_C_
#include "ezfm.h"

/****************************************************************************/

int moveToDirByName MY_ANSIARGS((char *destDir, char *message, int length));
int linkToDirByName MY_ANSIARGS((char *destDir, char *message, int length));

int copyToDirByName MY_ANSIARGS((char *destDir, char *message, int length));
int copyToDirByContent MY_ANSIARGS((char *destDir, char *message, int length));
int copyToDirByTar MY_ANSIARGS((char *destDir, char *message, int length));


/****************************************************************************
 *
 *  Move a file/directory to destDir
 */

/*--Move Files by name (only) --*/

int moveToDirByName(destDir, message, length)
     char *destDir; char *message; int length;
{
  char *filePath = message + PATHNAME_OFFSET;
  char command[1024], frm[500], to[500];

  if(canSeeTheFile(filePath, message) == 0)  return(EZ_DND_FAILURE);
  ezfmQuotePathname(filePath, frm);
  ezfmQuotePathname(destDir, to);
  sprintf(command, MOVE_CMD, frm, to);
  executeShCmd(command, NULL, 0, NULL);
  return(EZ_DND_SUCCESS);
}

/****************************************************************************
 *
 *  Copy a file/directory to destDir
 */

/*--Copy Files by name --*/

int copyToDirByName(destDir,message,length)
     char *destDir; char *message; int length;
{
  char *filePath = message + PATHNAME_OFFSET;
  char command[1024], frm[500], to[500];
  if(canSeeTheFile(filePath, message) == 0)  return(EZ_DND_FAILURE);
  ezfmQuotePathname(filePath, frm);
  ezfmQuotePathname(destDir, to);
  sprintf(command, COPY_CMD, frm, to);
  executeShCmd(command, NULL, 0, NULL);
  return(EZ_DND_SUCCESS);
}

/*--Copy Files by content --*/

int copyToDirByContent(destDir,message,length)
     char *destDir; char *message; int length;
{
  char *filePath = message + PATHNAME_OFFSET;
  char *ptr, newpath[256];
  int  fd;

  ptr = strrchr(filePath, '/');
  if(!ptr) return(EZ_DND_FAILURE);
  sprintf(newpath, "%s%s",destDir, ptr);
  if((fd = open(newpath,O_RDWR|O_CREAT, 0644)) >= 0)
    {
      char   *buf = message+ HEADER_OFFSET;
      size_t size = (size_t)(length - HEADER_OFFSET);
      int    ret = EZ_DND_SUCCESS;
      if(write(fd, buf,  size) != size) ret = EZ_DND_FAILURE;
      close(fd);
      return(ret);
    }
  return(EZ_DND_FAILURE);      
}

/*--Copy Files by tar --*/

int copyToDirByTar(destDir,message,length)
     char *destDir; char *message; int length;
{
  FILE  *fp;
  char  *ptr, tarcmd[1024], fname[512];
  char *filePath = message + PATHNAME_OFFSET;

  ptr = strrchr(filePath, '/');
  if(!ptr) return(EZ_DND_FAILURE);
  if(chdir(destDir) >= 0)
    {
      /*  /bin/tar xfz - filename */
      ptr++;
      ezfmQuotePathname(ptr, fname);
      sprintf(tarcmd, TAR_EXTRACT_CMD, fname);
      if((fp = popen(tarcmd, "w")) != NULL)
	{
	  char   *buf = message+ HEADER_OFFSET;
	  size_t size = (size_t)(length - HEADER_OFFSET);
	  int    ret = EZ_DND_SUCCESS;
	  if(fwrite(buf, 1,  size, fp) != size) ret = EZ_DND_FAILURE;
	  pclose(fp);
	  return(ret);
	}
    }
  return(EZ_DND_FAILURE);      
}

/****************************************************************************/
/* --Link Files by name only */

int linkToDirByName(destDir,message,length)
     char *destDir; char *message; int length;
{
  char *filePath = message + PATHNAME_OFFSET;
  char command[1024], frm[500], to[500];

  if(canSeeTheFile(filePath, message) == 0)  return(EZ_DND_FAILURE);
  ezfmQuotePathname(filePath, frm);
  ezfmQuotePathname(destDir,to);
  sprintf(command, LINK_CMD, frm, to);
  executeShCmd(command, NULL, 0, NULL);
  return(EZ_DND_SUCCESS);
}
/*****************************************************************/
#undef _DNDB_C_
