/*****************************************************************************************
*  Copyright (c) 2004, Beijing Senselock Data Security Centre
*  All rights reserved.
*
*  Filename£ºsample_36_win32.c
*          
*  Briefs£º  The purpose of this snippet is to demonstrate howto (un)install 
*            SenseLock EL's latest WHQL-certified driver in developer's own program.
*            
*  History:        
*  --------------------------------------------------------        
*            10/28/2004 v1.0.0.0 Lihua created
*            
*            
*******************************************************************************************/

#include "stdio.h"
#include "s4drv.h"

/* paramter
//	-u: uninstall
//	-nc: not clear old drivers
//	-pcsc: need virtual reader
//	-rd x: reader count
//	-path string: the dest path of driver file
*/
BOOL isInst = TRUE;
BOOL isPcsc = FALSE;
BOOL isClearOld = TRUE;
DWORD dwReaderCount = 1;
char lpszPath[MAX_PATH] = {0};
BOOL isNeedReboot = FALSE;

void HandleArgment(char* argv[], int argc)
{
	int i;
	for(i=1; i<argc; i++)
	{
		if(0 == strcmp(argv[i], "-u"))
			isInst = FALSE;
		else if(0 == strcmp(argv[i], "-pcsc"))
			isPcsc = TRUE;
		else if(0 == strcmp(argv[i], "-nc"))
			isClearOld = FALSE;
		else if(0 == strcmp(argv[i], "-rd"))
		{
			if(i+1 < argc)
				dwReaderCount = atoi(argv[i+1]);
			i++;
		}
		else if(0 == strcmp(argv[i], "-path"))
		{
			if(i+1 < argc)
				strcpy(lpszPath, argv[i+1]);
			i++;
		}
	}
}

int main(int argc, char* argv[])
{
	DWORD dwRet;
	char* pointer;
	GetSystemDirectory(lpszPath, MAX_PATH);
	pointer = strstr(lpszPath, ":\\");
	pointer[2] = '\0';
	strcat(lpszPath, "Program Files\\Senselock\\Driver");		// set default path;

	HandleArgment(argv, argc);

	if(isInst)
	{
		DWORD dwFlag = 0;
		dwFlag |= (isClearOld ? DRV_FLAG_CLEAR_OLD : 0);
		dwFlag |= (isPcsc ? DRV_FLAG_PCSC : 0);
		printf("start to install Senselock Drivers....\n");
		dwRet = s4drv_Install(lpszPath, dwReaderCount, dwFlag);
		if(dwRet != ERR_SUCCESS)
		{
			printf("driver installed failed, Error: 0x%08x\n", dwRet);
			return 1;
		}
	}
	else
	{
		printf("start to uninstall Senselock Drivers....\n");
		dwRet = s4drv_Uninstall();
		if(dwRet != ERR_SUCCESS)
		{
			printf("driver uninstalled failed, Error: 0x%08x\n", dwRet);
		}
	}
 	printf("finish\n");

	if(s4drv_IsNeedReboot())
		printf("you must restart your system!");

	return 0;
}

