/*
	h3.c
	フリーウェアコレクション コマンドモード ヘルパー
	Version 0.05 writen by calo.
	1994/03/01 1994/08/20 1995/02/20
*/

#define PROGRAM "fswch"
#ifdef TOWNS
#define VIRSION "\n\tVersion 0.05t  タウンズ専用版 1995/02/20 CopyLeft calo.\n"
#else
#define VIRSION "\n\tVersion 0.05r  MS-DOS汎用版 1995/02/20 CopyLeft calo.\n"
#endif
#define USAGE "\tFree SoftWare Collection helper for ms-dos\n"\
	"\tusage: "PROGRAM" [path|filename] [+n1,n2,...] [-n3,n4,...]\n"\
	"\t\t<n:キーワード番号 +:選択 -:否定選択 >"

#define MAIN
#include <stdlib.h>
#include "fswch.h"

/* 外部関数 */
char *dispprog( int nprog );		/* 作品名の表示 */
void lookfile( char cm,int no );	/* ファイルの表示 */
void dispkeyword( void );		/* キーワード表示 */
int chekkeyword( void );		/* 作品選択チェック */
void setkeyword( void );		/* 選択キーワード設定 */
void setkf( char *s );			/* キーフラグにセット */
void instl( int no );			/* インストール用バッチ制作 */
int sel_menu( int w, int firstline, char *word[] ); /* 選択メニュー */

/* 外部 文字列関数 */
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 *);	/* 文字列のコピー */
char *strcat( char *a,const char *b);	/* 文字列の連結 */

const char KW_SEP[] = " :\t\r\n";

void help( char *s );			/* 使い方 & exit(1) */

void main( int argc, char *argv[] ){
	int i,cpro;

	/* 変数の初期化 */
	for(i=0;i<PROG_MAX;i++){
		selprog[i]=-1;
	}
	for(i=0;i<KEY_MAX;i++){
		keyword[i][0]=NULL;
		skey[i]=OFF;
	}
	pg = getenv("PAGER"); /* 環境変数 PAGER の取得 */

	/* 引数の処理 */
	ckey = 0;
	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]);
			}
	
	/* 初期化 */
#ifdef TOWNS
	/* padinit(); */
	/* COM を off TRG を on に セットする */
#define PADCTRL 0x04d6
	outp( PADCTRL, 0x0f );
#undef PADCTRL
#endif
	curoff;

	{
		char line[85],*str[6];
		int i,j,c;
		fpos_t ptr;

		/* helper.idxを一度全部読む */
		cputs("helper.idx 読み込み中\r\n");

		/* keyword の取得 */
		kno = 0;
		do{ /* 1文字目が K であるあいだ */
			fgets(line,84,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,84,idxfp );
			switch (line[0]) {
			case 'P' : /* プログラムタイトル */
				/* 位置を記憶 */
				idxptr[++ino] = ptr;
				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)
						idxpkey[ino][j]=ON;
				break;
			default:
				break;
			} /* switch (line[0]) */
		} /* while( !feof(idxfp)) */

		/* 作品選択チェック */
		cpro = chekkeyword();
	}

	/* メインループ */
	{
	/* コマンド選択 */
	int cm=-1;
	char cmf = ON;
	char *cmd[] = {
		"キーワードの設定",
		"作品の選択",
		"概要ファイルの表示",
		"ドキュメントの表示",
		"作品インストール",
		"ヘルパー終了",
		""
	};
		while(cmf){
		char pbf[SELPRG_MAX][85],*pbfp[SELPRG_MAX+1],pbfe[]="";
		for(i=0;i<SELPRG_MAX;i++) pbfp[i]=pbf[i];
		pbfp[SELPRG_MAX+1]=pbfe;
		/* キーワード表示 */
		dispkeyword();
		cprintf("\r\n--- 全部で %d本の作品があります。 ---\r\n",ino+1);
		/* 選択された */
		if(cpro>0)
			cprintf("---  %d本の作品が選択されました。---",
				cpro);
		else
			cputs("--- 選択はありません。---");
		cputs("\r\n\n");
		if(cm>=0){ /* 現在選択中の 作品 */
			dispprog(selprog[cm]);
		} else cputs(ESC"2K");
		
		switch (sel_menu(3,14,cmd)) { /* コマンド選択 */
		case 0:/* キーワードの設定 */
		/* セットキーワード */
		setkeyword();
		/* 作品選択チェック */
		cpro = chekkeyword();
		cm = -1;
			break;
		case 1:/* 表示 プログラムの選択 */
			cputs("\r\n\n");
			if(cpro<1){
				cputs("作品の選択がありません。Hit anykey.");
				getop();
				break;
			}
			for( i = 0 ; i<cpro; i++){
				/* if(selprog[i]<0) break; */
				if( i!=0 && (i%SELPRG_MAX)==0 ){
					/* if((i+1)==cpro) break; */

				cprintf(ESC_GREEN
				"--- more program 実行orRET:sellect ---"
				" あと %d 作品あります。\r\n",cpro-i);

					/* RET or 実行 key */
					if(instr(getop(),"\x0d\x12")){
						break;
					}
					/*  cputs(ESC"2K\r"); */
				}
				strcpy(pbf[i%SELPRG_MAX],dispprog(selprog[i]));
			}
			i--;
			pbf[(i%SELPRG_MAX)+1][0]=NULL;
			clstext();
			cputs(ESC_SKYBULE"========= 作品選択 ==========\r\n");
			if((cm = sel_menu(1,3,pbfp))<0) break;
			cm = cm+(i/SELPRG_MAX)*SELPRG_MAX;

		case 2:/* 概要ファイルの表示 */
			cputs(ESC"22;0;f");
			lookfile('g',selprog[cm]);
			break;
		case 3:/* ドキュメントの表示 */
			cputs(ESC"22;0;f");
			lookfile('d',selprog[cm]);
			break;
		case 4:/* copy */
			instl(selprog[cm]);
			break;
		case 5:/* 終了 */
		default:
			{
			char *endp[]={ "終了する","続ける","" };
			cmf = ( sel_menu(2,18,endp)==0 ) ? OFF : ON ;
			}
			break;
		} /* switch */
		} /* while */
	} /* メインループ */

	/* 後始末 */
	clstext();
	fclose(idxfp);
	for(i=0;i<kno;i++) skey[i]=SELLECT;
	dispkeyword();
	putch('\r');
	cputs(argv[0]);
	cputs(" 終了");
	curon;
}

void help( char *s ){ /* 使い方 & 終了 */
	fputs( s, stderr );
	fputs( VIRSION, stderr );
	fputs( USAGE, stderr );
	exit(1);
}
