
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/exec_protos.h>
#include <clib/utility_protos.h>
#include <clib/locale_protos.h>

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

#include "project.h"

#define CATCOMP_NUMBERS

#include "project.catalog.h"

extern char PubName[];
extern struct Catalog *Catalog;

char *ProjectPathName;
char *ProjectName;
char *WindowTitle;

char *StartDir;
char *PrgDir;

/****************************************************************************
***									  ***
***	Fonctions divers						  ***
***									  ***
****************************************************************************/

void ReplaceChar( char **dest , char *source )
{
    free( *dest );
    *dest = strdup( source );
}

/****************************************************************
***	Change le nom du projet en cours		      ***
****************************************************************/

void ChangeProjectName( char *pathname ) {
  char temp[200];
  char *wintitle = "Project Handler : Sources Window / Project Name : ";

    ReplaceChar( &ProjectPathName , pathname );
    ReplaceChar( &ProjectName , BaseName( pathname ) );
    ProjectName[ strlen(ProjectName) - 4 ] = 0;
    if ( ROMVersion >=38 )
	strcpy( temp , GetCatalogStr( Catalog , MSG_WIN_TITLE_SOURCE , wintitle ) );
    else
	strcpy( temp , wintitle );
    strcat( temp , BaseName( pathname ) );
    ReplaceChar( &WindowTitle , temp );
    SetWindowTitles( ProjectWnd , WindowTitle , (UBYTE *)-1 );
}

/****************************************************************
***	Change le nom du repertoire de DICE		      ***
****************************************************************/

void ChangeDICEDir( char *dir ) {
  char *prgt;

    ReplaceChar( &DICEDir , dir );
    prgt = malloc( strlen( DICEDir ) + 10 );
    strcpy( prgt , DICEDir );
    strcat( prgt , "/bin/" );
    ReplaceChar( &PrgDir , prgt );
    free( prgt );
}

/****************************************************************
***	Lance l'edition d'un module spécifié par son nom      ***
****************************************************************/

void EditModule( char *name , int type ) {
  char *buf;
  struct TagItem *ti;

    ti = FindTagItem( SA_PubName , ScreenTags );
    if ( ti )
	SetDefaultPubScreen( (char *)(ti->ti_Data) );
    buf = malloc( strlen( PrgDir ) + strlen( name ) + 20 );
    switch( type )
    {
      case 'c':
      case 'h':
      case 'a':
	strcpy( buf , Editor );
	break;
      case 'g':
	strcpy( buf , GadTools );
	break;
      case 'r':
	strcpy( buf , ARexxBox );
	break;
      default:
	strcpy( buf , Editor );
	break;
    }
    strcat( buf , " \"" );
    strcat( buf , name );
    strcat( buf , "\"" );
    StartCLI( buf , TRUE , NULL , NULL );
    free( buf );
}

/****************************************************************
***	Gestion des fichiers de commentaires		      ***
****************************************************************/

char *CommentName( char *module ) {
  char *result;

    result = malloc( strlen( module ) + 10 );
    strcpy( result , module );
    strcat( result , ".comment" );
    return( result );
}

BOOL IsCommented( char *module ) {
  char *modulec;
  struct FileInfoBlock fib;
  BOOL r=FALSE;

    modulec = CommentName( module );
    if ( GetInfo( &fib , modulec ) ) {
	r = TRUE;
    }
    free( modulec );
    return( r );
}


