/* DIVIDE_START=getpath.c */
#include <stdio.h>
#include <stdlib.h>
#include	<ctype.h>	// for isspace
#include <string.h>
#include <winb.h>
#include <te.h>
#include <fntb.h>
#include <gui.h>
#include	<egb.h>
#include	<file_dlg.h>

#include	"var.h"

static	int		GetPathFuncPathNum;
static	char	*GetPathFuncPathMes;
static	char	GetPathNamePath[ MAX_DIRS ];
char	*GetPathName( char *path )
{
	int		i;

	GetPathNameSubSw= NO;

	*GetPathNamePath = '\0';
	for ( i=0; CopyInsideVarChar[i].insideVar != NULL ; i++ ) {
		if ( path == CopyInsideVarChar[i].insideVar ) {
			GetPathFuncPathNum = i;		break;
		};
	};
	if ( CopyInsideVarChar[i].insideVar == NULL )	return(GetPathNamePath);

	// 変数名を得る
	for ( i=0; InsideVar[i].Mes != NULL ; i++ ) {
		if ( path == InsideVar[i].insideVar )		break;
	};
	GetPathFuncPathMes= InsideVar[i].Mes;

	MMI_SendMessage( GetPathMessage, MM_SETMSG, 1, GetPathFuncPathMes );
	MMI_SendMessage( GetPathText, MM_SETTEXT, 3, path, strlen(path), FALSE );
	//	画面を表示する
	MMI_SendMessage( GetPathDialog, MM_ATTACH , 1 , TopMenuHyperDialog);
	MMI_SendMessage( GetPathDialog, MM_SHOW , 0 );
//	MMI_SendMessage( GetPathText, MM_WAKE, 0 );

	// TopMenuSubMenuBarId をインアクティブにする
//	MTL_setAtrObj( TopMenuSubMenuBarId , MS_INACTIVEL40);

	// 新しいループを回す
	MMI_ExecSystem();

	// TopMenuSubMenuBarId をアクティブにする
//	MTL_resetAtrObj( TopMenuSubMenuBarId , (~MS_INACTIVEL40));

	if ( GetPathNameSubSw == YES ) {
		MMI_SendMessage(GetPathText,MM_GETTEXT,3,GetPathNamePath,MAX_DIRS,FALSE);
	};
	MMI_SendMessage( GetPathDialog, MM_ERASE , 0 );
	MMI_SendMessage( GetPathDialog, MM_DETACH , 0 );

	return( GetPathNamePath );
}

struct	FAPXSet	{
			char	*FAPXVar;
			int		VarLen;
			char	*insideVar;
}	FAPXSet[] = {
//	{	"ID",				8,		MyID		},
//	{	"PassWord",			8,		NULL		},
//	{	"HANDLE",			16,		MyHandle	},
//	{	"TEMP",				128,	TmpDir		},
	{	"LOG",				128,	InDir		},
//	{	"UP",				128,	UpDir		},
	{	"CATLOG",			128,	OutDir		},
	{	"COMPRESS",			3,		NULL		},
	{	"LOGmode",			8,		NULL		},
	{	NULL,				0,		NULL		}
};

int		getFAPXSET( char *path )
{
	int		i, len;
	char	*p1, *p2;
	char	dummy[ 1024 ];
	FILE	*fpi;

	strcpy( FileName, path );	append_yen( FileName );
	strcat( FileName, "fapxset.set" );
	if ( (fpi= fopen( FileName, "r" ) ) == NULL )	return( NO );

	while ( fgets( str, LINE, fpi ) != NULL ) {
		for ( i=0; FAPXSet[i].FAPXVar != NULL; i++ ) {
			p1 = FAPXSet[i].FAPXVar;
			len = strlen( p1 );
			if (	strncmp( str, p1, len ) == 0
			&&		( p2 = FAPXSet[i].insideVar ) != NULL
			&&		(	isspace( str[len] )
					||	str[len] == ':'
					)
			) {
				p1 = skip_space( str + len );
				if ( strncmp( p1, ":={", 3 ) != 0 ) {
					without_crlf( str );
					sprintf( dummy, "\n<%s>で : = { がない", str );
					PrintMessage( dummy );	showKakunin( );
				} else {
					p1 += 3;
					for ( len =FAPXSet[i].VarLen; len>0; len-- ) {
						if ( isspace( *p1 ) )		break;
						if ( *p1 == '}' )			break;
						*p2++ = *p1++;
					};
					*p2 = '\0';
				};
			};
		};
	};
	fclose( fpi );
//	sprintf( str, "\n出力ディレクトリ:<%s>", OutDir );
//	PrintMessage( str );
	return( YES );
}

int		GetPluralPath( char *path, const char *mes, int pluralSw )
{
	unsigned	int		MSIctCnt;
				int		ret;
				char	*p, str[ MAX_DIRS ];
				FILE	*fpi;

	sprintf( str, "\n%sを指定して下さい。", mes );
	if ( pluralSw == YES )		strcat( str, "複数指定可。" );
	PrintMessage( str );

	// 複数のディレクトリが指定 → 最後のディレクトリで FDLG を表示する
	strcpy( str, path );	p= str;
	while( *p ) {
		if ( *p == ',' || *p == ';' ) {
			strcpy( str, p+1 );		p= str;
		} else {
			p++;
		};
	};
	if ( *str == '\0' ) {	// , or ; で終わっている場合
		strcpy( str, path );
		for ( p= str ; *p != '\0' ; p++ ) {
			if ( *p == ',' || *p == ';' ) {		*p= '\0';	break;	};
		};
	};

	// ファイルダイヤログを表示してパスを入力する
	p= last_yen( str );
	if ( p != str ) p++;
	if ( ( fpi=	fopen( str, "r" ) ) != NULL ) {
		fclose( fpi );
		FDG_SetFileText( p );
		// str からファイル名を削除する
		p--;	*p = '\0';
	};
	ret=FDG_DspFileDlg(TopMenuHyperDialog,FDG_TEXT,str,ExtStrAll,&MSIctCnt);
	if ( ret > 0 ) {
		FDG_GetPathName( str, &ret, 0 );
		if (	*path == '\0'
		||		pluralSw == NO
		) {
			strcpy( path, str );
		} else {
			sprintf( strSub, "\n<%s>に<%s>を追加しますか？", path, str );
			if ( get_yesno( strSub ) == YES ) {
				if ( strlen( path ) + strlen( str ) + 1 > MAX_DIRS ) {
					PrintMessage( "\n長すぎて追加できませんm(__)m" );
				} else {
					strcat( path, ";" );	strcat( path, str );
				};
			} else {						strcpy( path, str );
			};
		};
		ret= 1;
	};
	return( ret );
}

/*	initDataGETPATH:GetPathIcon:MJ_ICONL40の呼び出し関数	*/
/*	initDataGETPATH:GetPathDButton[0]:MJ_DBUTTONL40の呼び出し関数	*/
/*	initDataGETPATH:GetPathDButton[1]:MJ_DBUTTONL40の呼び出し関数	*/
/*	initDataGETPATH:GetPathDButton[2]:MJ_DBUTTONL40の呼び出し関数	*/
int		GetPathFunc( int kobj )
{
	char	str[ MAX_DIRS ] ;

	if ( kobj == GetPathIcon ) {
		// GetPathDialog をインアクティブにする
		MTL_setAtrObj( GetPathDialog, MS_INACTIVEL40);
		// パスを指定する
		MMI_SendMessage(GetPathText,MM_GETTEXT,3,str,256,FALSE);
		if ( GetPluralPath( str, GetPathFuncPathMes, YES ) > 0 ) {
			if (	CopyInsideVarChar[GetPathFuncPathNum].insideVar ==
					CatlogMesFileName
			) {
				CloseCATLOGMessageFile();
			};
			MMI_SendMessage(GetPathText,MM_SETTEXT,3,str,strlen(str),FALSE);
			MMI_SendMessage(GetPathDialog,MM_SHOW,0);
		};
		// GetPathDialog をアクティブにする
		MTL_resetAtrObj( GetPathDialog, (~MS_INACTIVEL40));
	} else if ( kobj == GetPathDButton[0] ) {				// 取消
		GetPathNameSubSw= NO;

		if ( Display == YES )	PrintMessage( " ← 取消" );

		MMI_SetHaltFlag( TRUE );
	} else if ( kobj == GetPathDButton[1] ) {				// 実行
		GetPathNameSubSw= YES;

		if ( Display == YES )	PrintMessage( " ← 実行" );

		MMI_SetHaltFlag( TRUE );
	} else if ( kobj == GetPathDButton[2] ) {				// 詳細設定
		CopyInside2Details();				// 内部変数を GUI 変数に転送する

		// GetPathDialog をｲﾝｱｸﾃｨﾌﾞにする
		MMI_SendMessage( GetPathDialog, MM_ERASE , 0 );
		MMI_SendMessage( GetPathDialog, MM_DETACH , 0 );

		GetDetails();

		// GetPathDialog をアクティブにする
		MMI_SendMessage( GetPathDialog, MM_ATTACH, 1, TopMenuHyperDialog );
		MMI_SendMessage( GetPathDialog, MM_SHOW , 0 );

		if ( DetailsSubSw == YES ) {
			MMI_SendMessage( GetPathText, MM_ERASE,0);
//			MMI_SendMessage( GetPathText, MM_SETTEXT, 3, NULL, 0, FALSE );
			MMI_SendMessage( GetPathText, MM_SETTEXT, 3, 
				CopyInsideVarChar[GetPathFuncPathNum].insideVar,
				strlen( CopyInsideVarChar[GetPathFuncPathNum].insideVar ),
				FALSE
			);
			MMI_SendMessage( GetPathText, MM_SHOW,0);
		};
	};
	return NOERR ;
}

/* DIVIDE_END */
