/* DIVIDE_START=makeidx.c */
/*
	ＮＩＦＴＹのＬＯＧ整理				makeidx.c
*/

#include	"catlog.h"

#define	TEST	1

/************************************************************************/
/* CATLOGで全体で１つのインデックスからフォーラム毎のインデックスを作る */
/************************************************************************/
void	makeidx_forum_mode()
{
	int		kai , seek_ichi , comment;
	char	old_forum[ 128 ] , new_forum[ 128 ];
	char	*p1 , *p2;
	FILE	*fpi , *fpo;

	strcpy( file_name , OutDir );	strcat( file_name , IDX_FILE_NAME );
	if ( ( fpi = fopen( file_name , "r" ) ) == NULL )	return;

	mess_check printf("\nインデックス・モードを変更します　→　");
/*
	printf(	"\nインデックス・ファイルをフォーラムごとに分割します。"
			"\nしばらくお待ち下さい ………… "
	);
*/

	*old_forum = '\0';	fpo = NULL;
	MesNum = 0;
	while( fgets( str , LINE , fpi ) != NULL ) {
		without_crlf( str );
		p1 = str;
			p1= skip_space( p1 );
		kai = atoi( p1 );							/* MES番号 HP MAIL */
			if ( *p1 == '-' ) p1++;
			while( isdigit( *p1 ) )		p1++;
			p1= skip_space( p1 );
		HatsugenNumber = atoi( p1 );				/* 発言番号 */
			while( isdigit( *p1 ) )		p1++;
			p1= skip_space( p1 );
		p2 = HatsugenDate;							/* 日時 */
			while( isdigit( *p1 ) )		*p2++ = *p1++;
				*p2 = '\0';
			p1= skip_space( p1 );
		p2 = new_forum;								/* FORUM名 HP名 ID名 */
			while( isalnum( *p1 ) )		*p2++ = *p1++;
				*p2 = '\0';
			p1= skip_space( p1 );
		seek_ichi = atoi( p1 );						/* シーク位置 */
			while( isdigit( *p1 ) )		p1++;
		if ( *p1 == '\0' ) {
			/* 旧タイプのインデックス情報の場合 */
			strcat( str , "   0" );
		};
			p1= skip_space( p1 );
		comment = atoi( p1 );						/* コメント番号 */

/*
		printf("\nforum=%s 会議室=%02d 発言=%3d 日時=%s ｼｰｸ位置=%4d ｺﾒﾝﾄ=%d",
			new_forum , kai , HatsugenNumber , HatsugenDate , seek_ichi ,
			comment
		);
*/

		if ( kai == ALL_MAIL )	kai = MAIL;
		if (	*old_forum == '\0'
		||		strcmp( old_forum , new_forum ) != 0
		||		(	(	kai < 0
					||	MesNum < 0
					)
					&&	MesNum != kai
				)
		) {
			if ( fpo != NULL )	fclose( fpo );
			strcpy( ForumName , new_forum );	MesNum = kai;
			strcpy( old_forum , new_forum );
			set_index_file_name( ForumName );		/* file.c */
/*
			printf("\n新しいｲﾝﾃﾞｯｸｽ･ﾌｧｲﾙです<%s>",file_name);
*/
			if ( ( fpo = fopen( file_name , "a" ) ) == NULL ) {
				error_open_file( "makeidx_forum_mode" );	error_end();
			};
		};
		fprintf( fpo , "%s\n" , str );
	};
	fclose( fpi );	fclose( fpo );

	/* mode:1 のｲﾝﾃﾞｯｸｽを削除する */
	strcpy( file_name , OutDir );	strcat( file_name , IDX_FILE_NAME );
	remove( file_name );

	mess_check printf("終了しました。");
}

int		isCATLOGFile( const char *f_name )
{
	int		num;
	char	*p1 , *p2;
	FILE	*fpi;

	if ( ( fpi=fopen( f_name , "r" ) ) == NULL ) {
		/* 起こり得ないエラー */
		strcpy( file_name , f_name );	error_open_file( "isCATLOGFile" );
	};
	fgets( str , LINE , fpi );	fclose( fpi );

	/* 1994.3.5 追加 チェックを強化した */
/* 1994.3.26 削除
	if ( strlen( str ) < 60 )					return( 0 );
*/

	if ( 		strncmp( str , "FORUM:" , 6 ) == 0 ) {
		p1 = str + 6;		p2 = str;
		while ( isalnum( *p1 ) ) *p2++ = *p1++;		*p2 = '\0';
		while ( *p1 == ' ' ) p1++;
		if ( strncmp( p1 , "MES:" , 4 ) == 0 ) {
			p1 += 4;		/* skip 'MES:' */
			num = atoi( p1 );
			if ( strlen( str ) <= 8 && num != 0 ) {
				strcpy( ForumName , str );	MesNum = num;
				return( num );
			};
		} else if ( strncmp( p1 , "LIB:" , 4 ) == 0 ) {
			p1 += 4;		/* skip 'LIB:' */
			num = atoi( p1 );
			if ( strlen( str ) <= 8 && num != 0 ) {
				strcpy( ForumName , str );	MesNum = LIB;
				HatsugenNumber = num;
				return( LIB );
			};
		};
	} else if ( strncmp( str , "MAIL:" , 5 ) == 0 ) {
		p1 = str + 5;		p2 = str;
		while ( isalnum( *p1 ) ) *p2++ = *p1++;		*p2 = '\0';
		if ( isIDname( str ) == YES ) {
			strcpy( ForumName , str );	MesNum = MAIL;
			return( MAIL );
		};
	} else if (	strncmp( str , "HP:" , 3 ) == 0 ) {
		p1 = str + 3;		p2 = str;
		while ( isalnum( *p1 ) ) *p2++ = *p1++;		*p2 = '\0';
		if ( isIDname( str ) == YES ) {
			strcpy( ForumName , str );	MesNum = HP;
			return( HP );
		};
	} else if (	strncmp( str , "PATIO:" , 6 ) == 0 ) {
		p1 = str + 6;		p2 = str;
		while ( isalnum( *p1 ) ) *p2++ = *p1++;		*p2 = '\0';
		if ( isIDname( str ) == YES ) {
			strcpy( ForumName , str );	MesNum = PATIO;
			return( PATIO );
		};
	} else if (	strncmp( str , "BILL:BILL" , 9 ) == 0 ) {
		strcpy( ForumName , "BILL" );	MesNum = BILL;
		return( BILL );
	} else if (	strncmp( str , "COLLECT_ID:" , 11 ) == 0 ) {
		p1 = str + 11;		p2 = str;
		while ( isalnum( *p1 ) ) *p2++ = *p1++;		*p2 = '\0';
		if ( isIDname( str ) == YES ) {
			strcpy( ForumName , str );	MesNum = COLLECT_ID;
			return( COLLECT_ID );
		};
	} else if (	strncmp( str , "ALL-MAIL" , 8 ) == 0 ) {
		strcpy( ForumName , "ALL-MAIL" );	MesNum = ALL_MAIL;
		return( ALL_MAIL );
	} else if (	strncmp( str , "CLIP" , 4 ) == 0 ) {
		strcpy( ForumName , "CLIP" );	MesNum = CLIP;
		return( CLIP );
	};
	return( 0 );
}

/******************************************************/
/* CATLOGを使って整理したﾌｧｲﾙからｲﾝﾃﾞｯｸｽを作る(ﾌｫｰﾗﾑ) */
/******************************************************/
static	void	makeidx_from_catlog_file_forum( const char *f_name )
{
	forever {
		CPDS = PSP;
		GET_LINE2	break;								/* 先読み */
		if ( isMesHyoudaiLine() == NO )	continue;
		if ( isMesSecondLine() == NO )	continue;

		catlog[ save_end ].line = atoi( PSP );
		catlog[ save_end ].seek_ichi = pool_seek_ichi;		/* seek 位置 */

		GET_LINE2	break;	/* 会議室番号の行 */
		present_str_copy( MesNumberLine );

		set_hatsugen_date_forum( MesNumberLine );			/* 日付 */
		catlog[ save_end ].date = date_to_int( HatsugenDate );

		if ( is_idx_data() == YES )	continue;

		/* コメント番号 */
		if (	strlen(  MesNumberLine ) > 21
		&&		isdigit( MesNumberLine[23] )
		) {			catlog[ save_end ].comment = atoi( MesNumberLine + 23 );
		} else {	catlog[ save_end ].comment = 0;
		};

		save_end++;
	};
}

/******************************************************/
/* CATLOGを使って整理したﾌｧｲﾙからｲﾝﾃﾞｯｸｽを作る(ﾒｰﾙ)   */
/******************************************************/
static	void	makeidx_from_catlog_file_mail( const char *f_name )
{
	MesNum = MAIL;
	forever {
		CPDS = PSP;
		GET_LINE2	break;								/* 先読み */
		if ( isMailFirstLine() == NO )	continue;

		catlog[ save_end ].line = id_to_int( PSP + 22 );
		catlog[ save_end ].comment = atoi( PSP );
		catlog[ save_end ].seek_ichi = pool_seek_ichi;		/* seek 位置 */
		set_hatsugen_date_mail( PSP );			/* 発言の日付 */
		catlog[ save_end ].date = date_to_int( HatsugenDate );

		if ( is_idx_data() == YES )	continue;

		save_end++;
	};
}

/******************************************************/
/* CATLOGを使って整理したﾌｧｲﾙからｲﾝﾃﾞｯｸｽを作る( HP )  */
/******************************************************/
static	void	makeidx_from_catlog_file_hp( const char *f_name )
{
	forever {
		CPDS = PSP;
		GET_LINE2	break;								/* 先読み */
		if ( isHPFirstLine() == NO )	continue;

		catlog[ save_end ].line = atoi( PSP );	/* 発言番号 */
		catlog[ save_end ].seek_ichi = pool_seek_ichi;		/* seek 位置 */

		set_hatsugen_date_hp( PSP );				/* 発言の日付 */
		catlog[ save_end ].date = date_to_int( HatsugenDate );

		if ( is_idx_data() == YES )	continue;

		catlog[ save_end ].comment = 0;					/* コメント先番号 */

		save_end++;
	};
}

/******************************************************/
/* CATLOGを使って整理したﾌｧｲﾙからｲﾝﾃﾞｯｸｽを作る(BILL)  */
/******************************************************/
static	void	makeidx_from_catlog_file_bill( const char *f_name )
{
	int		i , add;

	forever {
		CPDS = PSP;
		GET_LINE2	break;								/* 先読み */
		if ( check_bill_second_line() == NO )	continue;

		catlog[ save_end ].seek_ichi = pool_seek_ichi;		/* seek 位置 */

		for ( i=0 ; i<3 ; i++ ) strcpy( BillLine[i] , "         0" );
		IFPStrEqu2(BILL_SECOND_LINE2,strlen(BILL_SECOND_LINE2))	{
			BillLineNewTypeSw = TRUE;
		} else {
			BillLineNewTypeSw = FALSE;
		};
		for ( i=0 ; i<3 ; i++ ) {
			GET_LINE2 break;	present_str_copy( BillLine[i] );
		};
		add = 0;
		if ( BillLineNewTypeSw != TRUE )	add = 1;
		set_hatsugen_date_bill( BillLine[0] + add );		/* 発言の日付 */
		catlog[ save_end ].date = date_to_int( HatsugenDate );
		add = 0;	for ( i=0 ; i<3 ; i++ ) add += atoi( BillLine[ i ] + 6 );
		catlog[ save_end ].line = add;							/* 発言番号 */

		if ( is_idx_data() == YES )	continue;

		catlog[ save_end ].comment = 0;					/* コメント先番号 */

		save_end++;
	};
}

/***********************************************************/
/* CATLOGを使って整理したﾌｧｲﾙからｲﾝﾃﾞｯｸｽを作る(発言の収集) */
/***********************************************************/
static	void	makeidx_from_catlog_file_collect_id( const char *f_name )
{
	forever {
		CPDS = PSP;
		GET_LINE2	break;								/* 先読み */
		if ( isMesHyoudaiLine() == YES && isMesSecondLine() == YES ) {
			catlog[ save_end ].line = atoi( PSP );
			catlog[ save_end ].seek_ichi = pool_seek_ichi;		/* seek 位置 */
			GET_LINE2	break;	/* 会議室番号の行 */
			present_str_copy( MesNumberLine );
			MesNum = atoi( MesNumberLine + 1 );
			/* 日付 */
			set_hatsugen_date_forum( MesNumberLine );
			catlog[ save_end ].date = date_to_int( HatsugenDate );
			if ( is_idx_data() == YES )	continue;
			/* コメント番号 */
			if (	strlen(  MesNumberLine ) > 21
			&&		isdigit( MesNumberLine[23] )
			) {		catlog[ save_end ].comment = atoi( MesNumberLine + 23 );
			} else {catlog[ save_end ].comment = 0;
			};
			save_end++;
		} else if ( isMailFirstLine() == YES ) {
			MesNum = MAIL;
			catlog[ save_end ].line = id_to_int( PSP + 22 );
			catlog[ save_end ].comment = atoi( PSP );
			catlog[ save_end ].seek_ichi = pool_seek_ichi;	/* seek 位置 */
			set_hatsugen_date_mail( PSP );		/* 発言の日付 */
			catlog[ save_end ].date = date_to_int( HatsugenDate );

			catlog[ save_end ].line = atoi( PSP );
			catlog[ save_end ].comment = 0;
			if ( is_idx_data() == YES )	continue;
			save_end++;
		} else if ( isHPFirstLine() == YES ) {
			catlog[ save_end ].line = atoi( PSP );	/* 発言番号 */
			catlog[ save_end ].seek_ichi = pool_seek_ichi;		/* seek 位置 */
			MesNum = HP;
			set_hatsugen_date_hp( PSP );			/* 発言の日付 */
			catlog[ save_end ].date = date_to_int( HatsugenDate );
			if ( is_idx_data() == YES )	continue;
			catlog[ save_end ].comment = 0;				/* コメント先番号 */
			save_end++;
		};
	};
}

static	void
makeidx_from_catlog_file( const char *f_name , const char *ff_name )
{
	int		d , f_size;

	d = isCATLOGFile( f_name );

	f_size = _get_file_size( f_name );	if ( f_size <= 0 )	return;

	/* 先ず、新しい形式のインデックスから登録済みのデータを得る */
	save_start = save_end = 0;		get_idx_data();
	save_start = save_end;

	/* このファイルを読み、フォーラム名・会議室番号が一致するものを探す */
	strcpy( file_name , f_name );
	initial_check_pool( f_size , NO /* append 0x0a switch */ );
	if ( d > 0 || d == PATIO ) {
			makeidx_from_catlog_file_forum( f_name );
	} else if ( d == MAIL || d == ALL_MAIL ) {
			makeidx_from_catlog_file_mail( f_name );
	} else if (	d == HP ) {
			makeidx_from_catlog_file_hp( f_name );
	} else if (	d == BILL ) {
			makeidx_from_catlog_file_bill( f_name );
	} else if (	d == COLLECT_ID ) {
			makeidx_from_catlog_file_collect_id( f_name );
			MesNum = COLLECT_ID;
	};
	end_check_pool();
	append_idx_data();		/* catlog.c */
}

static	void
makeidx_from_catlog_file_idx( const char *f_name , const char *ff_name )
{
	if ( strcmp( ff_name , IDX_FILE_NAME ) == 0 ) {
		remove( f_name );
	};
}

/************************************************************************/
/* CATLOGで整理したファイルから新しい形式のインデックス・ファイルを作る */
/************************************************************************/
void	makeidx_from_catlog_file_main()
{
	printf("\nCATLOGで整理したファイルからインデックス・ファイルを作ります。"
			"\nしばらくお待ち下さい ………… "
	);

	/* まず、インデックス・ファイルを削除する */
	catlog_findfirst_mes_sw = NO;
	catlog_findfirst( OutDir, makeidx_from_catlog_file_idx, YES, YES );

	if ( max_catlog == 0 ) {
		catlog_malloc();
	} else {
		while ( ( catlog = (struct CATLOG *)malloc(
				 (size_t)max_catlog * sizeof(struct CATLOG)
			) ) == NULL
		) {
			max_catlog -= 100;
			if ( max_catlog < 1000 ) {
				printf("\nインデックスのための領域を確保できません。\n"
					"メモリーを増設して下さい。"
				);
				error_end();
			};
		};
	};

	catlog_findfirst_mes_sw = NO;
	catlog_findfirst( OutDir, makeidx_from_catlog_file, YES, YES );

	/* 確保した領域を解放する */
	free( catlog );

	printf("終了しました。");
}

/************************/
/* 旧タイプ → 新タイプ */
/************************/
static	void
makeidx_new_type_loop_sub( const char *file_path , const char *f_name )
{
	int		sd , num , line , seek_ichi , comment;
	char	date[ 128 ] , id[ 128 ] ;
	char	*p;
	FILE	*fpi,*fpo;

	/*********************/
	/* f_name のチェック */
	/*********************/
	strcpy( date , f_name );	p = (char *)last_comma( date );	*p = '\0';
	if ( strcmp( f_name , IDX_FILE_NAME ) == 0 ) {
		/* 新しい名前のインデックス */
	} else if ( matchstr( f_name , "MES%%_%%" ) == FALSE ) {
																return;
	} else if ( isIDname( date ) == YES ) {
		/* 発言の収集 or Home Party or Mail */
	} else if ( strcmp( date , "BILL" ) == 0 ) {
		/* 課金情報 */
	} else {
		/* 他のプログラムのインデックス？ */
		return;
	};

	/* 「全体で１つ」のインデックス・ファイル？ */
	strcpy( file_name , OutDir );	strcat( file_name , IDX_FILE_NAME );
	if ( strcmp( file_name , file_path ) == 0 )	return;

	if ( (fpo=fopen( file_name , "a" ) ) == NULL ) {
		error_open_file( "makeidx_new_type_loop_sub" );
	};

	/* 旧タイプのインデックスであるので処理を加える */
	if ( (fpi=fopen( file_path , "r" ) ) == NULL ) {
		strcpy( file_name , file_path );
		error_open_file( "makeidx_new_type_loop_sub" );
	};
	while( fgets( str , LINE , fpi ) != NULL ) {
		/* 旧タイプのインデックス情報が書かれている？ */
		sd = sscanf( str , IDX_FILE , 
			&num,		/* MES番号 HP MAIL */
			&line,		/* 発言番号 */
			 date,		/* 日時 */
			 id,		/* FORUM名 HP名 ID名 */
			&seek_ichi,	/* シーク位置 */
			&comment	/* コメント番号 */
		);
		if ( sd != EOF && sd != 5 && sd != 6 )	break;
		if ( sd == 5 ) {	without_crlf( str );	strcat( str , " 0\n" );	};
		fprintf( fpo , "%s" , str );
	};
	fclose( fpo );	fclose( fpi );

	/************************/
	/* ファイルを消すよ〜〜 */
	/************************/
	remove( file_path );
}

/********************************/
/* 旧タイプのインデックスを探す */
/********************************/
static	void	makeidx_new_type_sub( const char *f_name, const char *ff_name )
{
	const	char	*p;

	p = last_comma( ff_name );
	if (	p != ff_name
	&&		(	matchstr( p , ".IDX" ) == TRUE
			||	matchstr( p , ".ID%" ) == TRUE
			)
	) {
		/* 旧タイプのインデックスである */
		makeidx_new_type_loop_sub( f_name , ff_name );
	};
}

/********************************************************************/
/* IDX がないので、新しく作る の メイン・ルーチン                   */
/* 出力ﾃﾞｨﾚｸﾄﾘの下のｻﾌﾞ･ﾃﾞｨﾚｸﾄﾘの中の旧タイプのｲﾝﾃﾞｯｸｽ･ﾌｧｲﾙから作る */
/********************************************************************/
void	makeidx_new_type()
{
	catlog_findfirst_mes_sw = NO;
	catlog_findfirst( OutDir, makeidx_new_type_sub, YES, YES );
}

/***********************************************************/
/* IndexMode の値を調べてその値とファイルとをチェックする */
/***********************************************************/
void	check_index_mode()
{
	int		fsize;

	if ( IndexMode == 1 ) {
		/****************************/
		/* 全体で１つのインデックス */
		/****************************/
		strcpy( file_name , OutDir );
		strcat( file_name , IDX_FILE_NAME );
		fsize = _get_file_size( file_name );
		if ( fsize <= 0 ) {
			mess_check printf("\nインデックス・モードを変更します　→　");
			makeidx_new_type();
			mess_check printf("終了しました。");
		};
	} else {
		/********************************/
		/* フォーラムごとのインデックス */
		/********************************/
		makeidx_forum_mode();
	};
}

/* DIVIDE_END */
