@echo off

if exist ..\SDRSharp.exe goto Download
echo SDRSharp not found
pause
exit

:Download
if not exist backup\ mkdir backup
copy /Y ..\*.* backup\
echo SDR# create a copy in the backup directory
pause
cls

set /p INP="Update SDRSharp and RTLSDR Y or N ? >"
if /i not %INP%==Y goto UpdateTSSDR

mkdir src
echo Downloading SDR# ...
httpget http://sdrsharp.com/downloads/sdr-nightly.zip src\sdr-nightly.zip

echo Downloading SDR#'s RTLSDR Plugin ...
httpget http://sdrsharp.com/downloads/sdr-nightly-rtlsdr.zip src\sdr-nightly-rtlsdr.zip

echo Downloading RTLSDR Driver ...
httpget http://sdr.osmocom.org/trac/raw-attachment/wiki/rtl-sdr/RelWithDebInfo.zip src\RelWithDebInfo.zip

mkdir tmp
unzip -o src\sdr-nightly.zip -d ..
unzip -o src\sdr-nightly-rtlsdr.zip -d tmp
move /Y tmp\SDRSharp.RTLSDR.dll ..
move /Y tmp\msvcr100.dll ..
move /Y tmp\libusb-1.0.dll ..
move /Y tmp\libusb0.dll ..
move /Y tmp\config\SDRSharp.exe.config ..

echo Update RelWithDebInfo rtlsdr.dll ...
unzip -o src\RelWithDebInfo.zip -d tmp
move tmp\rtl-sdr-release\x32\rtlsdr.dll ..

pause 

:UpdateZadig
cls

set zadig_exe=zadig.exe
ver | findstr /l "5.1." > NUL
if %errorlevel% equ 0 set zadig_exe=zadig_xp.exe
if exist ..\%zadig_exe% goto DeleteWorkDirectory
echo Downloading Zadig ...
httpget http://zadig.akeo.ie/downloads/%zadig_exe% src\%zadig_exe%
echo Update zadig.zip ...
move src\%zadig_exe% ..

:DeleteWorkDirectory

echo delete work directory ...
rmdir src /S /Q
rmdir tmp /S /Q
pause

:UpdateTSSDR
cls
set /p INP="Update plugins from rtl-sdr.ru Y or N ? >"
if /i not %INP%==Y goto updateconfig

mkdir src

echo Downloading MF+  Plugin ...
httpget -np http://rtl-sdr.ru/uploads/download/freqmen.zip src\FreqMen.zip
unzip -o src\FreqMen.zip
copy /Y FreqMen\*.* ..
rmdir FreqMen /S /Q

echo Downloading WRP+ plugin ...
httpget -np http://rtl-sdr.ru/uploads/download/wavrec.zip src\WavRec.zip
unzip -o src\WavRec.zip
copy /Y WavRec\*.* ..
rmdir WavRec /S /Q

echo Downloading FS plugin ...
httpget -np http://rtl-sdr.ru/uploads/download/freqscanner.zip src\FreqScanner.zip
unzip -o src\FreqScanner.zip
copy /Y FreqScanner\*.* ..
rmdir FreqScanner /S /Q

echo Downloading DAP plugin ...
httpget -np http://rtl-sdr.ru/uploads/download/dap.zip src\dap.zip
unzip -o src\dap.zip
copy /Y dap\*.* ..
rmdir dap /S /Q

echo Downloading CTCSS Decoder plugin ...
httpget -np http://rtl-sdr.ru/uploads/download/ctcssdecoder.zip src\CTCSSDecoder.zip
unzip -o src\CTCSSDecoder.zip
copy /Y CTCSSDecoder\*.* ..
rmdir CTCSSDecoder /S /Q

echo Downloading TimeShift plugin ...
httpget -np http://rtl-sdr.ru/uploads/download/timeshift.zip src\timeshift.zip
unzip -o src\timeshift.zip
copy /Y timeshift\*.* ..
rmdir timeshift /S /Q

pause

:updatepluginsconfig
cls
set /p INP="Update Plugins.xml to default with all rtl-sdr.ru plugins Y or N ? >"
if /i %INP%==Y goto updateplugins
copy /Y backup\Plugins.xml ..
goto updateconfig

:updateplugins
echo Update Plugins.xml ...
httpget -np http://rtl-sdr.ru/uploads/download/plugins.zip src\plugins.zip
unzip -o src\plugins.zip
copy /Y plugins\*.* ..
rmdir plugins /S /Q

pause

:updateconfig
cls
set /p INP="Update SDR# config to default Y or N ? >"
if /i %INP%==Y goto end
copy /Y backup\sdrsharp.exe.config ..

pause
 
:end
cls 
echo delete work directory ...
rmdir src /S /Q

echo Installation is complete.
pause
exit
 