#include <string.h>
#include <ctype.h>

#include "language.h"

#include "filetype.h"

static const int moDay = ('O'<<8) | 'M';
static const int weDay = ('E'<<8) | 'W';
static const int tuDay = ('U'<<8) | 'T';
static const int frDay = ('R'<<8) | 'F';
static const int thDay = ('H'<<8) | 'T';
static const int saDay = ('A'<<8) | 'S';
static const int suDay = ('U'<<8) | 'S';

char *msgOfFileType( const char *name, FileType *type, FileType *type1 )
{
  const char *p( name+strlen(name)-4 );
  register FileType rt( FileFile );
  register int ext( (toupper(p[2])<<8) | toupper(p[1]) );

  if( !stricmp( p , ".Pkt" ) )
    rt = FileMail;
  else if( !stricmp( p , ".Req" ) )
    rt = FileReq;
  else if( strlen(p)==4 && p[0]=='.' &&
  	   ( ext==moDay || ext==weDay || ext==tuDay || ext==frDay ||
	    ext==thDay || ext==saDay || ext==suDay
	   ) )
    rt = FileXMail;
  else if( !stricmp( p , ".Rip" ) || 
           !stricmp( p , ".Riz" ) || 
           !stricmp( p , ".Rif" ) )
    rt = FileRIP;

  if( type!=NULL )
    if( rt!=FileRIP )
      *type = rt;
    else
      *type = FileFile;

  if( type1!=NULL )
    *type1 = rt;

  switch( rt )
  {
  case FileMail:
    return Msg_FileTypeMail;

  case FileXMail:
    return Msg_FileTypeXMail;

  case FileReq:
    return Msg_FileTypeFREQ;

  case FileFile:
    return Msg_FileTypeFile;

  case FileRIP:
    return Msg_FileTypeRIP;

  default:
    return Msg_FileTypeUnknown;
  }
}
