@echo off
title %~nx0
set local
cls


:START
    echo.
    echo                   Copyright -- Matthew Shillam -- 2018
    echo.
    echo ---------------------------------------------------------------------------
    echo               %date% %time% - Duplicati Install started
    echo ---------------------------------------------------------------------------
    echo.

    set RESTART=0



:GETOSVERSION

    SET OSVersion=Unknown
 
    VER | FINDSTR /L "5.0" > NUL
    IF %ERRORLEVEL% EQU 0 SET OSversion="2000"& SET OSDescription="Windows 2000" 
     
    VER | FINDSTR /L "5.1" > NUL
    IF %ERRORLEVEL% EQU 0 SET OSversion="XP"& SET OSDescription="Windows XP"

    VER | FINDSTR /L "5.2" > NUL
    IF %ERRORLEVEL% EQU 0 SET OSversion="2003"& SET OSDescription="Windows XP Pro x64 | Windows Server 2003"
     
    VER | FINDSTR /L "6.0" > NUL
    IF %ERRORLEVEL% EQU 0 SET OSversion="Vista"& SET OSDescription="Windows Vista | Windows Server 2008"
     
    VER | FINDSTR /L "6.1" > NUL
    IF %ERRORLEVEL% EQU 0 SET OSversion="7"& SET OSDescription="Windows 7 | Windows Server 2008 R2"

    VER | FINDSTR /L "6.2" > NUL
    IF %ERRORLEVEL% EQU 0 SET OSversion="8"& SET OSDescription="Windows 8 | Windows Server 2012"

    VER | FINDSTR /L "6.3" > NUL
    IF %ERRORLEVEL% EQU 0 SET OSversion="8.1"& SET OSDescription="Windows 8.1 | Windows Server 2012 R2"

    VER | FINDSTR /L "10.0" > NUL
    IF %ERRORLEVEL% EQU 0 SET OSversion="10"& SET OSDescription="Windows 10 | Windows Server 2016"

    IF %OSVersion%==Unknown (
        
        echo.
        echo  Unable to Determine your operating system, it must be old!

    
    ) ELSE (

        echo.
        echo  You appear to be using %OSDescription%
    )



:DUPLICATIINSTALL
	echo .
	echo Installing duplicati binary...
	echo Ensure that you deselect run at startup to ensure service works proper!
    echo Also! Ensure you do not run duplicati after install has completed!!
    start /wait msiexec /i %~dp0bin\duplicati-2.0.3.11_canary_2018-09-05-x64.msi
    goto GETPROGRAMPATH



:GETPROGRAMPATH
    
    SET NUL=NUL
    IF "%OS%"=="Windows_NT" SET NUL=
    echo.
    echo ---------------------------------------------------------------------------
    IF EXIST "C:\Program Files (x86)\Duplicati 2\%NUL%" ( SET "PROGRAMFOLDER=C:\Program Files (x86)\Duplicati 2\" )& echo  Program path is: "C:\Program Files (x86)\Duplicati 2\"
    IF EXIST "C:\Program Files\Duplicati 2\%NUL%" (SET "PROGRAMFOLDER=C:\Program Files\Duplicati 2\")& echo  Program path is: "C:\Program Files\Duplicati 2\"
    REM IF "%PROGRAMFOLDER%" == "" echo Software was not installed... time to debug!& GOTO EXITNOW



:SETUPSERVICE

	set "execpath=%PROGRAMFOLDER%\Duplicati.WindowsService.exe"
	"%execpath%" install --webservice-interface=127.0.0.1 --webservice-port=8200 --portable-mode
	echo .
	NET START Duplicati



:GETCLIENTNAME
    echo Enter client name ( In email eg: client-mat-is-ace )? :
    set /p CLIENTNAME=
    echo.
    echo ---------------------------------------------------------------------------
    echo  Ok -- %CLIENTNAME% it is...
    echo ---------------------------------------------------------------------------
    echo.
    goto GETSCHEDULES



:GETSCHEDULES
	set FILEXRAYS="schedule-xrays-%CLIENTNAME%.json"
	set FILEDATABASE="schedule-database-%CLIENTNAME%.json"
	set FILEDOCUMENTS="schedule-documents-%CLIENTNAME%.json"

	%~dp0bin\winscp.com /ini=nul /command ^
	    "open sftp://mrdownloader@mydentalbackup.co.uk:5349/ -hostkey=""ssh-ed25519 256 PTL6imOWXdhJQEuBThd8yfozcuOtOxpKEZs0hOGZh3c="" -privatekey=""%~dp0bin\id_deployer2.ppk""" ^
	    "cd /home/mydentalbackup.co.uk/files" ^
	    "lcd %~dp0bin" ^
	    "get %FILEDATABASE%" "get %FILEDOCUMENTS%" "get %FILEXRAYS%" ^
	    "close" ^
	    "exit"

	goto ADDSCHEDULES



:ADDSCHEDULES

	%~dp0bin\duplicati_client.exe login http://127.0.0.1:8200
	%~dp0bin\duplicati_client.exe create backup %~dp0bin\schedule-database-%CLIENTNAME%.json
	%~dp0bin\duplicati_client.exe create backup %~dp0bin\schedule-documents-%CLIENTNAME%.json
	%~dp0bin\duplicati_client.exe create backup %~dp0bin\schedule-xrays-%CLIENTNAME%.json

goto SORTCHORTCUTS


:SORTCHORTCUTS

    echo Removing shortcut from desktop...
    echo. 
    del "%USERPROFILE%\Desktop\Duplicati 2.lnk"
    echo.
    echo Adding new shortcut to desktop
    xcopy "%~dp0bin\Duplicati 2 start.lnk" %USERPROFILE%\Desktop
	

:EXITNOW
    echo.
    echo.
    echo ---------------------------------------------------------------------------
    echo.
    echo  Ok -- if all went well you should see no errors above... Scroll up 
    echo  and check!!! ( yes you John! )
    echo.
    echo ---------------------------------------------------------------------------

    echo. & echo %date% %time% - %~nx0 ended & echo.

Cmd /k

:EXITCLOSE