# @summary Installs the Desktop Central Agent
#
# Installs the Desktop Central Agent
#
# @example
#   include desktopcentral_agent::install
class desktopcentral_agent::install { 

  # Vars
  $source_file_linux = 'puppet:///modules/desktopcentral_agent/UEMSLinuxAgent.zip'
  $source_file_windows = 'puppet:///modules/desktopcentral_agent/LocalOffice_Agent.exe'
  $download_path_linix = '/var/cache/UEMSLinuxAgent.zip'
  $download_path_windows = 'C:\ProgramData\Package Cache\LocalOffice_Agent.exe'
  $archive_file_linux = 'UEMSLinuxAgent.zip'
  $extract_path_linux = '/var/cache/UEMSLinuxAgent'
  $extract_creates_linux = "${extract_path_linux}/UEMS_LinuxAgent.bin"
  $package_name_windows = 'ManageEngine Endpoint Central - Agent'
  

  case $::osfamily {
    'RedHat','Debian': {
      include 'archive'
      $source_file = $source_file_linux
      $download_path = $download_path_linix
      $archive_file = $archive_file_linux
      $extract_path = $extract_path_linux
      $extract_creates = $extract_creates_linux

      package { 'unzip':
        ensure => present,
      }

      # Create the package extraction folder
      file { $extract_path:
          ensure => 'directory',
      }

      # Download and Extract the package
      archive { $download_path:
        source       => $source_file,
        extract      => true,
        extract_path => $extract_path,
        creates      => $extract_creates,
        cleanup      => false,
      }

      # Set the binary as executable
      file { "${extract_path}/UEMS_LinuxAgent.bin":
        mode => 'ug+x',
      }

      # Execute the binary
      exec { 'UEMS_LinuxAgent.bin':
        cwd       =>  $extract_path,
        command   => "/bin/bash UEMS_LinuxAgent.bin",
        creates   => '/usr/local/desktopcentralagent/RemoveUEMSAgent.sh',
        logoutput => true, 
      }
    } 

    'Windows': {
      $source_file = $source_file_windows
      $download_path = $download_path_windows
      $package_name =  $package_name_windows 


      # Create the package extraction folder
      file { $download_path :
          ensure => present,
          source => $source_file,
      }
      
      # Install the package
      package {  $package_name:
        ensure          => installed,
        source          => $download_path_windows,
        install_options => ['/silent']
      }
    }   

  }
}