@echo off

SETLOCAL ENABLEDELAYEDEXPANSION
set BATCH_DIR=%~dp0
set DEFAULT_STORE_PATH=%BATCH_DIR%..\store
set DEFAULT_USER_CERTIFICATE_PATH=%BATCH_DIR%..\..\UserCertificate

rem Parse batch parameters
set INTERACTIVE=true

set HOSTNAME=
set DOMAINNAME=
set IPADDR=
set STORE_PATH=%DEFAULT_STORE_PATH%
set USER_CERTIFICATE_PATH=%DEFAULT_USER_CERTIFICATE_PATH%

:param_loop
if "%1"=="" goto end_param_loop
if "%1"=="-c" (
	if "%4"=="" goto usage
	set INTERACTIVE=false
	set HOSTNAME=%~2
	set DOMAINNAME=%~3
	set IPADDR=%~4
	shift & shift & shift & shift
	goto param_roop
)
if "%1"=="-o" (
	set STORE_PATH=%~2
	shift & shift
	goto param_loop
)
goto usage
:end_param_loop

if "%INTERACTIVE"=="false" goto ip_selected
rem ---------------------------------------------------------------------------
rem  Non-interactive configuration
rem ---------------------------------------------------------------------------

rem Host name
for /f %%i in ('HOSTNAME') do set HOSTNAME=%%i

rem Domain
set DOMAINNAME=%USERDNSDOMAIN%

rem IP address
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
	)
)
echo OK
set /a IPCOUNT=0
for %%i in (%IPS%) do (
	set /a IPCOUNT=!IPCOUNT! + 1
	echo !IPCOUNT!: %%i
)
if %IPCOUNT% EQU 0 (
	echo No IP address found, using 127.0.0.1 ^(localhost^)
	echo.
	pause
	set IPADDR=127.0.0.1
	goto ip_selected
)
if %IPCOUNT% EQU 1 (
	for %%i in (%IPS%) do set IPADDR=%%i
	goto ip_selected
)
set /a SELECTED=0

:select_ip
set /p INPUT=Please select an IP address (x to abort):
if /i "%INPUT%"=="x" goto done
set /a SELECTED=%INPUT%
if %INPUT% GTR %IPCOUNT% goto input_error
if %INPUT% LSS 1 goto input_error
goto selection_ok

:input_error
rem invalid selection - show possible option again
echo.
echo Invalid selection
echo.
set /a IPCOUNT=0
for %%i in (%IPS%) do (
	set /a IPCOUNT=!IPCOUNT! + 1
	echo !IPCOUNT!: %%i
)
goto select_ip

:selection_ok
set /a SEL=0
for %%i in (%IPS%) do (
	set /a SEL=!SEL! + 1
	if !SEL! EQU %SELECTED% set IPADDR=%%i
)

:ip_selected
if "%IPADDR%"=="" goto error_no_ip
if "%INTERACTIVE%"=="false" goto info_done
echo Creating certificates with following information:
echo     Host: "%HOSTNAME%"
echo     Domain: "%DOMAINNAME%"
echo     IP Address: "%IPADDR%"
echo.
echo If anything is incorrect please abort using Ctrl-C
pause
:info_done

md "%STORE_PATH%"
md "%USER_CERTIFICATE_PATH%"

copy /y "%BATCH_DIR%index.txt" "%STORE_PATH%\index.txt"
rem copy /y "%BATCH_DIR%crlnumber" "%STORE_PATH%\crlnumber"
copy /y "%BATCH_DIR%serial" "%STORE_PATH%\serial"

copy /y "%BATCH_DIR%index.txt" "%USER_CERTIFICATE_PATH%\index.txt"
rem copy /y "%BATCH_DIR%crlnumber" "%STORE_PATH%\crlnumber"
copy /y "%BATCH_DIR%serial" "%USER_CERTIFICATE_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 DW_CERT_GENERATION_URI=URI:urn:%HOSTNAME%/ORiN2/OpcUaMultiple/Provider

echo =============================================
echo = Generating client self signed certificate =
echo =============================================
call "%BATCH_DIR%create_self_signed_certificate.bat" "%STORE_PATH%" multiple_client_self_signed "/C=JP/ST=AICHI/L=AGUI/O=DENSO WAVE/OU=DEV/CN=ORiN2 Opc Ua Multiple Provider/DC=%HOSTNAME%"

echo =============================================
echo = Genarating client user certificate        =
echo =============================================
call "%BATCH_DIR%create_user_certificate.bat" "%USER_CERTIFICATE_PATH%" multiple_client_user "/C=JP/ST=AICHI/L=AGUI/O=DENSO WAVE/OU=DEV/CN=ORiN2 Opc Ua Multiple Provider User/DC=%HOSTNAME%"
goto done

:error_no_ip
echo.
echo Cannot determine IP address automatically.
echo Please invoke this batch using parameters to specify IP address.
echo.
goto usage

: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
