@echo off

SETLOCAL ENABLEDELAYEDEXPANSION
set BATCH_DIR=%~dp0
set DEFAULT_STORE_PATH=%BATCH_DIR%..\store

set HOSTNAME=
set DOMAINNAME=
set IPADDR=
set REQDAYS=
set STORE_PATH=%DEFAULT_STORE_PATH%

:param_loop
if "%1"=="" goto end_param_loop
if "%1"=="-c" (
	set HOSTNAME=%~2
	set DOMAINNAME=%~3
	set REQDAYS=%~4
	
	shift & shift & shift & shift
	goto param_loop
)

if "%1"=="-o" (
	set STORE_PATH=%~2
	shift & shift
	goto param_loop
)
goto usage

:end_param_loop

set IPS=
for /f "skip=2" %%l in ('wmic nicconfig where ipenabled^=true get ipaddress /format:csv') do (
	for /f "tokens=2 delims={" %%i in ("%%l") do (
		for /f "tokens=1 delims=}" %%u in ("%%i") do set IPS=!IPS! %%u
	)
)
set IPADDRS=
set /a IPCOUNT=0
for %%i in (%IPS%) do (
	set /a IPCOUNT=!IPCOUNT! + 1
	set IPADDRS=!IPADDRS!,IP:%%i
)

md "%STORE_PATH%"

copy /Y "%BATCH_DIR%index.txt" "%STORE_PATH%\index.txt"
copy /Y "%BATCH_DIR%crlnumber" "%STORE_PATH%\crlnumber"
copy /Y "%BATCH_DIR%serial" "%STORE_PATH%\serial"

rem Do not include the domain name if the host does not belong to a domain
if not "%DOMAINNAME%" == "" (
	set FQDN=%HOSTNAME%.%DOMAINNAME%
) else (
	set FQDN=%HOSTNAME%
)

rem This variable is required to be set because it is referenced from the OpenSSL config file. 
SET SOFTING_CERT_GENERATION_URI=URI:urn:%HOSTNAME%/ORiN2/OpcUa/Server,DNS:%FQDN%%IPADDRS%

echo =====================================
echo = Creating root certificate
echo =====================================
CALL "%BATCH_DIR%create_root_ca_cert.bat" "%STORE_PATH%" "/C=JP/L=AGUI/ST=AICHI/O=DENSO WAVE/OU=DEV/CN=ORiN2 OpcUa CA/DC=%HOSTNAME%"

echo =====================================
echo = Updating CRL
echo =====================================
CALL "%BATCH_DIR%update_revokation_list.bat" "%STORE_PATH%"

echo =====================================
echo = Generating server self signed certificate
echo =====================================
CALL "%BATCH_DIR%create_self_signed_certificate.bat" "%STORE_PATH%" server_self_signed "/C=JP/L=AGUI/ST=AICHI/O=DENSO WAVE/OU=DEV/CN=ORiN2 OpcUa Server (self signed)/DC=%HOSTNAME%" %REQDAYS%

rem ↓以下、クライアントのユーザ認証用のx.509証明書とプライベートキーの作成
rem サーバ用のBATの為、行いません
rem rem This variable is required to be set because it is referenced from the OpenSSL config file. 
rem SET SOFTING_CERT_GENERATION_URI=URI:urn:%HOSTNAME%/ORiN2/OpcUa/Client

rem echo =====================================
rem echo = Generating client self signed certificate
rem echo =====================================
rem CALL "%BATCH_DIR%create_user_self_signed_certificate.bat" "%STORE_PATH%" user_client_self_signed "/C=JP/L=AGUI/ST=AICHI/O=DENSO WAVE/OU=DEV/CN=ORiN2 OpcUa Client (self signed)/DC=%HOSTNAME%" %REQDAYS%
rem ↑ここまで

goto done

:usage
echo off
echo This batch file creates certificates and corresponding private keys for
echo the sample client and server applications delivered with the OPC UA Toolkit.
echo.
echo Usage: %~n0 ^[-c ^<Host name^> ^<Domain^> ^<IP address^>^] ^[-o ^<Output directory^>^] ^]
echo.
echo -c  Use the given host name, domain name and IP address
echo     to create instance certificates. If this parameter is
echo     not specified, the configuration is performed interactively.
echo.
echo -o  Specifies the output directory of the generated files.
echo     By default, the generated files are placed in
echo     %DEFAULT_STORE_PATH%
goto done

:done
