---
- hosts: all
  name: Install the MATLAB Compiler Runtime

  vars:
    tmpdir: /tmp
    mcr_installer: mcr/files/MCRInstaller.zip
    installdir: /tmp/MATLAB/MATLAB_Compiler_Runtime
    licensefile: ''
    # licensefile: "~/matlab/license.dat"
    # installkey: xxxxx-xxxxx-xxxxx-xxxxx.....

  tasks:
    - include: common/tasks/hosts.yml hosts=${groups.all}
    - include: common/tasks/hostname.yml

    - name: make temp directory
      action: file dest=$tmpdir/MCRInstaller state=directory

    - name: copy MCR installer zipfile
      action: copy src=$mcr_installer dest=$tmpdir/MCRInstaller/MCRInstaller.zip force=yes backup=no 

    - name: copy installation response file
      action: template src=mcr/templates/input.txt.j2 dest=$tmpdir/MCRInstaller/MCRInstaller.input force=yes backup=no

    - name: unzip installation files (may fail if `unzip` is not installed)
      action: command unzip -q -o $tmpdir/MCRInstaller/MCRInstaller.zip -d $tmpdir/MCRInstaller
      
    - name: Install license file, if present
      action: copy src=$licensefile dest=$tmpdir/MCRInstaller/license.dat force=yes backup=no
      only_if: "'$licensefile' != ''"

    - name: run MCR Installer
      action: command $tmpdir/MCRInstaller/install -inputFile $tmpdir/MCRInstaller/MCRInstaller.input

    - name: copy profile setup script
      action: template src=mcr/templates/profile.sh.j2 dest=/etc/profile.d/MATLAB_Compiler_Runtime.sh force=yes backup=no
      # Ignore errors since we may not be root on the remote machine
      ignore_errors: yes

    - name: cleanup temporary installation files
      action: command rm -r -f -v $tmpdir/MCRInstaller

  tags:
    - matlab
    - mcr
