#include "config.h"
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
int stat();
char *path_segment();

bool
findfile(path,n,fontmag,name)
char path[STRSIZE];  /* PIXEL path */
char n[STRSIZE];     /* name of font */
long fontmag;        /* magnification */
char name[STRSIZE];  /* full name of PXL file  (returned) */
{
   char local_path[STRSIZE];
   char *pathpt;
   struct stat s;
   int rc = -1;
   int resolution, i;

   resolution = (int)(fontmag/5.0 +0.5) ;

   for(i=0; (pathpt=path_segment((bool)(i==0),path,local_path))!=NULL;i++)
       {
#ifdef USEPXL
               sprintf(name,"%s/%d/%s.pk",pathpt,resolution,n);
               if ((rc = stat(name,&s))!=0)
               {
                       sprintf(name,"%s/%d/%s.pxl",pathpt,resolution,n);
                       if ((rc = stat(name,&s))!=0)
                       {
                               sprintf(name,"%s.pk",n);
                               if ((rc = stat(name,&s))!=0)
                               {
                                       sprintf(name,"%s.pxl",n);
                                       if ((rc = stat(name,&s))!=0)
                                       {
                                               sprintf(name,"%s/%ld/%s.pk",pathpt,fontmag,n);
                                               if ((rc = stat(name,&s))!=0)
                                               {
                                                       sprintf(name,"%s/%ld/%s.pxl",pathpt,fontmag,n);
                                                       if ((rc = stat(name,&s))!=0)
                                                       {
#ifndef MSDOS
                                                               sprintf(name,"%s/%d/%s.pk",pathpt,resolution,n);
                                                               if ((rc = stat(name,&s))!=0)
                                                               {
                                                                       sprintf(name,"%s/%d/%s.pxl",pathpt,resolution,n);
                                                                       if ((rc = stat(name,&s))!=0)
                                                                       {
#endif
                                                                               sprintf(name,"%s/%d/%s.",pathpt,resolution,n);
                                                                               rc = stat(name,&s);
#ifndef MSDOS
                                                                       }
                                                               }
#endif
                                                       }
                                               }
                                       }
                               }
                       }
               }
#else
               sprintf(name,"%s/%d/%s.gf",pathpt,resolution,n);
               if ((rc = stat(name,&s))!=0)
               {
                       sprintf(name,"%s/%ld/%s.gf",pathpt,fontmag,n);
                       rc = stat(name,&s);
               }
#endif
               if (rc==0) return(TRUE);
       };

#ifdef FUTURE
   for(i=0; (pathpt=path_segment((bool)(i==0),VFPATH,local_path))!=NULL;i++) {
      sprintf(name,"%s/%s.vfm",pathpt,n);
      printf("searching virtual font <%s>\n",name);
      if (stat(name,&s) == 0) return(TRUE);
   }
#endif

#ifdef USEPXL
   /* return error messaage */
   sprintf(name,"font not found: <%s>/<%d;%ld>/%s.gf",
                 path,resolution,fontmag,n);
#else
   sprintf(name,"font not found: <%s>/<%d;%ld>/%s.<pk;pxl>",
                 path,resolution,fontmag,n);
#endif

return(FALSE);
}

char *
path_segment(first,full_path,local_path)
bool first;
char *full_path, *local_path;
{
       static char *pppt;
       char *pathpt;

       if (first) pathpt = strcpy(local_path,full_path);
       else pathpt = pppt;
       if (pathpt != NULL) {
#ifdef unix
               pppt = strchr(pathpt , ':' );
#else
               pppt = strchr(pathpt , ';' );
#endif
#ifdef MSDOS
               pppt = strchr(pathpt , ';' );
#endif
               if (pppt != NULL) {
                  *pppt = '\0';
                  pppt++;
                  }
       }
return pathpt;
}