@echo OFF
REM script creates mongodb replica set with 1 primary, 1 secondary, 1 hidden secondary and 1 arbiter.
REM nodes are started on ports 27018, 27019, 27020, 27021
IF "%OS%"=="Windows_NT" setlocal enabledelayedexpansion enableextensions

REM reset variables
set "MONGO_ROOT="
set "MONGODB_PATH="
set "REPL_NAME="
set "REPL_INIT="

REM parse incoming paramenters
:parse_args
set "arg=%~1"
rem @echo "arg value: %arg%"
if [%1]==[] goto script_start
if "%1"=="-mongoRoot" goto set_mongoroot
if "%1"=="-mr" goto set_mongoroot
if "%1"=="-mongoDbPath" goto set_mongodbpath
if "%1"=="-mdp" goto set_mongodbpath
if "%1"=="-replName" goto set_replname
if "%1"=="-rn" goto set_replname
if "%1"=="-replInit" goto set_replinit
if "%1"=="-ri" goto set_replinit

goto :error

:set_mongoroot
set "MONGO_ROOT=%~2"
rem @echo "Mongo root: %MONGO_ROOT%"
shift
shift
goto parse_args

:set_mongodbpath
set "MONGODB_PATH=%~2"
shift
shift
goto parse_args

:set_replname
set "REPL_NAME=%~2"
rem @echo "Repl name: %REPL_NAME%"
shift
shift
goto parse_args

:set_replinit
set "REPL_INIT=replSetInitiate"
rem @echo "Repl init: %REPL_INIT%"
shift
goto parse_args

:script_start
REM set input params to defaults if they are empty
if "%MONGO_ROOT%"=="" set "MONGO_ROOT=C:\Program Files\MongoDB\Server\3.2\bin"
if "%REPL_NAME%"=="" set "REPL_NAME=rs0"
set "NOD1PORT=27018"
set "NOD2PORT=27019"
set "NOD3PORT=27020"
set "ARBPORT=27021"
if "%MONGODB_PATH%"=="" set "MONGODB_PATH=C:\mongodb\%REPL_NAME%"
set "NOD1_DIR=1"
set "NOD2_DIR=2"
set "NOD3_DIR=3"
set "ARB_DIR=arb"

@echo Preparing db folders
REM creating node1 /data and /log folders
mkdir "%MONGODB_PATH%\%NOD1_DIR%\data"
@echo Folder %MONGODB_PATH%\%NOD1_DIR%\data created
mkdir "%MONGODB_PATH%\%NOD1_DIR%\log"
@echo Folder %MONGODB_PATH%\%NOD1_DIR%\data created
REM creating node2 /data and /log folders
mkdir "%MONGODB_PATH%\%NOD2_DIR%\data"
@echo Folder %MONGODB_PATH%\%NOD2_DIR%\data created
mkdir "%MONGODB_PATH%\%NOD2_DIR%\log"
@echo Folder %MONGODB_PATH%\%NOD2_DIR%\data created
REM creating node3 /data and /log folders
mkdir "%MONGODB_PATH%\%NOD3_DIR%\data"
@echo Folder %MONGODB_PATH%\%NOD3_DIR%\data created
mkdir "%MONGODB_PATH%\%NOD3_DIR%\log"
@echo Folder %MONGODB_PATH%\%NOD3_DIR%\data created
REM creating arbiter /data and /log folders
mkdir "%MONGODB_PATH%\%ARB_DIR%\data"
@echo Folder %MONGODB_PATH%\%ARB_DIR%\data created
mkdir "%MONGODB_PATH%\%ARB_DIR%\log"
@echo Folder %MONGODB_PATH%\%ARB_DIR%\data created

@echo "Mongo root path: %MONGO_ROOT%"
if not exist "%MONGO_ROOT%\mongod.exe" (
    @echo "Cannot find mongod.exe by path '%MONGO_ROOT%'"
    goto :error
)

@echo "Replica set name: %REPL_NAME%"
IF "%REPL_NAME%"=="" (
    @echo "Replica set name is missing. Please provide the replica set name."
    goto :error
)

REM set variable pointing to mongod.exe
set "MONGOD=%MONGO_ROOT%\mongod.exe"
REM command line to start mongo instance in replica set mode
set "NOD1PARAM=--port %NOD1PORT% --dbpath %MONGODB_PATH%\%NOD1_DIR%\data --logpath %MONGODB_PATH%\%NOD1_DIR%\log\mongod.log --replSet %REPL_NAME%"
REM start mongod instance 1 (used as primary)
rem use /k argument if you want to hold the cmd window open
start cmd /c ""%MONGOD%" %NOD1PARAM%"
timeout /T 1

REM command line to start mongo instance for node2 in replica set mode
set "NOD2PARAM=--port %NOD2PORT% --dbpath %MONGODB_PATH%\%NOD2_DIR%\data --logpath %MONGODB_PATH%\%NOD2_DIR%\log\mongod.log --replSet %REPL_NAME%"
REM start mongod instance 2 (used as secondary)
start cmd /c ""%MONGOD%" %NOD2PARAM%"
timeout /T 1

REM command line to start mongo instance for node3 in replica set mode
set "NOD3PARAM=--port %NOD3PORT% --dbpath %MONGODB_PATH%\%NOD3_DIR%\data --logpath %MONGODB_PATH%\%NOD3_DIR%\log\mongod.log --replSet %REPL_NAME%"
REM start mongod instance 3 (used as hidden)
start cmd /c ""%MONGOD%" %NOD3PARAM%"
timeout /T 1

REM command line to start mongo instance for node3 in replica set mode
set "ARBPARAM=--port %ARBPORT% --dbpath %MONGODB_PATH%\%ARB_DIR%\data --logpath %MONGODB_PATH%\%ARB_DIR%\log\mongod.log --replSet %REPL_NAME%"
REM start mongod instance 3 (used as arbiter)
start cmd /c ""%MONGOD%" %ARBPARAM%"
timeout /T 1

REM to configure node as arbiter add arbiterOnly:true field to node configuration
REM to configure node as hidden secondary add hidden:true,priority:0 to node configuration
set "ADMINCMD=db.adminCommand({'%REPL_INIT%':{_id:'%REPL_NAME%',members:[{_id:0,host:'localhost:%NOD1PORT%'},{_id:1,host:'localhost:%NOD2PORT%'},{_id:2,host:'localhost:%NOD3PORT%',hidden:true,priority:0},{_id:3,host:'localhost:%ARBPORT%',arbiterOnly:true}]}})"
set "HOSTNOD=localhost:%NOD1PORT%"
set "STATSCMD=db.adminCommand({'replSetGetStatus':1})"
set "RSCONFCMD=rs.conf()"
if not "%REPL_INIT%"=="" (
    REM initialize replica set
    @echo "Running command: %MONGO_ROOT%\mongo %HOSTNOD% --eval %ADMINCMD%"
    start cmd /c ""%MONGO_ROOT%\mongo" %HOSTNOD% --eval %ADMINCMD% & timeout /T 2 & "%MONGO_ROOT%\mongo" %HOSTNOD% --eval %RSCONFCMD% & timeout /T 10 & "%MONGO_ROOT%\mongo" %HOSTNOD% --eval %STATSCMD% & PAUSE"
) 

rem @echo "replica set has been setup successfully!"
REM go to end of file if execution has been completed successfully.
goto :eof

:error
@echo "Error occurred. Make sure parameters are correct."
goto :eof