/* ======================================================================== */
/* = Name            : GetFileType V11.6				  = */
/* =									  = */
/* ======================================================================== */
/* = Author/Copyright : (c) 1995 by Andreas Ralph Kleinert.               = */
/* =		       Freeware. All rights reserved.			  = */
/* ======================================================================== */
/* = Function	      :                                                   = */
/* =		                                                          = */
/* ======================================================================== */
/* = Last Update      : 17.05.1995					  = */
/* =									  = */
/* ======================================================================== */
/* = Compiler	      : SAS/C V6.55  					  = */
/* =		        (smakefile)                                       = */
/* ======================================================================== */

#include <exec/types.h>
#include <exec/memory.h>

#include <superview/superview.h>
#include <superview/superviewbase.h>
#include <svobjects/svobjects.h>

#include <libraries/asl.h>
#include <workbench/startup.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/asl.h>
#include <proto/superview.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


   /* Help- and Info- Texts */

char entry1_text  [] = "\2331;32;40mGetFileType V11.6 \2330;32;40m\2333;32;40m(FREEWARE)\2330;32;40m\n(c) 1995 by Andreas Ralph Kleinert.\nAndreas R. Kleinert, Grube Hohe Grethe 23, D-57074 Siegen, Germany.\n";
char entry2_text  [] = "Examines SVObject relations to graphics file types.\n";
char entry3_text  [] = "USAGE : \2330;33;40mGetFileType\2330;31;40m <PicFileName>\n";


char ver_text [] = "\0$VER: GetFileType V11.6 (17.5.95)";


/* *************************************************** */
/* *						     * */
/* * Error-Messages for Leave() and KF_Message()     * */
/* *						     * */
/* *************************************************** */

char asllib_text 	[] = "You need \42asl.library\42 V37+ !";
char svlib_text 	[] = "You need \42superview.library\42 V11+ !";


/* *************************************************** */
/* *						     * */
/* * Function Declarations			     * */
/* *						     * */
/* *************************************************** */

void __regargs GetFileType(char *filename);
void __regargs Leave(char *endtext, long code);


   /* Functions from module "GetFileType_Subs.o" : */

extern void __stdargs K_Printf(char *formatstring, ...);


/* *************************************************** */
/* *						     * */
/* * Additional Base Declarations		     * */
/* *						     * */
/* *************************************************** */

extern struct ExecBase *SysBase;

struct Library       *AslBase       = N;
struct SuperViewBase *SuperViewBase = N;


/* *************************************************** */
/* *						     * */
/* * MAIN					     * */
/* *						     * */
/* *************************************************** */

extern struct WBStartup *WBenchMsg;
char		        *wbloadname = N;
long                     wb         = FALSE;

void main(long argc, char **argv)
{
 if(!argc)
  {
   wb = TRUE;

   if(WBenchMsg->sm_NumArgs>1) wbloadname = WBenchMsg->sm_ArgList[1].wa_Name;
  }

 if( argc > 3 || (argv[1][0] =='?')||(argv[2][0] =='?'))
  {
   K_Printf("%s%s%s", entry1_text, entry2_text, entry3_text);

   Leave(N, 0);
  }

 AslBase = (struct Library *) OpenLibrary("asl.library", 37);
 if(!AslBase) Leave(asllib_text, 102);

 SuperViewBase = (struct SuperViewBase *) OpenLibrary("superview.library", 11);
 if(!SuperViewBase) Leave(svlib_text, 103);


 /* Examine */

 if(argc < 2 && !wbloadname)
  {
   struct FileRequester     *request;
   struct TagItem __aligned  tags[4];
   char                      namebuffer [256];

   namebuffer[0] = (char) 0;

   tags[0].ti_Tag  = (Tag)   ASL_Hail;
   tags[0].ti_Data = (ULONG) "Select Picture to examine:";

   tags[1].ti_Tag  = (Tag)   ASL_OKText;
   tags[1].ti_Data = (ULONG) " Examine ";

   tags[2].ti_Tag  = (Tag)   ASL_CancelText;
   tags[2].ti_Data = (ULONG) " Quit ";

   tags[3].ti_Tag  = (Tag)   TAG_DONE;
   tags[3].ti_Data = (ULONG) N;

   request = AllocAslRequest(ASL_FileRequest, N);
   if(request)
    {
     if(AslRequest(request, &tags[0]))
      {
       strcpy(namebuffer, request->rf_Dir);
       if(    (namebuffer[strlen(namebuffer)-1] !=      ':')
           && (namebuffer[strlen(namebuffer)-1] !=      '/')
           && (namebuffer[0]                    != (char) 0) ) strcat(namebuffer, "/");

       strcat(namebuffer, request->rf_File);

       if(namebuffer[0] != (char) 0) GetFileType(namebuffer);
      }

     FreeAslRequest(request);
    }
  }else
  {
   if(wbloadname) GetFileType(wbloadname);
    else          GetFileType(argv[2]);
  }

 Leave(N, 0);
}

/* *************************************************** */
/* *						     * */
/* * Show-Function				     * */
/* *						     * */
/* *************************************************** */

void __regargs GetFileType(char *filename)
{
 APTR handle;
 ULONG retval = SVERR_NO_ERROR, filetype;

 handle = SVL_AllocHandle(N);
 if(handle)
  {
   if(!(retval = SVL_InitHandleAsDOS(handle, N)))
    {
     if(!(retval = SVL_GetFileType(handle, filename, &filetype)))
      {
       struct List           *obj_list = N;
       struct SVO_ObjectNode *svo_node = N;

       obj_list = &SuperViewBase->svb_SVObjectList;

       for(svo_node=(APTR) obj_list->lh_Head;(svo_node)&&(svo_node!=(APTR) &(obj_list->lh_Tail));)
        {
         if(svo_node->svo_TypeCode == filetype)
          { 
           K_Printf("\nInformation on related SVObject\n");

           K_Printf("\nName : \42%s\42",     svo_node->svo_FileName);
           K_Printf("\nType : %lx",          svo_node->svo_ObjectType);
           K_Printf("\nPri  : %ld",          (long) ((struct Node *)svo_node)->ln_Pri);
           K_Printf("\nID   : \42%s\42\n\n", svo_node->svo_TypeID);
          }
         svo_node = (APTR) ((struct Node *)svo_node)->ln_Succ;
        }
      }
    }
   
   SVL_FreeHandle(handle);

  }else retval = SVERR_NO_HANDLE;

 if(retval) Leave(SVL_GetErrorString(retval), 0);
}

/* *************************************************** */
/* *						     * */
/* * LEAVE : Global Exit Function Replacement	     * */
/* *						     * */
/* *************************************************** */

void __regargs Leave(char *endtext, long code)
{
 if(SuperViewBase) CloseLibrary((APTR) SuperViewBase);
 if(AslBase)       CloseLibrary((APTR) AslBase);

 if(endtext)       if(!wb) K_Printf("%s\n", endtext);

 exit(code);
}
