#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>
#include    <ctype.h>
#include    <mos.h>
#include    "scrn.h"
#include    "keyword.h"
#include    "graphic.h"
#include    "dir.h"
#include    "file.h"
#include    "oaklib.h"
#include    "coldef.h"

#define	DEBUG

/*******
char	*strdup(char *str);
********/

extern char	*crent_drive;

       char	index_file[128];
       int	cur_x=0;
       int	cur_y=0;
static int	kb_act=0;
static int 	kb_flg=0;
static int 	ch_bak=0;
static unsigned ec_bak=0;

void	put_put(int x,int y,int len,int pos,char *str,char *att)
{
    int     i,col;
    char    tmp[4];
    static int kan_col[]={ 15,1,2,3,4,5,6,7,0,9,10,11,12,13,14,8 };

    for ( i = 0 ; i < len ; ) {
	col = kan_col[*att];
	if ( i == pos )
	    col |= 8;

	if ( iskanji(*str) && iskanji2(*str+1) ) {
	    tmp[0] = *(str++);
	    tmp[1] = *(str++);
	    tmp[2] = '\0';
	    if ( (x + 16) > 640 ) {
		x = 0;
		y += 16;
	    }
	    gputs(x,y,15,col,tmp);
	    x += 16;
	    att += 2;
	    i += 2;
	} else {
	    tmp[0] = *(str++);
	    tmp[1] = '\0';
	    if ( (x + 8) > 640 ) {
		x = 0;
		y += 16;
	    }
	    gputs(x,y,15,col,tmp);
	    x += 8;
	    att++;
	    i++;
	}
    }
}
void	putmode(int md,int sf,char *str)
{
    static char *sts_str=NULL;

    if ( str != NULL )
	sts_str = str;

    if ( sts_str != NULL && kb_act != 0 )
	gputs(560,460,XPRG_COL,PRG_COL,sts_str);
}
void	putsys(int len,char *str,char *att)
{
    static BLOCK *save=NULL;

    if ( save != NULL ) {
	DSP_pop_vram(save);
	save = NULL;
    }

    if ( len > 0 ) {
        save = DSP_push_vram(8,460,len*8+8,479);
	put_put(8,460,len,256,str,att);
    }
}
void	putcnv(int pos,int len,char *str,char *att)
{
    static BLOCK *save1=NULL;
    static BLOCK *save2=NULL;
    int     n;

    if ( save1 != NULL ) {
	DSP_pop_vram(save1);
	save1 = NULL;
    }
    if ( save2 != NULL ) {
	DSP_pop_vram(save2);
	save2 = NULL;
    }

    if ( len > 0 ) {
	if ( (n = cur_x+len*8) > 640 ) {
	    n -= (624);
	    save1 = DSP_push_vram(cur_x,cur_y,639,cur_y+15);
	    save2 = DSP_push_vram(0,cur_y+16,n,cur_y+31);
	} else
	    save1 = DSP_push_vram(cur_x,cur_y,n,cur_y+15);
	put_put(cur_x,cur_y,len,pos,str,att);
    }
}
int	PRG_clic(int no)
{
    int     i,n;
    int     fg;
    char    tmp[256];

    if ( prg_tbl[no] == NULL )
	return (no + PRG_NO);

    if ( prg_tbl[no]->dir != NULL ) {
	DSP_mos(2);
	if ( prg_tbl[no]->manual == NULL ) {
	    if ( (prg_tbl[no]->manual = 
			file_serch(prg_tbl[no]->dir,"*.RRR")) == NULL ) {
	        if ( (prg_tbl[no]->manual = 
			    file_serch(prg_tbl[no]->dir,"*.MAN")) == NULL )
		    prg_tbl[no]->manual = 
			        file_serch(prg_tbl[no]->dir,"*.DOC");
	    }
	}
	if ( prg_tbl[no]->readme == NULL ) {
	    if ( (prg_tbl[no]->readme = 
			file_serch(prg_tbl[no]->dir,"*.GGG")) == NULL )
		prg_tbl[no]->readme = 
			    file_serch(prg_tbl[no]->dir,"*.DOC");
	}
	if ( prg_tbl[no]->copycnt == 0 ) {
	    if ( (prg_tbl[no]->copy[0] = 
			file_serch(prg_tbl[no]->dir,"*.QQQ")) == NULL ) {
		strcpy(tmp,prg_tbl[no]->dir);
		joint_path(tmp,"*.*");
		prg_tbl[no]->copy[0] = strdup(tmp);
	    }
	    prg_tbl[no]->copycnt++;
	}
	DSP_mos(0);
    }

    MSG_disp("%-29.29s",prg_tbl[no]->name);
    MENU_mask(RETN_NO,ON);
    MENU_mask(LIST_NO,OFF);
    MENU_mask(PRNT_NO,OFF);
    MENU_mask(INST_NO,prg_tbl[no]->copycnt > 0 ? ON:OFF);
    MENU_mask(EXIT_NO,OFF);

    if ( prg_tbl[no]->readme != NULL ) {
	MENU_mask(LIST_NO,prg_tbl[no]->manual != NULL ? ON:OFF);
	FILE_open(prg_tbl[no]->readme);
	fg = 0;
    } else if ( prg_tbl[no]->manual != NULL ) {
	MENU_mask(TEXT_NO,prg_tbl[no]->readme != NULL ? ON:OFF);
	FILE_open(prg_tbl[no]->manual);
	fg = 1;
    } else {
	FILE_open(NULL);
	fg = (-1);
    }

    n = NON_NO;
    for ( ; ; ) {
	while ( (i = MENU_no(0)) == n )
	    FILE_irq();
	n = i;

	if ( i == CLER_NO )
	    break;
	else if ( i == LIST_NO ) {
	    if ( fg == 0 && prg_tbl[no]->manual != NULL ) {
		MENU_mask(TEXT_NO,prg_tbl[no]->readme != NULL ? ON:OFF);
		FILE_open(prg_tbl[no]->manual);
		fg = 1;
	    } else if ( fg == 1 && prg_tbl[no]->readme != NULL ) {
		MENU_mask(LIST_NO,prg_tbl[no]->manual != NULL ? ON:OFF);
		FILE_open(prg_tbl[no]->readme);
		fg = 0;
	    }
	} else if ( i == PRNT_NO )
	    FILE_lpt();
	else if ( i == INST_NO )
	    COPY_all(prg_tbl[no]->copycnt,prg_tbl[no]->copy);
	else if ( i == BACK_NO )
	    FILE_back();
	else if ( i == NEXT_NO )
	    FILE_next();

    }

#ifdef	DEBUG
    MENU_mask(CLER_NO,OFF);
    MENU_mask(LIST_NO,ON);
    MENU_mask(PRNT_NO,ON);
    MENU_mask(INST_NO,ON);
    MENU_mask(EXIT_NO,ON);
#else
    MENU_mask(CLER_NO,OFF);
    MENU_mask(LIST_NO,OFF);
    MENU_mask(PRNT_NO,OFF);
    MENU_mask(INST_NO,OFF);
    MENU_mask(EXIT_NO,ON);
#endif
    MSG_disp("%-29.29s","");
    FILE_close();
    PRG_status();

    return i;
}
int	LIST_proc(void)
{
    int     i,n;
    int     fg=0;
    char    *p,*s;

LOOP:
    if ( (p = FILE_select()) == NULL )
	return LIST_NO;

    if ( JOKE_run(p) )
	goto LOOP;

    MENU_mask(RETN_NO,ON);
    MENU_mask(LIST_NO,OFF);
    MENU_mask(PRNT_NO,ON);
    MENU_mask(INST_NO,OFF);
    MENU_mask(EXIT_NO,OFF);
    FILE_open(p);

    n = NON_NO;
    for ( ; ; ) {
	while ( (i = MENU_no(0)) == n )
	    FILE_irq();
	n = i;

	if ( i == CLER_NO )
	    break;
	else if ( i == PRNT_NO )
	    FILE_lpt();
	else if ( i == BACK_NO )
	    FILE_back();
	else if ( i == NEXT_NO )
	    FILE_next();

    }

#ifdef	DEBUG
    MENU_mask(CLER_NO,OFF);
    MENU_mask(LIST_NO,ON);
    MENU_mask(PRNT_NO,ON);
    MENU_mask(INST_NO,ON);
    MENU_mask(EXIT_NO,ON);
#else
    MENU_mask(CLER_NO,OFF);
    MENU_mask(LIST_NO,OFF);
    MENU_mask(PRNT_NO,OFF);
    MENU_mask(INST_NO,OFF);
    MENU_mask(EXIT_NO,ON);
#endif
    FILE_close();
    PRG_status();

    goto LOOP;
}
void	main(int argc,char *argv[])
{
    int     i,n,cdrv;
    char    *p;
    char    *exp;
    char    tmp[4];

    cdrv = getdrv();
    tmp[0] = cdrv + 'A'; tmp[1] = ':'; tmp[2] = '\0';
    crent_drive = tmp;
    exp = argv[0];
    index_file[0] = '\0';

    if ( argc > 1 ) {
	while ( --argc > 0 ) {
	    p = *(++argv);
	    if ( *p == '-' || *p == '/' ) {
		switch(toupper(*(p+1))) {
		case 'D':
		    p += (*(p+2) == '=' ? 3:2);
		    crent_drive = p;
		    break;
		}
	    } else
		strcpy(index_file,p);
	}

    }

    if ( index_file[0] == '\0' ) {
	p = exp;
	if ( p[0] != '\0' && p[1] != ':' ) {
	    index_file[0] = 'A' + cdrv;
	    index_file[1] = ':';
	} else {
	    index_file[0] = *(p++);
	    index_file[1] = *(p++);
	}
	index_file[2] = '\0';

	if ( crent_drive == tmp )
	    crent_drive = strdup(index_file);

	if ( *p != '\\' ) {
	    getdir(index_file+2);
	    if ( index_file[3] != '\0' )
		strcat(index_file,"\\");
	}
	strcat(index_file,p);
	if ( (p = strrchr(index_file,'\\')) == NULL )
	    for ( p = index_file ; *p != '\0' ; p++ );
	strcpy(p,"\\HELPER.IDX");
    }

    DSP_init();
    DSP_mos(2);

    ICON_init();
    SCRN_init();
    DB_init(index_file);
    KEY_cler();
    KAN_open(putcnv,putsys,putmode);


#ifdef	DEBUG
    MENU_mask(CLER_NO,OFF);
    MENU_mask(LIST_NO,ON);
    MENU_mask(PRNT_NO,ON);
    MENU_mask(INST_NO,ON);
    MENU_mask(EXIT_NO,ON);
#else
    MENU_mask(CLER_NO,OFF);
    MENU_mask(LIST_NO,OFF);
    MENU_mask(PRNT_NO,OFF);
    MENU_mask(INST_NO,OFF);
    MENU_mask(EXIT_NO,ON);
#endif

    DSP_mos(0);

    n = NON_NO;
    for ( ; ; ) {
	while ( (i = MENU_no(1)) == n );
	n = i;

	if ( i == EXIT_NO ) {
	    if ( yesno("HELPERを終了しますか？") != (-1) )
	        break;
	} else if ( i == CLER_NO )
	    KEY_cler();
#ifdef	DEBUG
	else if ( i == LIST_NO )
	    i = LIST_proc();
	else if ( i == PRNT_NO )
	    PLT_control();
	else if ( i == INST_NO )
	    KEY_input();
#endif
	else if ( i == BACK_NO )
	    PRG_back();
	else if ( i == NEXT_NO )
	    PRG_next();
	else if ( i >= PRG_NO )
	    i = PRG_clic(i - PRG_NO);
	else if ( i >= KEY_NO )
	    KEY_clic(i - KEY_NO);

    }

    KAN_close();
    DSP_end();
    chdrv(cdrv);
}
void	INKEY_start(void)
{
    kb_act = 1;
    KYB_clrbuf();
    putmode(0,0,NULL);
}
void	INKEY_end(void)
{
    kb_act = 0;
    gputs(560,460,XPRG_COL,PRG_COL,"         ");
}
int	kbhit(void)
{
    if ( kb_flg != 0 )
	return 1;

    ch_bak = KAN_read(1,&ec_bak);

    if ( (ec_bak & 0xFF00) == 0xFF00 )
	return 0;

    kb_flg = 1;
    return 1;
}
int	getch(void)
{
    if ( kbhit() == 0 )
	return EOF;

    kb_flg = 0;
    if ( ch_bak  != 0xFFFF ) {
	if ( (ch_bak & 0x8000) != 0 || (ec_bak & 0xFF00) > 0x5100 )
	    ch_bak = 0xFFFF;
	switch(ec_bak&0xFF00) {
	case 0x7300: ch_bak = 0x000D; break;
	case 0x7200: ch_bak = 0x001B; break;
	}
    }
    return ((ch_bak & 0xFF00) == 0xFF00 ? EOF:ch_bak);
}
/***********************
int	getch(void)
{
    int     ch;
    unsigned ec;

    ch = KYB_read(1,&ec);
    return ((ch & 0xFF00) == 0xFF00 ? EOF:ch);
}
*************************/
