@echo off

set CHAIN=mainnet
set ACTION=
set ACCOUNT=
set PRI_PASS=
set PUB_PASS=
set MNEMONICS=
set NUM_ADDR=10

setlocal enabledelayedexpansion

goto :main

:main
    @REM Step 0: Parsing args.
    call :parse_args %*
    if "%ACCOUNT%" == "" if "%ACTION%" == "" if %PRI_PASS% == "" if %PUB_PASS% == "" goto :exit_abnormal

    @REM Step 1: Set variables.
    set RELEASE_DIR=%ABEWALLET_RELEASE_HOME%
    set PROG="%RELEASE_DIR%\abewallet"

    @REM Step 2: Make a temporary working directory and generate scripts.
    call :uniq_work_dir
    set WORK_DIR=%WORK_DIR%
    set JOB_SCRIPT_NAME=make-account-%ACCOUNT%@%CHAIN%.bat
    set TMP_WALLET_DATA_DIR=%WORK_DIR%\wallet-data
    md %WORK_DIR%

    set CHAIN_OPT=
    if "%CHAIN%"=="testnet" (
        set CHAIN_OPT=--testnet
    ) else if "%CHAIN%"=="simnet" (
        set CHAIN_OPT=--simnet
    )

    echo @echo off>> %WORK_DIR%\%JOB_SCRIPT_NAME%
    echo=>> %WORK_DIR%\%JOB_SCRIPT_NAME%
    echo %PROG% ^^>> %WORK_DIR%\%JOB_SCRIPT_NAME%
    echo   --appdata="%TMP_WALLET_DATA_DIR%" ^^>> %WORK_DIR%\%JOB_SCRIPT_NAME%
    echo   --logdir="%TMP_WALLET_DATA_DIR%\logs" ^^>> %WORK_DIR%\%JOB_SCRIPT_NAME%
    echo   --noninteractivecreate %CHAIN_OPT% ^^>> %WORK_DIR%\%JOB_SCRIPT_NAME%
    echo   %%*>> %WORK_DIR%\%JOB_SCRIPT_NAME%

    @REM Step 3: Define and execute.
    cd %WORK_DIR%
    if "%ACTION%"=="create" (
        call :do_create_account
    ) else if "%ACTION%"=="import" (
        call :do_import_account
    )

    @REM Step 4: Parse output to get result.
    set output_mnemonics=
    set line_number=1
    for /f "usebackq delims=" %%a in ("stdout.txt") do (
        if "!line_number!" == "4" set output_mnemonics=%%a
        set /a line_number+=1
    )
    del "stdout.txt"

    @REM Step 5: Output result to stdout.
    echo %WORK_DIR%
    echo %output_mnemonics%
    echo=
    goto :EOF

:usage
    echo Usage: %0 [-c CHAIN(mainnet^|testnet^|simnet)] -a ACTION(create^|import) [-m MNEMONICS] [-r NUM_ADDR]
    echo    -x PRIVATE_PASSPHRASE -y PUBLIC_PASSPHRASE ACCOUNT
    goto :EOF

:exit_abnormal
    call :usage
    exit /b 1

:parse_args
    set next_line=

    for %%i in (%*) do (
        if !next_line! == "-c" (
            set CHAIN=%%i
            if not "!CHAIN!"=="mainnet" if not "!CHAIN!"=="testnet" if not "!CHAIN!"=="simnet" goto exit_abnormal
        )
        if !next_line! == "-a" (
            set ACTION=%%i
            if not "!ACTION!"=="create" if not "!ACTION!"=="import" goto exit_abnormal
        )
        if !next_line! == "-x" set PRI_PASS=%%i
        if !next_line! == "-y" set PUB_PASS=%%i
        if !next_line! == "-r" set NUM_ADDR=%%i
        if !next_line! == "-m" set MNEMONICS=%%i

        if "%%i" == "-c" (
            set next_line="-c"
        ) else if "%%i" == "-a" (
            set next_line="-a"
        ) else if "%%i" == "-x" (
            set next_line="-x"
        ) else if "%%i" == "-y" (
            set next_line="-y"
        ) else if "%%i" == "-r" (
            set next_line="-r"
        ) else if "%%i" == "-m" (
            set next_line="-m"
        ) else (
            set next_line=
        )

        set ACCOUNT=%%i
    )

:uniq_work_dir
    set WORK_DIR=%tmp%\abelwallet.%ACCOUNT%.XXXXX.%RANDOM%
    if exist "%WORK_DIR%" goto :uniq_work_dir
    exit /B

:do_create_account
    call %WORK_DIR%\%JOB_SCRIPT_NAME% ^
      --mypassword=%PRI_PASS% ^
      --mywalletpass=%PUB_PASS% > stdout.txt 2> stderr.txt
    EXIT /B

:do_import_account
    call %WORK_DIR%\%JOB_SCRIPT_NAME% ^
      --mypassword=%PRI_PASS% ^
      --mywalletpass=%PUB_PASS% ^
      --myversion=0 ^
      --withmnemonic ^
      --mymnemonic=%MNEMONICS% ^
      --myrestorenumber=%NUM_ADDR% > stdout.txt 2> stderr.txt
    EXIT /B

endlocal