#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <snd.h>
#include <winb.h>
#include <te.h>
#include <fntb.h>
#include <gui.h>
#include <fmcfrb.h>
#include <egb.h>
#include "sftlib.h"


PROTO int DB_init( char *optfile )
{
    extern int    DB_menuI[] ;
	int ret;

	DB_work_open( "システムの初期処理を行っています", "おまちなさって");
	EGB_getPalette( 0, palette0_old);
	EGB_getPalette( 1, palette1_old);
	DB_work_put( "環境ファイルを読み込んでいます" );
	ret=DB_readopt( optfile );
	if( ret!=0 ) ret=DB_readopt( "sftlib.sft" );
	if( ret!= 0 ){
		AMI_PATH_t ps;
		char path[_MAX_PATH];
		AMI_pathsplit( &ps, DB_startp);
		strcpy(path, ps.drive);
		strcat(path, ps.dir);
		strcat(path, "sftlib.sft");
		ret=DB_readopt( path );
		if( ret!=0 ) {
			DB_work_put( "環境ファイルが読み込めませんでした" );
			DB_work_put( path );
			/*DB_work_close();*/
			return -1;
		}
	}

    DB_readcfg( DB_sftcfgp );
    MMI_SetUpSDKMenu( DB_menuI[1], -1 );  /* サイドワークメニュー設定 */
	DB_work_put( "データを読み込んでいます" );
	DB_indexRead();
	DB_work_put( "リストを作っています" );
	DB_MakeList();         /* リスト作成   */
    KYB_init();        /* キーボード初期化 */
	DB_work_close();
    DB_base_dsp();         /* 基本画面表示 */
	DB_showData();         /* roots.c */ 
    return 0;
}

PROTO int DB_end( void )
{
  switch( DB_sftcfgm ){
    case 0:   break;
    case 1:
             DB_writecfg( DB_sftcfgp );
             break;
    default:
             if( DB_YESNO_ALERTCall("環境ファイルを保存しますか\n")==0 ) 
                 DB_writecfg( DB_sftcfgp );
             break;
  }
  if( DB_dataary ) AMI_free(DB_dataary);
  return 0;
}

static int getval_sub( FILE *fp, char *str, int len)
{
	char buf[512];
	getstr(fp, buf, sizeof(buf));
	getstr(fp, str, len);
	DB_schch( fp, ';' );
	return 0;
}

static int getval_int( FILE *fp, int *n )
{
	char buf[512];
	getstr(fp, buf, sizeof(buf));
	getstr(fp, buf, sizeof(buf));
	*n=DB_atoi(buf);
	DB_schch( fp, ';' );
	return 0;
}

static int getval_ary( FILE *fp, char *ary, int len)
{
	char buf[512];
	int i,n;

	DB_schch(fp, '[');
	getstr(fp, buf, sizeof(buf));
	n=DB_atoi( buf);
	DB_schch(fp, '{');
	for(i=0; i< n; i++) {
		getstr(fp, &ary[i*len], len);
        DB_schstrch(fp, ",}" );
	}
	DB_schch( fp, ';' );
	return 0;
}

static getval_plt( FILE *fp, int page, char *plt )
{
	char buf[512];
	int i,n;

	DB_schch(fp, '[');
	getstr(fp, buf, sizeof(buf));
	n=DB_atoi(buf);
	DWORD(plt)=n;
	DB_schch(fp, '{');
	for(i=0; i< n; i++) 
	{
		getstr(fp, buf, sizeof(buf) );
		DWORD(plt+ 4+i*8)=DB_atoi(buf);   /* Pllette Number */
		getstr(fp, buf, sizeof(buf));    /*  Blue      */    
		plt[ 8+i*8]=DB_atoi(buf);   
		getstr(fp, buf, sizeof(buf));    /*  Red       */
		plt[ 9+i*8]=DB_atoi(buf);   
		getstr(fp, buf, sizeof(buf));    /* Green      */
		plt[10+i*8]=DB_atoi(buf);   

		BYTE(plt+11+i*8)=0;  
	}
	DB_schch( fp, ';' );

	EGB_writePage( guiEgbPtr, page );
	EGB_palette( guiEgbPtr, 1, plt);
	EGB_writePage( guiEgbPtr, DB_page );
	return 0;
}


PROTO  /* 環境ファイルから search read*/
PROTO int getval_search( FILE *fp, search *sch )
{
	char buf[512];

	DB_schch(fp, '{');
	getstr(fp, buf, sizeof(buf));    
    	sch->code0bit=  DB_atoi(buf);     /* 管理コード0         */
	getstr(fp, buf, sizeof(buf));    
    	sch->code1bit=  DB_atoi(buf);     /* 管理コード1         */
	getstr(fp, buf, sizeof(buf));    
    	sch->code2bit=  DB_atoi(buf);     /* 管理コード2         */
	getstr(fp, buf, sizeof(buf));    
    	sch->code3bit=  DB_atoi(buf);     /* 管理コード3         */
	getstr(fp, buf, sizeof(buf));    
    	sch->sortbit=  DB_atoi(buf);      /* 分類コード          */
	getstr(fp, buf, sizeof(buf));    
    	sch->mediabit= DB_atoi(buf);      /* メディア            */
	getstr(fp, buf, sizeof(buf));    
	    sch->name1bit= DB_atoi(buf);      /* タイトルの1文字     */
	getstr(fp, buf, sizeof(buf));    
    	sch->name2bit= DB_atoi(buf);      /* サブタイトルの1文字 */
	getstr(fp, buf, sizeof(buf));    
   		sch->makerbit= DB_atoi(buf);      /* メーカの1文字       */
	getstr(fp, buf, sizeof(buf));    
    	sch->aboutbit=DB_atoi(buf);       /* 概要                */
	getstr(fp, buf, sizeof(buf));    
    	sch->infombit=DB_atoi(buf);       /* 必須･対応周辺機器   */
	getstr(fp, buf, sizeof(buf));    
    	sch->teikad=   DB_atoi(buf);      /* 定価下              */
	getstr(fp, buf, sizeof(buf));    
    	sch->teikau=   DB_atoi(buf);      /* 定価上              */
	getstr(fp, buf, sizeof(buf));    
    	sch->ramd=     DB_atoi(buf);      /* 必要メモリ下        */
	getstr(fp, buf, sizeof(buf));    
    	sch->ramu=     DB_atoi(buf);      /* 必要メモリ上        */
	getstr(fp, buf, sizeof(buf));    
    	sch->hdd=      DB_atoi(buf);      /* 必要ハードディスク下    */
	getstr(fp, buf, sizeof(buf));    
	 	sch->hdu=      DB_atoi(buf);     /* 必要ハードディスク上    */
	DB_schch( fp, ';' );

	set_DB_search( ); /* GUIに設定 */
	return 0;
}

/* search を環境ファイルに書き込む */
PROTO int setval_search( FILE *fp, search *sch )
{
	fprintf( fp, "DB_search={   # 検索する条件 \n");
	fprintf( fp, "  0x%08x  # 管理コード0         \n", sch->code0bit);    
	fprintf( fp, "  0x%08x  # 管理コード1         \n", sch->code1bit);    
	fprintf( fp, "  0x%08x  # 管理コード2         \n", sch->code2bit);    
	fprintf( fp, "  0x%08x  # 管理コード3         \n", sch->code3bit);    
	fprintf( fp, "  0x%08x  # 分類コード          \n", sch->sortbit);    
	fprintf( fp, "  0x%08x  # メディア            \n", sch->mediabit);    
	fprintf( fp, "  0x%08x  # タイトルキー        \n", sch->name1bit);    
	fprintf( fp, "  0x%08x  # サブタイトルキー    \n", sch->name2bit);    
	fprintf( fp, "  0x%08x  # メーカキー          \n", sch->makerbit);    
	fprintf( fp, "  0x%08x  # 概要キー            \n", sch->aboutbit);  
	fprintf( fp, "  0x%08x  # 必須･対応周辺機器   \n", sch->infombit);  
	fprintf( fp, "  %10d  # 定価下              \n", sch->teikad );    
	fprintf( fp, "  %10d  # 定価上              \n", sch->teikau );    
	fprintf( fp, "  %10d  # 必要メモリ下        \n", sch->ramd );    
	fprintf( fp, "  %10d  # 必要メモリ上        \n", sch->ramu );    
	fprintf( fp, "  %10d  # 必要ハードディスク下\n", sch->hdd );    
	fprintf( fp, "  %10d  # 必要ハードディスク上\n", sch->hdu );    
	fprintf( fp, "};\n\n" );
	return 0;
}


int	DB_readopt( char *optfile )
{
	FILE *fp;
	char val[512];
	int mos;
	
	fp=fopen( optfile, "rb");
	if( fp==NULL ) return -1;

	MG_PushPtr( 64 , &mos ) ;
	while( getstr(fp, val, sizeof(val))>=0 ){
		if( val[0]=='#' ) DB_schch( fp, '\n' );
		else if( strcmp( val,  "DB_sftlibp") ==0 ) 
				getval_sub( fp, DB_sftlibp, sizeof(DB_sftlibp) );
		else if( strcmp( val,  "DB_sftcfgp") ==0 ) 
				getval_sub( fp, DB_sftcfgp, sizeof(DB_sftcfgp) );
		else if( strcmp( val,  "DB_sftcfgm") ==0 ) {
				getstr(fp, val, sizeof(val));    
				getstr(fp, val, sizeof(val));    
				DB_sftcfgm=DB_atoi( val );
				DB_schch(fp, ';');
		}
		else if( strcmp( val,  "DB_indexp") ==0 ) 
				getval_sub( fp, DB_indexp, sizeof(DB_indexp) );
		else if( strcmp( val,  "DB_docdir") ==0 ) 
				getval_sub( fp, DB_docdir, sizeof(DB_docdir) );
		else if( strcmp( val,  "DB_tifflp") ==0 ) 
				getval_sub( fp, DB_tifflp, sizeof(DB_tifflp) );
		else if( strcmp( val,  "bunrui_mes") ==0 ) 
				getval_ary( fp, (char*)bunrui_mes, sizeof(bunrui_mes[0]) );
		else if( strcmp( val,  "media_mes") ==0 ) 
				getval_ary( fp, (char*)media_mes, sizeof(media_mes[0]) );
		else if( strcmp( val,  "date_mes") ==0 ) 
				getval_ary( fp, (char*)date_mes, sizeof(date_mes[0]) );
		else if( strcmp( val,  "DB_help0p") ==0 ) 
				getval_sub( fp, DB_help0p, sizeof(DB_help0p) );
		else if( strcmp( val,  "DB_help1p") ==0 ) 
				getval_sub( fp, DB_help1p, sizeof(DB_help1p) );
		else if( strcmp( val,  "DB_help2p") ==0 ) 
				getval_sub( fp, DB_help2p, sizeof(DB_help2p) );
		else if( strcmp( val,  "DB_help3p") ==0 ) 
				getval_sub( fp, DB_help3p, sizeof(DB_help3p) );
		else if( strcmp( val,  "DB_help4p") ==0 ) 
				getval_sub( fp, DB_help4p, sizeof(DB_help4p) );
	}

	MG_PopPtr( mos ) ;
	fclose(fp);
	return 0;
}

int	DB_readcfg( char *cfgfile )
{
	FILE *fp;
	char val[512];
	int mos;
	extern char *sftlibp;
	
	fp=fopen( cfgfile, "rb");
	if( fp==NULL ) {
		char buf[512];int ret;
		sprintf( buf, "%sファイルがよみこめませんです。\n"
			"%sファイルの sftcfgp項目がおかしいかもよ\n"
			"しょりをつづけますか\n",
			cfgfile, sftlibp );
		ret=DB_YESNO_ALERTCall( buf );
		if( ret!=0 ) 	MMI_SetHaltFlag(True);
        return -1;
	}

	MG_PushPtr( 64 , &mos ) ;
	while( getstr(fp, val, sizeof(val))>=0 ){
		if( val[0]=='#' ) DB_schch( fp, '\n' );
		else if( strcmp( val,     "palette0") ==0 ) 
				getval_plt( fp, 0, palette0 );
		else if( strcmp( val,     "palette1") ==0 ) 
				getval_plt( fp, 1, palette1 );
		else if( strcmp( val,     "DB_search") ==0 ) 
				getval_search( fp, &DB_search );
		else if( strcmp( val,     "DB_lstmode") ==0 ) 
				getval_int( fp, &DB_lstmode );
		else if( strcmp( val,     "DB_pmodec1") ==0 ) 
				getval_int( fp, &DB_pmodec1 );
		else if( strcmp( val,     "DB_cndmode") ==0 ) 
				getval_int( fp, &DB_cndmode );
		else if( strcmp( val,     "DB_sortmode") ==0 ) 
				getval_int( fp, &DB_sortmode );
		else if( strcmp( val,     "DB_sortrev") ==0 ) 
				getval_int( fp, &DB_sortrev );
	}

	if( DB_lstmode<0 || DB_lstmode>10 ) DB_lstmode=1;
	MG_PopPtr( mos ) ;

	fclose(fp);
	return 0;
}


int DB_indexRead()
{
	FILE *fp;
	int len, mos, ret;

	DB_datamax=DB_datanum=DB_datalnm=0;
	fp=fopen( DB_indexp, "rb");
	if( fp==NULL ) {
		char buf[512];
		sprintf( buf, "%s がオープンできませんでした\n続けますか", DB_indexp);
		ret=DB_YESNO_ALERTCall(buf);
		if( ret!=0 ) 	MMI_SetHaltFlag(True);
		else  {
			DB_work_open( "インデックスを作成中","しばらく待っててね");
			DB_mkind();
			DB_work_close();
			fp=fopen( DB_indexp, "rb");
			if( fp==NULL ) return 1;
		}
	}

   	MG_PushPtr( 64 , &mos ) ;
	len=DB_filelength( fp );
	if( len <=0 ) { ret=-2; goto RETURN;}
	DB_datamax=len/sizeof(index);
	DB_dataary=AMI_malloc( len );
	if( DB_dataary==NULL) { ret=-3; goto RETURN;}
	DB_datanum=fread( DB_dataary, sizeof(index), DB_datamax, fp );
RETURN:
	fclose(fp);
   	MG_PopPtr( mos ) ;
	return ret;
}



int DB_base_dsp()
{
    extern int DB_root, DB_data;
    extern int DB_scond_TM[];
    extern int DB_scond_TM2[];
	extern int DB_mdfy_mdaM[];
	extern DB_data_UdateM[];
	extern DB_mdfy_Alert_tM[];
	extern AMI_PTGL_tm[];
    int i;

    for( i=0; i< BUNRUI_NUM; i++) {
        MMI_SendMessage( DB_scond_TM[i], MM_SETMSG, 1, &bunrui_mes[i][0] );
    }
    for( i=0; i< MEDIA_NUM; i++) {
        MMI_SendMessage( DB_scond_TM2[i], MM_SETMSG, 1, &media_mes[i][0] );
        MMI_SendMessage( DB_mdfy_mdaM[i], MM_SETMSG, 1, &media_mes[i][0] );
    }
	for( i=0; i< 6; i++) {
        MMI_SendMessage( DB_data_UdateM[i], MM_SETMSG, 1, &date_mes[i][0] );
        MMI_SendMessage( DB_mdfy_Alert_tM[i], MM_SETMSG, 1, &date_mes[i][0] );
        MMI_SendMessage( AMI_PTGL_tm[2+i], MM_SETMSG, 1, &date_mes[i][0] );
	}

    MMI_SendMessage( DB_root, MM_ATTACH, 1, DB_base);
    MMI_SendMessage( DB_root, MM_SHOW, 0);
    MMI_SendMessage( DB_data, MM_ATTACH, 1, DB_base);
    MMI_SendMessage( DB_data, MM_SHOW, 0);
    return 0;
}



int  DB_writecfg( char *cfgfile )
{
	FILE *fp;
	int i, mos;
	
    DB_work_open( " 環境ファイルを保存しています", "しばらくお待ちください");
	fp=fopen( cfgfile, "wb");
	if( fp==NULL ) {
	    DB_work_close();
		return -1;
	}
   	MG_PushPtr( 64 , &mos ) ;
	EGB_getPalette( 0, palette0);

	fprintf(fp, "DB_lstmode =%d;# リストの段組数    \n", DB_lstmode);
	fprintf(fp, "DB_pmodec1 =%d;# 1:管理コードを表示する\n", DB_pmodec1);
	fprintf(fp, "DB_cndmode =%d;# 0:検索条件を無効にする\n\n", DB_cndmode);
	fprintf(fp, "DB_sortmode=%d;# ソートの種類     \n", DB_sortmode);
	fprintf(fp, "DB_sortrev =%d;# -1:逆にソートする\n\n", DB_sortrev);

	fprintf(fp, "palette0[16]={ # ページ0のパレット\n");
	for( i=0; i<16; i++){
		fprintf(fp, "    %3d 0x%02x 0x%02x 0x%02x\n", i, 
			palette0[i*8+8],palette0[i*8+9], palette0[i*8+10]);
	}
	fprintf( fp, "};\n" );

	EGB_getPalette( 1, palette1);
	fprintf(fp, "palette1[16]={ # ページ1のパレット\n");
	for( i=0; i<16; i++){
		fprintf(fp, "    %3d 0x%02x 0x%02x 0x%02x\n", i, 
			palette1[i*8+8],palette1[i*8+9], palette1[i*8+10]);
	}
	fprintf( fp, "};\n\n" );

	setval_search( fp, &DB_search );

	fclose( fp );
   	MG_PopPtr( mos ) ;
    DB_work_close();
  return 0;
}


PROTO int DB_mkind(void)
{
	FILE *fpr, *fpw;
	soft data;
	index ind; int n;
	int   ret;
	
	ret=0;
	fpr=fopen( DB_sftlibp ,"rb");
	if( fpr==NULL) { ret=1; goto RET0; }
	fpw=fopen( DB_indexp, "wb");
	if( fpw==NULL) { ret=2; goto RET1;}

	n=0;
	while( fread( &data, sizeof(data), 1, fpr )==1 ) 
	{
		data2ind( &data, &ind );
		ind.softno=n;n++;
		ret=fwrite( &ind, sizeof(ind),1,fpw);
		if( ret<0) {ret=3;break;}
	}
	fclose(fpw);
RET1:
	fclose(fpr);
RET0:	
	return ret;
}

