/*****************************************************************************
* Copyright (c) 1990-98 Digi International Inc., All Rights Reserved
* 
* This software contains proprietary and confidential information of Digi
* International Inc.  By accepting transfer of this copy, Recipient agrees
* to retain this software in confidence, to prevent disclosure to others,
* and to make no use of this software other than that for which it was
* delivered.  This is an unpublished copyrighted work of Digi International
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
* prohibited.
* 
* Restricted Rights Legend
*
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
* Technical Data and Computer Software clause at DFARS 252.227-7031 or
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -
* Restricted Rights at 48 CFR 52.227-19, as applicable.
*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
*
*****************************************************************************/
// linkcfg.cpp : Defines the initialization routines for the DLL.
// The main file that provides all entry points for LinkService management
// as well as other support routines that create services and write/read 
// from the registry
// This file is modified from the original supplied in the sample SDK.

#include "stdafx.h"
#include "sdlccfg.h"
#include "common.h"
#include "commonres.h"
#include "HdwSettings.h"
#include "modeset.h"

#include <winreg.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif





// BOOL		bNewLinkService = FALSE;





/////////////////////////////////////////////////////////////////////////////
// CLinkcfgApp

BEGIN_MESSAGE_MAP(CLinkcfgApp, CWinApp)
	//{{AFX_MSG_MAP(CLinkcfgApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLinkcfgApp construction

CLinkcfgApp::CLinkcfgApp()
{
	//
	// Indicate that the Property Sheet uses the Help Button.
	//
	cpsLinkService.m_psh.dwFlags |= PSH_HASHELP + PSH_NOAPPLYNOW;

	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CLinkcfgApp object

CLinkcfgApp theApp;


/*
**      Purpose:
**              Configuration Function for an IBM SDLC Adaptor.
**
**      Arguments: 
**
**      Returns:
**              TRUE	if network bindings need to be recalculated
**              FALSE	if network bindings do not need to be recalculated.
**
**      Notes:
**              
**
***************************************************************************/
__declspec(dllexport) BOOL WINAPI ConfigureLinkService(LPSTR szComputerName, 
												LPSTR szLinkServiceTitle)
{
   AFX_MANAGE_STATE(AfxGetStaticModuleState());

   BOOL			bReturnCode = FALSE;
	BOOL			bNewLinkService = FALSE;

	fConnectRegistry(szComputerName);

	HINSTANCE hInst = AfxGetResourceHandle();
	//
	// Check to see if we have administrative rights to the remote box.
	//
	if (!fCanWeAdministerRemoteBox())
	{
	CHAR		temp_string[32],temp_string2[128];

		LoadString(hInst,IDS_UNABLE_ADMIN_REMOTE_T,temp_string,sizeof(temp_string));
		LoadString(hInst,IDS_UNABLE_ADMIN_REMOTE_M,temp_string2,sizeof(temp_string2));
		::MessageBox(GetActiveWindow(),
					 temp_string2,
					 temp_string,
					 MB_ICONEXCLAMATION);

		return TRUE;
	}

	// Set the Global Global Variables
	SetOSGlobals();

	// if we are adding a new service, find out if we have an available line pre configured to 
	// run SNA
	CString szSNALinePath;
	if (!strcmp(szLinkServiceTitle, ""))
	{
		BOOL bSuccess;

		if (gdwOSMajorVersion <= 4)
			bSuccess = bAvailableSNALineNT4(szSNALinePath);
		else
			bSuccess = bAvailableSNALineNT5(szSNALinePath);

		if (!bSuccess)
		{
			return FALSE;
		}

		bNewLinkService = TRUE;
	}

	
	gMgsdlc_data.bBindingsNeeded = FALSE;

	//
	// Add all Property Pages to the Link Service Property Sheet
	//
	CHdwSettings	cppHdwSettings;
	CModeSet		cppModeSet;
	theApp.cpsLinkService.AddPage(&cppModeSet);
	theApp.cpsLinkService.AddPage(&cppHdwSettings);

   //
	// Setup the global data structure. This will also read data from registry
	//
	fSetupGlobalValues(szLinkServiceTitle, szComputerName, (LPSTR)(LPCTSTR)szSNALinePath);

	CString szPropSheetTitle(gMgsdlc_data.szLinkServiceTitle);
	szPropSheetTitle += _T(" ");
	szPropSheetTitle += gMgsdlc_data.szCardString;

	theApp.cpsLinkService.SetTitle( szPropSheetTitle );

	//
	// Invoke the Link Service Property Sheet.
	//
	if (IDOK == theApp.cpsLinkService.DoModal())
	{
		//
		// Create the Link Service Service
		//

		fInstallService(bNewLinkService,szComputerName,(LPSTR)(LPCTSTR)szSNALinePath);
	}
	fDisconnectRegistry();
	fAddPerfmonCounters(szComputerName);
	bReturnCode = gMgsdlc_data.bBindingsNeeded;

	return bReturnCode;
}



/*
**      Purpose:
**              Remove Link Service.
**
**      Arguments: 
**
**      Returns:
**              TRUE	if network bindings need to be recalculated
**              FALSE	if network bindings do not need to be recalculated.
**
**      Notes:
**              
**
***************************************************************************/
__declspec(dllexport) BOOL WINAPI RemoveLinkService(LPSTR szComputerName, 
											 LPSTR szLinkServiceTitle)
{
   AFX_MANAGE_STATE(AfxGetStaticModuleState());

   // Set the Global Global Variables
	SetOSGlobals();

	TRACE("RemoveLinkService: szLinkServiceTitle = %s\n", szLinkServiceTitle);

	return fRemoveLinkService(szComputerName,szLinkServiceTitle);
}




/*
**      Purpose:
**              Remove All IBM SDLC Link Services.
**
**      Arguments: 
**
**      Returns:
**              TRUE	if network bindings need to be recalculated.
**              FALSE	if network bindings do not need to be recalculated.
**
**      Notes:
**              
**
***************************************************************************/
__declspec(dllexport) BOOL WINAPI RemoveAllLinkServices(LPSTR szComputerName)
{
   AFX_MANAGE_STATE(AfxGetStaticModuleState());

   // Set the Global Global Variables
	SetOSGlobals();

	TRACE("fRemoveAllLinkServices: szComputerName = %s\n", szComputerName);

	return fRemoveAllLinkServices(szComputerName);
}

