:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Setting Parameters
Set Destination=%ProgramFiles%\SIX Payment Services Ltd\MPD(Multi Protocol Driver)
Set TraceDestination=%SystemDrive%\Trace
Set Other=/NoProbe /AutoStart
Set NoSerialPortScan=/NoSerialPortScan


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Testing Administartive permissions
@echo off
cls
ECHO.
Echo Administrative permissions required. Detecting ...

Net Session >nul 2>&1
If %errorLevel% == 0 (
   Echo Success: Administrative permissions confirmed.
   goto Install
) else (
   COLOR 0C
   ECHO.
   ECHO.
   ECHO.ERROR: Logged-on user does not have administrative privilege!
   ECHO.
   ECHO.Logon as administrator or try right click on %0 and select 'Run as administrator'
   ECHO.
   ECHO.
   PAUSE
   COLOR 07 
   cls
   Exit /B 1
)


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: starting installation
:Install
CD /D "%~dp0"

@echo off

Set /p Version= < version.srv

Set ServiceDisplayName=SIX MPD(Multi Protocol Driver)

Set TID=%1

IF not "%TID%"=="" GOTO setTID 
Set /P TID="Enter the Terminal ID (TID): "


:setTID
Set TID=/TID %TID%


@echo on


Set StartUpParameter=%Other% %NoSerialPortScan% /TraceDir %TraceDestination% %TID%

net stop eftmpd

".\%Version%\Windows\eftdvs.exe" /UnRegServer

md "%Destination%"
md "%TraceDestination%"

copy ".\%Version%\Windows\dzip32.dll" "%Destination%" /Y
copy ".\%Version%\Windows\eftdvs.exe" "%Destination%" /Y

del "%Destination%\eftdvscfg.xml" 
ECHO %StartUpParameter% > "%Destination%\eftdvs.cfg"
"%Destination%\eftdvs.exe" /Service /Autostart

sc config eftmpd displayname= "%ServiceDisplayName%"
sc description eftmpd "Runs SIX MPD(Multi Protocol Driver) %Version%"
sc failure "eftmpd" reset= 300 command= "" actions= restart/10000/restart/20000/restart/30000

:: Get windows Version numbers
For /f "tokens=2 delims=[]" %%G in ('ver') Do (set _version=%%G) 
For /f "tokens=2,3,4 delims=. " %%G in ('echo %_version%') Do (set _major=%%G& set _minor=%%H& set _build=%%I) 
if %_major% GTR 5 goto Vista_and_Newer

:XP_and_Older
netsh firewall add portopening protocol=TCP port=8138 name="%ServiceDisplayName% allow inbound  TCP-8138" mode=ENABLE scope=ALL
netsh firewall add portopening protocol=UDP port=33333 name="%ServiceDisplayName% allow outbound UDP-33333" mode=ENABLE scope=ALL
GOTO StartService

:Vista_and_Newer
netsh advfirewall firewall delete rule name="%ServiceDisplayName% allow inbound  TCP-8138"
netsh advfirewall firewall delete rule name="%ServiceDisplayName% allow outbound UDP-33333"
netsh advfirewall firewall add rule name="%ServiceDisplayName% allow inbound  TCP-8138" protocol=TCP dir=in  localport=8138  action=allow
netsh advfirewall firewall add rule name="%ServiceDisplayName% allow outbound UDP-33333" protocol=UDP dir=out localport=33333 action=allow
GOTO StartService

:StartService
net start eftmpd

:END

Set TID=
Set Destination=
Set Other=
Set NoSerialPortScan=
Set StartUpParameter=
Set Version=

pause
