; ShopfloorConnect MTC Service Installer
; Automated installation of Machine Tool Client Service for GE Aerospace

[Setup]
AppId={{F8A3B2C1-D4E5-6F7A-8B9C-0D1E2F3A4B5C}
AppName=WJDT ShopfloorConnect MTC Service
AppVersion=1.0
AppPublisher=WJDT
AppPublisherURL=http://tsgwp00524.logon.ds.ge.com
AppSupportURL=http://tsgwp00524.logon.ds.ge.com
AppUpdatesURL=http://tsgwp00524.logon.ds.ge.com
DefaultDirName=C:\ShopFloorConnect
CreateAppDir=yes
ChangesAssociations=no
PrivilegesRequired=admin
OutputDir=.\Output
OutputBaseFilename=ShopfloorConnect_Installer
SolidCompression=yes
WizardStyle=modern
SetupIconFile=gea-logo.ico
WizardImageFile=patrick.bmp
WizardSmallImageFile=patrick-sm.bmp
UninstallDisplayIcon={app}\Java\j2sdk\jre\bin\java.exe
DisableWelcomePage=no

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Messages]
WelcomeLabel2=This will install the ShopfloorConnect MTC Service on this machine.%n%nThe installer will:%n  1. Copy all required files to C:\ShopFloorConnect%n  2. Set environment variables%n  3. Install the MTC Service%n  4. Configure West Jefferson CA-LR connection settings%n%nConfiguration:%n  - Teamcenter Server: 10.233.113.141%n  - Shop Floor PC FQDN: Auto-detected%n  - Site: West Jefferson CA-LR

[Files]
; Copy ALL files from ShopFloorConnect folder
Source: "ShopFloorConnect\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs

[Dirs]
Name: "{app}\Log"

[Code]
// No wizard pages needed - batch file handles all configuration

procedure CurStepChanged(CurStep: TSetupStep);
var
  ResultCode: Integer;
  ErrorMsg: String;
begin
  if CurStep = ssPostInstall then
  begin
    // STEP 1: Set Environment Variable
    if not Exec('cmd.exe', '/c setx /m TCFITDIR "C:\ShopFloorConnect"', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) or (ResultCode <> 0) then
    begin
      MsgBox('Failed to set TCFITDIR environment variable!' + #13#10 +
             'Error Code: ' + IntToStr(ResultCode) + #13#10#13#10 +
             'You may need to set this manually in System Properties.',
             mbError, MB_OK);
      Exit;
    end;

    // STEP 2: Install MTC Service (this creates the FIT-MI_Server folder structure)
    // Note: Using echo | to automatically respond to any pause prompts in the batch file
    if not Exec('cmd.exe', '/c cd /d "C:\ShopFloorConnect\MachineToolClient_Service" && echo. | call install.bat', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
    begin
      MsgBox('Failed to execute MTC Service installer!' + #13#10#13#10 +
             'Please check if all files were copied correctly to:' + #13#10 +
             'C:\ShopFloorConnect\MachineToolClient_Service',
             mbError, MB_OK);
      Exit;
    end;

    // Wait for service installation to complete and files to settle
    Sleep(3000);

    // STEP 2a: Verify FIT-MI_Server folder was created
    if not DirExists('C:\ShopFloorConnect\FIT-MI_Server') then
    begin
      MsgBox('Installation Error: FIT-MI_Server folder not created!' + #13#10#13#10 +
             'The install.bat script did not create the expected folder structure.' + #13#10 +
             'This usually means the Ant build script failed.' + #13#10#13#10 +
             'Please check:' + #13#10 +
             '- TCFITDIR environment variable is set' + #13#10 +
             '- Java runtime exists at C:\ShopFloorConnect\Java\j2sdk' + #13#10 +
             '- You have write permissions to C:\ShopFloorConnect',
             mbError, MB_OK);
      Exit;
    end;

    // STEP 2b: Verify Windows service was registered
    if not Exec('cmd.exe', '/c sc query SFCMTCService', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) then
    begin
      MsgBox('Warning: Unable to query Windows service!' + #13#10#13#10 +
             'Could not check if SFCMTCService was registered.',
             mbError, MB_OK);
      Exit;
    end;

    if ResultCode <> 0 then
    begin
      MsgBox('Service Registration Failed!' + #13#10#13#10 +
             'The Windows service "SFCMTCService" was not created.' + #13#10 +
             'Error Code: ' + IntToStr(ResultCode) + #13#10#13#10 +
             'The installation may have completed, but the service was not registered.' + #13#10#13#10 +
             'You may need to manually register the service by running:' + #13#10 +
             'C:\ShopFloorConnect\FIT-MI_Server\Tomcat 5.0\bin\SFC_MTC_Tomcat_service.bat install',
             mbError, MB_OK);
      Exit;
    end;

    // STEP 3: Run West Jeff configuration batch file
    // This auto-detects FQDN and configures mi_server.ini with fsw_ip=10.233.113.141
    if not Exec('cmd.exe', '/c cd /d "C:\ShopFloorConnect" && call "3 - West_Jeff_CA_LR_Update_ini_File.bat"', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) or (ResultCode <> 0) then
    begin
      ErrorMsg := 'Failed to configure site settings!' + #13#10 +
                  'Error Code: ' + IntToStr(ResultCode) + #13#10#13#10 +
                  'The service may have been installed, but configuration failed.' + #13#10 +
                  'You may need to manually run:' + #13#10 +
                  'C:\ShopFloorConnect\3 - West_Jeff_CA_LR_Update_ini_File.bat';
      MsgBox(ErrorMsg, mbError, MB_OK);
      Exit;
    end;

    // STEP 3a: Verify the config file was created
    if not FileExists('C:\ShopFloorConnect\FIT-MI_Server\webapps\wut\WEB-INF\conf\mi_server.ini') then
    begin
      MsgBox('Configuration Error: mi_server.ini not found!' + #13#10#13#10 +
             'Expected file:' + #13#10 +
             'C:\ShopFloorConnect\FIT-MI_Server\webapps\wut\WEB-INF\conf\mi_server.ini' + #13#10#13#10 +
             'The service installation may have failed to create this file.',
             mbError, MB_OK);
      Exit;
    end;

    MsgBox('Installation Complete!' + #13#10#13#10 +
           'The ShopfloorConnect MTC Service has been installed and configured.' + #13#10#13#10 +
           'Windows Service: SFCMTCService (Registered)' + #13#10 +
           'Startup Type: Delayed Automatic' + #13#10 +
           'Site Configuration: West Jefferson CA-LR' + #13#10 +
           'Teamcenter Server: 10.233.113.141' + #13#10 +
           'Shop Floor PC FQDN: Auto-detected' + #13#10#13#10 +
           'Config file: C:\ShopFloorConnect\FIT-MI_Server\webapps\wut\WEB-INF\conf\mi_server.ini' + #13#10#13#10 +
           'To start the service:' + #13#10 +
           '  1. Open Services (services.msc)' + #13#10 +
           '  2. Find "SFCMTCService"' + #13#10 +
           '  3. Right-click → Start' + #13#10#13#10 +
           'Or run: sc start SFCMTCService',
           mbInformation, MB_OK);
  end;
end;

[UninstallDelete]
Type: filesandordirs; Name: "{app}\Log\*"
Type: filesandordirs; Name: "{app}\FIT-MI_Server\webapps\wut\WEB-INF\conf\*.tmp"
