/*********************************************************************
 *
 *            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. 
 *
 *************************************************************************/



#define _MISC_C_
#include "ezfm.h"
/*********************************************************************/
int   getSerialNumber          MY_ANSIARGS((void));
char  *getNewFileName          MY_ANSIARGS((char *pname, char *dir,
					    int timestr, char *name_ret));
char  *getTmpFileName          MY_ANSIARGS((char *name_ret));
void  deleteTmpFile            MY_ANSIARGS((char *pname, int when));
int   locateCommand            MY_ANSIARGS(( char *name, char *path_ret ));
void  removeTrailingSpaces     MY_ANSIARGS(( char *buf));

/*********************************************************************/
static sequenceNumber;
static char *defaultPath="/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin";
/*********************************************************************/

int getSerialNumber() {return(sequenceNumber++);}

/*********************************************************************
 *
 *  return a tmpfilename  "/tmp/pid-tmp-serial" 
 */
char *getTmpFileName(path_ret) char *path_ret;
{ return(getNewFileName(NULL, NULL, 0, path_ret));}

/*********************************************************************
 *
 * Generate a tmpfile name in "dir". If dir==NULL, '/tmp' is used.
 * If 'pname' != NULL, its basename is attached to the tmp name generated,
 * otherwise, "tmp" is used.
 * If timestr != 0; the current time string is the leading part of the
 * name generated, otherwise, pid is used.
 */

char *getNewFileName(pname, dir, timestr, path_ret)
     char *pname, *dir, *path_ret; int timestr;
{
  char        *p, *n = "tmp";
  struct stat stat_buf;
  
  if(dir == NULL) dir = "/tmp";
  if(pname)
    {
      p = strrchr(pname, '/');
      if( p && p[1] != '\0') n = p+1;
      else n = pname;
    }
  if(lstat(dir, &stat_buf) >= 0 && S_ISDIR (stat_buf.st_mode))
    {
      char   tname[256], tmstr[16];
      int     i = 0;

      if(timestr) 
	{
	  time_t ctm = time(NULL);
	  struct tm *ltm = localtime(&ctm);
	  strftime(tmstr, 15, "%Y%m%d%H%M", ltm);
	}
      else sprintf(tmstr, "%d", getpid());

      do {
	sprintf(tname, "%s/%s-%s-%d",dir,tmstr,n, i++);
      } while(access(tname, F_OK) == 0 && i < 200);
      strcpy(path_ret, tname);
      return(path_ret);
    }
  return(NULL);
}
/*********************************************************************
 *
 * delete a tmp file at a given time.
 */
static void timerCallback(timer, data)
     EZ_Timer *timer; void *data;
{
  char *pathname = (char *)data;
  if(pathname)
    {
      if(access(pathname,F_OK) == 0)
	{
	  struct stat stat_buf;
	  int omtime = EZ_GetTimerIntData(timer);
	  stat(pathname, &stat_buf); 	  
	  if(omtime == (int)stat_buf.st_ctime) unlink(pathname);
	}
      (void)EZ_Free(data);
    }
}

void deleteTmpFile(pathname, when)
     char *pathname; int when;
{
  if(access(pathname, F_OK) == 0)
    {
      if(when <= 0) unlink(pathname);
      else
	{
	  char *data = (char *)EZ_Malloc( (strlen(pathname)+1) * sizeof(char));
	  struct stat stat_buf;
	  stat(pathname, &stat_buf); 
	  (void)EZ_CreateTimer(when, 0, 0, timerCallback, data,  (int)stat_buf.st_ctime);
	}
    }
}
/*********************************************************************/
int locateCommand(name, path_ret)
     char *name; char *path_ret;
{
  if(name)
    {
      char try[1024], *ptr, path[1024], save;
      int  found = 0, len,  namelen = strlen(name);

      ptr = getenv("PATH");
      if(ptr != NULL) strcpy(path, ptr);
      else strcpy(path, defaultPath);

      ptr = path;
      while (*ptr != '\0' && found == 0)
	{
	  len = 0;
	  while (*ptr != ':' && *ptr != '\0')
	    { len++;  ptr++; }
	  save = *ptr;
	  *ptr = '\0';
	  sprintf(try, "%s/%s", ptr-len, name);
	  *ptr = save;
	  if(*ptr) ptr++;
	  found = (access(try, X_OK) == 0); 
	  if(found) break;
	}
      if(found) { strcpy(path_ret, try); return(1); }
      else path_ret[0] = '\0';
    }
  return(0);
}
/*********************************************************************/

void removeTrailingSpaces(buf) char *buf;
{
  if(buf && buf[0] != '\0')
    {
      int len = strlen(buf);
      char *ptr =buf + len - 1;
      while(*ptr == ' ' || *ptr == '\t' && ptr != buf) ptr--;
      if(ptr == buf) ptr[0] = '\0';
      else ptr[1] = 0;
    }
}
/**************************************************************************/	  
#undef _MISC_C_
