:: *** Knoks Ltd. by Vlad Amirov - 23/05/2017 ***
@ECHO OFF
TITLE Knoks API - deployment to d01 environmen script

ECHO === set vars =================================================================================
FOR %%A IN ("%~dp0\..") DO SET "PROJECT_DIR=%%~fA"
SET BACH_FILE_NAME_WITH_EXTENTION=%~n0%~x0
SET BACH_FILE_NAME_WITHOUT_EXTENTION=%~n0
SET SRC_SYNC=%PROJECT_DIR%\bin\Debug\netcoreapp1.1\publish
SET DST_SYNC=/home/ec2-user/ldi/Knoks.Api/publish
SET PRIVATEKEY_FILE=C:\Users\user\Dropbox\Dev\LDI\AWS\LDI-DEV-KP.ppk

ECHO Project directory: %PROJECT_DIR%
ECHO Bach file name with extention: %BACH_FILE_NAME_WITH_EXTENTION%
ECHO Bach file name withou extention: %BACH_FILE_NAME_WITHOUT_EXTENTION%
ECHO Source: %SRC_SYNC%
ECHO Destination: %DST_SYNC%
ECHO Private key file: %PRIVATEKEY_FILE%
ECHO.

ECHO === detect sync mode (update or preview) =====================================================
IF "%~1"=="u" (
    SET PREVIEW_OPT=
	ECHO Sync mode: UPDATE!
) ELSE (
    SET PREVIEW_OPT=-preview
	ECHO Sync mode: Preview
)
ECHO.

::ECHO === git pull by user's answer ================================================================
::CHOICE /N /C:yn /M "Run 'git pull' ? (y/n)"%1
::IF ERRORLEVEL 2 GOTO NoGitPull
::git pull
:::NoGitPull
::ECHO.

ECHO === compile and publish application ==========================================================
dotnet publish ..\Knoks.Api.csproj

ECHO === create logs folder if not exists for WinSCP.com ==========================================
IF EXIST "%PROJECT_DIR%\Deploy\Logs" (
    ECHO Logs folder already exists
) ELSE (
	ECHO Creat Logs folder
    MKDIR "%PROJECT_DIR%\Deploy\Logs"
)

ECHO === run WinSCP ===============================================================================
::help link: https://winscp.net/eng/docs/scriptcommand_synchronize
"C:\Users\user\Dropbox\Dev\LDI\WinSCP\WinSCP.com" /log="%PROJECT_DIR%\Deploy\Logs\%BACH_FILE_NAME_WITHOUT_EXTENTION%.log" /ini=nul /command ^
    "open scp://ec2-user@ec2-54-167-223-45.compute-1.amazonaws.com/ -hostkey=""ssh-ed25519 256 89:be:f3:ef:6b:64:fa:a0:e2:cc:01:e7:16:fd:a3:f7"" -privatekey=""%PRIVATEKEY_FILE%"" -rawsettings AuthGSSAPI=1 TimeDifference=""000000000000C03F"" TimeDifferenceAuto=0 SendBuf=0 SshSimple=0" ^
	"call cd $HOME/ldi/Knoks.Api" ^
	"call ./api-kill.sh" ^
	"call ./api-set-backup.sh" ^
	"synchronize remote %PREVIEW_OPT% -delete -preservetime -transfer=automatic %SRC_SYNC% %DST_SYNC%" ^
	"call ./api-set-restore.sh" ^
	"call ./api-run.sh" ^
	"exit"
ECHO.
	
ECHO === script end ===============================================================================
ECHO Script completed! Press any key to exit...
PAUSE > NUL
