/*

8086|Printman/POSTCARD セットアップユーティリティ Version 1.00
Copyright (c) 1993,94 Delmonta

*/

#include<stdlib.h>
#include"dprint.h"

struct	DFLFILE	Dflfile = 
{
	PRNBIOS_TOWNS,PRNMODE_ESCP,	/* プリンタの機種設定 */
	300,850,			/* マージン */
	{PROP_LEFT,PROP_RIGHT,PROP_CENTER,PROP_CENTER,PROP_FLEFT}
};

enum	PRNBIOS	Prn_machine = PRNBIOS_TOWNS;
enum	PRNMODE	Prn_mode    = PRNMODE_ESCP;

/*---------------------------------------------------------------------------*/

static unsigned	ask_int(char *mes,unsigned max)
{
	char		buf[6];
	unsigned	a;

askint_rep:
	printf("\n\n%s:",mes);
	fgets(buf,sizeof(buf),stdin);

	a = atoi(buf);
	if	(a>max)
		goto askint_rep;

	return a;
}

/*---------------------------------------------------------------------------*/

static	bool	ask_yesno(char *mes)
{
	printf("%s(y/n)?",mes);
askyn_rep:
	switch	(getc(stdin))
	{
	case 'y':
	case 'Y':
		while	(getc(stdin)!='\n')
			;
		return TRUE;
	case 'n':
	case 'N':
		while	(getc(stdin)!='\n');
			;
		return FALSE;
	default:
		putchar('\a');
		goto askyn_rep;
	}
}

/*---------------------------------------------------------------------------*/

static	fract	ask_fract(char *mes)
{
	char		buf[8];

	printf("\n\n%s:",mes);

	

	fgets(buf,sizeof(buf),stdin);

	return dpatoi(buf);
}

/*---------------------------------------------------------------------------*/

static	void	testprint(void)
{
	while	(prn_init())
	{
		printf("プリンタの準備ができていません.リターンキーで再開します\n");
		while	(getc(stdin)!='\n')
			;
	}

	prn_putstr("■");		/* プリンタのエラーチェックは省略 */
	prn_formfeed();
}

/*---------------------------------------------------------------------------*/

int	main(int argc,char **argv)
{
	printf( "\n8086|Printman/POSTCARD セットアップユーティリティ Version 1.00\n"
		"Copyright (c) 1993,4 Delmonta\n");

	Dflfile.prn_machine = Prn_machine =(enum PRNBIOS)ask_int(
		"コンピュータの機種は?\n\n"
		"0...NEC PC-9801/9821シリーズ/セイコーエプソン PC-286/386/486シリーズ\n"
		"1...富士通 FMR/FM TOWNSシリーズ/松下電器 Panacom Mシリーズ\n"
		"2...IBM PC/AT互換機\n"
		"3...東芝 J-3100シリーズ/AX規格機\n\n",3);

	Dflfile.prn_mode = Prn_mode = (enum PRNMODE)ask_int(
		"プリンタの機種は?\n\n"
		"0...ESC/P24-J84規格準拠\n"
		"1...MSX規格準拠漢字プリンタ\n"
		"2...NEC PC-PR201H/101H互換\n"
		"3...富士通 FMPRシリーズプリンタ\n\n",3);

	if	(ask_yesno("テスト印刷をしますか"))
		testprint();

	Dflfile.umrgin = ask_fract("プリンタの物理上マージンは[mm]?");
	Dflfile.lmrgin = ask_fract("プリンタの物理左マージンは[mm]?");

	if	(ask_yesno("定義ファイルに書き込みますか"))
	{
	static	char	dflheader[] =   "8086|Printman/POSTCARD デフォルト設定"
					"ファイル Version 1.00\x1a";

		FILE	*fp = fopen("DPRINT.DFL","wb");

		if	(fp==NULL)
		{
			printf("ファイルがオープンできません.\n");
			exit(1);
		}
						/* 最後の'\0'は書き込まない */
		fwrite(dflheader,1,sizeof(dflheader)-1,fp);
		fwrite(&Dflfile,sizeof(struct DFLFILE),1,fp);
		fclose(fp);
	}

	exit(0);
}
