/* Copyright (c) 1984-98 by The MathWorks, Inc. */
/* $Revision: 1.3 $ */
#include "windows.h"
#include "mex.h"

#define STRSIZE 256

char *QueryNetscapeReg(char *key, char *value_name)
{
	char key_path[STRSIZE], *program_path, reg_path[STRSIZE];
	HKEY hRootKey;
	LONG lret;
	DWORD size = STRSIZE;
    DWORD dwType = REG_SZ;
    int i;
	OFSTRUCT of;

	key_path[0] = '\0';

    /* Check registry for Netscape key and value names.  If any call
     * fails, return immediately.
     */
	strcpy(reg_path, "Software\\Netscape\\Netscape Navigator\\");
    strcat(reg_path, key);

	
	lret = RegOpenKeyEx(HKEY_CURRENT_USER,
                        reg_path,
                        NULL,
                        KEY_READ,
                        &hRootKey);

    if (lret != ERROR_SUCCESS)
        return NULL;

    lret = RegQueryValueEx(hRootKey,
                           (LPSTR)value_name,
                           NULL,
                           &dwType,
                           (LPBYTE)key_path,
                           &size);

    if (lret != ERROR_SUCCESS)
        return NULL;

    lret = RegCloseKey(hRootKey);

    if (lret != ERROR_SUCCESS)
        return NULL;

    /* Key does not exist.  Bail */
	if (key_path[0] == '\0') {
        return NULL;
    }

    /* Allocate string for exe path */
    if (program_path = mxCalloc(STRSIZE, 1))
		strcpy(program_path, key_path);
    else
        return NULL;

    /* Check current directory for exe */

    strcat(program_path, "\\netscape.exe");

    if (OpenFile(program_path, &of, OF_EXIST) != HFILE_ERROR) {
        return program_path;
    }

    /* Check in directory returned + Program */
    for (i = strlen(program_path); i >= 0; i--) {
        if (program_path[i] == '\\') {
            program_path[i] = '\0';
            break;
        }
    }

    strcat(program_path, "\\Program\\netscape.exe");

    if (OpenFile(program_path, &of, OF_EXIST) != HFILE_ERROR) {
        return program_path;
    }

    /* Check in directory above for exe */
	strcpy(program_path, key_path);

    strcat(program_path, "\\netscape.exe");

    if (OpenFile(program_path, &of, OF_EXIST) != HFILE_ERROR) {
        return program_path;
    }

    /* Check in directory returned + Program */
    for (i = strlen(program_path); i >= 0; i--) {
        if (program_path[i] == '\\') {
            program_path[i] = '\0';
            break;
        }
    }

    strcat(program_path, "\\Program\\netscape.exe");

    if (OpenFile(program_path, &of, OF_EXIST) != HFILE_ERROR) {
        return program_path;
    }

    for (i = strlen(program_path); i >= 0; i--) {
        if (program_path[i] == '\\') {
            program_path[i] = '\0';
            break;
        }
    }

    /* Both places failed */
    mxFree(program_path);
    return NULL;
}

void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
	char *prog_path;

    /* Check various likely registry entries for clues as to where
     * Netscape exe is.
     */
    if (prog_path = QueryNetscapeReg("Cache", "Cache Dir")) {
		plhs[0] = mxCreateString(prog_path);
        mxFree(prog_path);
        return;
    }
        
    if (prog_path = QueryNetscapeReg("Mail", "Mail Directory")) {
		plhs[0] = mxCreateString(prog_path);
        mxFree(prog_path);
        return;
    }
        
    if (prog_path = QueryNetscapeReg("News", "News Directory")) {
		plhs[0] = mxCreateString(prog_path);
        mxFree(prog_path);
        return;
    }

    if (prog_path = QueryNetscapeReg("Security", "Security Directory")) {
		plhs[0] = mxCreateString(prog_path);
        mxFree(prog_path);
        return;
    }

    if (prog_path = QueryNetscapeReg("Main", "Install Directory")) {
		plhs[0] = mxCreateString(prog_path);
        mxFree(prog_path);
        return;
    }

	plhs[0] = mxCreateString("");
    
}
