/*
 * Windows Kermit
 * 
 * Written by William S. Hall
 *	      3665 Benton Street, #66
 *	      Santa Clara, CA 95051
 *
 * Kermit specific initialization
 */

#define NOCOMM
#define NOKANJI
#define NOATOM
#define NOMINMAX
#include <windows.h>
#include <stdlib.h>

#include "winkpr.h"
#include "winkpf.h"
#define INITKEYSIZE	1024
 
/* read win.ini for protocol settings */
BOOL krmInit(HWND hWnd, HANDLE hInstance)
{

    char szAppStr[40];
    char szKeyStr[80];
    char szMessageStr[80];
    char szDefStr[80];
    char *szInitKeyStr;
    HANDLE hInitKeyStr;
    int Initlen;
    register BOOL WinIniModified = FALSE;
    register int i;
    int reply;
    int *ptr;

    Kermit.maxsenddatalen = 80;
    Kermit.chksumtype = 1;
    Kermit.mstimeout = 5000;
    Kermit.maxtry = KRM_DEFMAXTRY;
    Kermit.timer = TRUE;
    Kermit.filewarning = TRUE;
    Kermit.filediscard = TRUE;
    Kermit.bell = TRUE;

    sndpkt.mark = rcvpkt.mark = KRM_DEFMARK;
    
    remote.maxpacketsize = KRM_DEFPACKETSIZE;
    remote.timeout = KRM_DEFTIMEOUT;
    remote.padcount = KRM_DEFPADCOUNT;
    remote.padchar = KRM_DEFPADCHAR;
    remote.eol = KRM_DEFEOL;
    remote.quote = KRM_DEFQUOTE;
    remote.binquote = KRM_DEFBINQUOTE;
    remote.chksumtype = KRM_DEFCHKSUM;
    remote.rptprefix = KRM_DEFRPTCHAR;

    local.maxpacketsize = KRM_MAXPACKETSIZE;
    local.timeout = 10;
    local.padcount = KRM_DEFPADCOUNT;
    local.padchar = KRM_DEFPADCHAR;
    local.eol = KRM_DEFEOL;
    local.quote = KRM_DEFQUOTE;
    local.binquote = 'Y';
    local.chksumtype = KRM_DEFCHKSUM;
    local.rptprefix = KRM_DEFRPTCHAR;

    hInitKeyStr = LocalAlloc(LPTR, INITKEYSIZE);
    if ((szInitKeyStr = LocalLock(hInitKeyStr)) == NULL)
	return FALSE;

    LoadString(hInstance,IDS_KRM_KERMIT,(LPSTR)szAppStr,sizeof(szAppStr));
    Initlen = GetProfileString(szAppStr, (LPSTR)NULL, (LPSTR)"",
			       szInitKeyStr, INITKEYSIZE);
    if (Initlen == 0) {
        LoadString(hInstance,IDS_KRM_ADDDEFAULTS, 
			(LPSTR)szMessageStr, sizeof(szMessageStr));
        reply = MessageBox(hWnd,(LPSTR)szMessageStr,
		   (LPSTR)szAppStr, MB_ICONQUESTION | MB_YESNO);
	if (reply == IDNO)
	    return FALSE;
    }

    ptr = &Kermit.timer;
    for (i = 0; i < 4; i++) {
        LoadString(hInstance,IDS_KRM_TIMER+i,(LPSTR)szKeyStr,sizeof(szKeyStr));
	if (!SearchKey(szInitKeyStr, szKeyStr, Initlen)) {
            itoa(*ptr,szDefStr,10);
	    WriteProfileString(szAppStr,szKeyStr,szDefStr);
	    WinIniModified = TRUE;
	}
        *ptr++ = GetProfileInt((LPSTR)szAppStr, (LPSTR)szKeyStr, *ptr);
    }	    
    LocalUnlock(hInitKeyStr);
    LocalFree(hInitKeyStr);

    if (WinIniModified)
	BroadcastWinIniChange();

    return TRUE;
}
