/*
	instl.c
	インストール用バッチ制作関数
	1994/03/03 1994/08/20
*/

#include "fswch.h"

int instr( char a, const char *);	/* 文字列sの文字aの位置 */
int strcmp( const char *,const char *);	/* 文字列の比較 */
char *strcpy( char *,const char *);	/* 文字列のコピー */
char *koumoku( char *k );		/* 項目とりだし */

const char fname[]="instl.bat";

void instl( int no ){/* インストール用バッチ制作 no:作品番号 */
	fpos_t ptr;
	FILE *fp;
	char cpf = OFF,*bp;
	static char buff[256],bdir[85];

	if( no < 0 ) return;
	
	cprintf("\r\n\n%s 作成\r\n",fname);
	/* instl.bat オープン */
	if((fp=fopen(fname,"a+"))==NULL){
		cprintf(ESC_RED"%s がオープン出来ない."
			" --- Hit anykey ---",fname);
		getop();
		return;
	}

	ptr = idxptr[no];
	fsetpos(idxfp,&(ptr));

	do{
		fgets( buff, 256,idxfp );
		bp = koumoku( buff );
		/*
		if((bp = koumoku( buff ))==NULL){
			fputs("項目エラー",stderr);
			return;
		}
		*/
		if(strcmp(buff,"PROGRAM")==NULL){
			fprintf(fp,"rem %s\n",bp);
			cprintf("rem %s\r\n",bp);
		}
		if(strcmp(buff,"COPY")==NULL){
			cpf = ON;
			fprintf(fp,"copy q:%s\n",bp);
			cprintf("copy q:%s\r\n",bp);
		}
		if(strcmp(buff,"DIR")==NULL){
			strcpy(bdir,bp);
		}
	}while(*buff!='\n' && *buff!=NULL);
	if(cpf==OFF){
		/* DIR */
		fprintf(fp,"copy q:%s\\*.*\n",bdir);
		cprintf("copy q:%s\\*.*\r\n",bdir);
	}
	fputs("pause\n\n",fp);
	fclose(fp);
	cputs("\r\n\n"ESC_GREEN"作成終了！ --- hit anykey. ---");
	getop();
}
