#include <stdio.h>
#include <conio.h>
#include <assert.h>
#include "sense4.h"

define DEST_DIR "df01"

int main(int argc, char* argv[])
{
	SENSE4_CONTEXT stS4Ctx = {0};
	unsigned long dwResult = 0;

	unsigned char lpOldPin[] = "123456781234567812345678";
	unsigned long dwOldPinLen = sizeof(lpOldPin)-1;
	unsigned char lpNewPin[] = "zaq12wsxcde34rfvbgt56yhn";
	unsigned long dwNewPinLen = sizeof(lpNewPin)-1;

	unsigned char lpOldUsrPin[] = "12345678";
	unsigned long dwOldUsrPinLen = sizeof(lpOldUsrPin)-1;
	unsigned char lpNewUsrPin[] = "zaq12wsx";
	unsigned long dwNewUsrPinLen = sizeof(lpNewUsrPin)-1;
	
	unsigned long dwSubDirSize = 20000;/* estimated directory size: 3K */

	printf("指定 ELドングルに接続...\n");
	if (!(dwResult = OpenS4ByIndex(FIRST_S4_INDEX, &stS4Ctx)))
	{
		return 1;
	} 
	printf("Success!\n");

	/* ルートディレクトリに移動 */
	printf("Change to root directory...\n");
	dwResult = S4ChangeDir(&stS4Ctx, "\\");
	if (dwResult != S4_SUCCESS) 
	{
		printf("Change directory failed! \n",dwResult);
		S4Close(&stS4Ctx);
		return dwResult;
	}
	printf("Success!\n");

	
	printf("ディレクトリ %s に移動\n", DEST_DIR);
	dwResult = S4ChangeDir(&stS4Ctx, DEST_DIR);
	if (dwResult != S4_SUCCESS && dwResult != S4_FILE_NOT_FOUND) 
	{
		printf("Change directory failed! \n",dwResult);
		S4Close(&stS4Ctx);
		return dwResult;
	}else if (dwResult == S4_SUCCESS)
	{
		printf("Success!\n");

		/* ディレクトリが存在したら削除 
	         ディレクトリ以下のファイルとサブディレクトリが削除される

	         ELに変更を加えるため開発者 PIN でログイン(S4VerifyPin)
		*/
	    printf("To erase all the file under %s,first verify DEV_PIN...\n", DEST_DIR);
		dwResult = S4VerifyPin(&stS4Ctx, lpNewPin, dwNewPinLen, S4_DEV_PIN);
		if (dwResult != S4_SUCCESS) 
		{
			printf("Verify DEV_PIN failed! \n",dwResult);
			S4Close(&stS4Ctx);
			return dwResult;
		}
		printf("Success!\n");
		
		printf("Erasing...\n");
		/* 
		 PIN はディレクトリ毎に設定する。ルートディレクトリを削除すると
		 PIN は初期値に設定される
		*/
		dwResult = S4EraseDir(&stS4Ctx, NULL);
		if (dwResult != S4_SUCCESS) 
		{
			printf("Erase root directory failed! \n",dwResult);
			ResetAndCloseS4(&stS4Ctx);
			return dwResult;
		}
		printf("Success!\n");
		
	}else /* "df01"という名前にディレクトリを作成 */
	{
		printf("Not exist!Now create directory %s...\n", DEST_DIR);
		
		/* ルートディレクトリの開発者 PIN でログイン */
		printf("Verify DEV_PIN of root directory...\n");
		dwResult = S4VerifyPin(&stS4Ctx, lpOldPin, dwOldPinLen, S4_DEV_PIN);
		if (dwResult != S4_SUCCESS) 
		{
			printf("Verify DEV_PIN failed! \n",dwResult);
			S4Close(&stS4Ctx);
			return dwResult;
		}
		printf("Success!\n");
		
		printf("Create directory %s ...\n", DEST_DIR);
		dwResult = S4CreateDir(&stS4Ctx, DEST_DIR, dwSubDirSize, S4_CREATE_SUB_DIR);
		if (dwResult != S4_SUCCESS) 
		{
			printf("Create df01 directory failed! \n",dwResult);
			ResetAndCloseS4(&stS4Ctx);
			return dwResult;
		}
		printf("Success!\n");
		
		
	}
	/* 指定ディレクトリの開発者 PIN とユーザ PIN を設定 */
	printf("Change USER_PIN of directory %s...\n", DEST_DIR);
	dwResult = S4ChangePin(&stS4Ctx,lpOldUsrPin,dwOldUsrPinLen,lpNewUsrPin,dwNewUsrPinLen,S4_USER_PIN);
	if (dwResult != S4_SUCCESS) 
	{
		printf("Change USER_PIN of df01 directory failed! \n",dwResult);
		S4Close(&stS4Ctx);
		return dwResult;
	}
	printf("Success!\n");
	
	printf("Change DEV_PIN of directory %s...\n", DEST_DIR);
	dwResult = S4ChangePin(&stS4Ctx,lpOldPin,dwOldPinLen,lpNewPin,dwNewPinLen,S4_DEV_PIN);
	if (dwResult != S4_SUCCESS) 
	{
		printf("Change DEV_PIN of df01 directory failed! \n",dwResult);
		S4Close(&stS4Ctx);
		return dwResult;
	}
	printf("Success!\n");

	
	/* PINでログインしたので、S4Close ではなく ResetAndCloseS4 で切断
	ResetAndCloseS4(&stS4Ctx);
    getch(); 
	return dwResult;
}