/*
	h.c
*/

#define PROGRAM "fschlp"
#define USAGE "\n\tFree SoftWare Collection helper for ms-dos\n"\
	"\tusage: "PROGRAM" [path|filename] [+n1,n2,...] [-n3,n4,...]\n"\
	"\t\t<n:キーワード番号 +:選択 -:否定選択"

#include <stdio.h>
#include <stdlib.h>

#define KEY_MAX 32
#define PROG_MAX 1000
#define ON 1
#define OFF 0
#define SELLECT 1
#define NONSELLECT 2

#define ESC		"\33["
#define ESC_WHITE	ESC"0m"
#define ESC_YELLOW	ESC"33m"
#define ESC_SKYBULE	ESC"36m"
#define ESC_MAZENTA	ESC"35m"
#define ESC_GREEN	ESC"32m"
#define ESC_RED		ESC"31m"
#define ESC_BULE	ESC"34m"

unsigned char skey[KEY_MAX]; /* keyword 選択フラグ */
signed int ckey = 0; /* keyword 選択カウンタ */

struct {
	fpos_t ptr;
	unsigned char k[KEY_MAX];
} index[PROG_MAX];
/* /    int fgetpos(FILE *fp, fpos_t *ptr); */
/* /    int fsetpos(FILE *fp, const fpos_t *ptr); */

char keyword[PROG_MAX][16];
int kno,ino; /* keyword_max index_max */
const char kw_sep[]=" :\t\r\n"; /* keyword separater */
FILE *idxfp;

void help( char *s ); /* 使い方 & 終了 */
void setkf( char *s ); /* キーフラグにセット */
int menu( int * );
void selkey();
void dispprog();
void selprog();
void lookfile();
int instr( char a, const char *); /* 文字列sの文字aの位置 */
int split( char *s, char **str, const char *sep); /* 文字の切り分け */
int strcmp( const char *,const char *); /* 文字列の比較 */
char *strcpy( char *,const char *); /* 文字列のコピー */

void main( int argc, char **argv ){
	int i;

	/* 変数の初期化 */
	for(i=0;i<PROG_MAX;i++) keyword[i][0]=NULL;
	for(i=0;i<PROG_MAX;i++) strcpy(keyword[i],"KEY-WORD");
	for(i=0;i<KEY_MAX;i++) skey[i]=OFF;

	/* 引数の処理 */
	if(argc>1){
		for(i=1;i<argc;i++) /* 全ての引数について */
		switch(argv[i][0]){ /* 一文字目が */
		case '-' :
			if(instr(argv[i][1],"Hh?")) help(argv[0]);
			setkf(argv[i]);
			break;
		case '+' :
			setkf(argv[i]);
			break;
		default :
			idxfp=fopen(argv[i],"r");
			break;
		}
	}
	if(idxfp==NULL)
		/* ファイルの指定がなければ カレントディレクトリから */
		if((idxfp=fopen("helper.idx","r"))==NULL)
			/* q:\helper.idx */
			if((idxfp=fopen("q:\\helper.idx","r"))==NULL){
				fputs("helper.idxが 見つからない",stderr);
				help(argv[0]);
			}
	
	/* 初期化 */
	{
		char line[85],*str[6];
		int i,j,c,cpro;
		fpos_t ptr;

		/* helper.idxを一度全部読む */

		/* keyword の取得 */
		kno = 0;
		do{ /* 1文字目が K であるあいだ */
			fgets(line,85,idxfp);
			c = split(line,str,kw_sep);
			for(i=1;i<c;i++){ /* str[0] はとばす */
				strcpy(keyword[kno++],str[i]);
			}
		}while(line[0]=='K');
		
		/* indexのセット */
		ino = -1 ; /* ino プリインクリメントのため */
		while( !feof(idxfp)){ /* ファイルの終わりまで */
			fgetpos( idxfp,&ptr ); /* 現在位置 */
			fgets( line,100,idxfp );
			switch (line[0]) {
			case 'P' : /* プログラムタイトル */
				/* 位置を記憶 */
				index[++ino].ptr = ptr;
				/*
			cprintf("ptr %8l:",ptr);
			cputs( line );
			cputs("\r");
			*/
				break;
			case 'K' : /* keyword */
				c = split(line,str,kw_sep);
				for(j=0;j<kno;j++)
				for(i=1;i<c;i++)
					if(strcmp(keyword[j],str[i])==NULL)
						index[ino].k[j]=ON;
				break;
			default:
				break;
			} /* switch (line[0]) */
		} /* while( !feof(idxfp)) */

		/* KEYWORD 表示 */
		for(cpro=0,i=0;i<kno;i++){
			for(j=0;j<ino;j++)
				if(index[j].k[i]==ON) cpro++;
			cputs( (skey[i]==SELLECT) ? ESC_WHITE :
				(skey[i]==NONSELLECT) ? ESC_YELLOW :
				ESC_RED );
			cprintf( "[%2d:%-10.10s%4d]",i,keyword[i],cpro);
			if(((i+1)%4)==0) cputs("\r\n");
		}
		cputs( ESC_WHITE );

		cprintf("\r\n---  %d本の作品があります。---\r\n",ino+1);
		
		/* DEBUG */
		if(argc>1) {
		cpro = 0;
		for(i=0;i<ino;i++){
			int cc;
			for(cc=0,j=0;j<kno;j++){
				if(index[i].k[j]!=ON) continue;
				if(skey[j]==SELLECT ){
					cc++;
				}else
				if(skey[j]==NONSELLECT){
					cc=0;
					break;
				}
			}
			if(cc==0 || cc!=ckey) continue;
			cpro++;
			fsetpos(idxfp,&(index[i].ptr));
			do{
			if(kbhit()&&getch()==0x1b) exit(1);
				fgets(line,85,idxfp);
				cputs(line);
				cputs("\r");
			}while(instr(':',line));
		}
		/* 選択された */
		cprintf("---  %d本の作品が選択されました。---\r\n",cpro);
		}
	}
	exit(1); /* DEBUG */

	/* メインループ */
	{
	int cm = ON;
		while(menu(&cm))switch (cm) {
		case 1:/* キーワードの選択 */
			selkey();
			break;
		case 2:/* 表示 */
			dispprog();
			break;
		case 3:/* プログラムの選択 */
			selprog();
			break;
		case 4:/* 概要ファイルの表示 */
			lookfile();
			break;
		case 5:/* ドキュメントの表示 */
			lookfile();
			break;
		case 6:/* copy */
			break;
		default:
			break;
		}
	}

	/* 後始末 */
}

void help( char *s ){ /* 使い方 & 終了 */
	fputs( s, stderr );
	fputs( USAGE, stderr );
	exit(1);
}

void setkf( char *s ){ /* キーフラグにセット */
	int c,i;
	char *str[10];
	c = split(s+1,str,",/");
	for(i=0;i<c;i++){
		int no;
		no=atoi(str[i]);
		if( no < 0 || no >= KEY_MAX ) continue;
		if( *s == '+' ){
			skey[no] = SELLECT ;
			ckey++;
		} else {
			skey[no] = NONSELLECT ;
		}
	}
}

int menu( int *cmd ){
}
void selkey(){
}
void dispprog(){
}
void selprog(){
}
void lookfile(  ){
}
