---

- hosts: all

  tasks:
    - name: Install VNC-required system packages
      apt: pkg={{ item }} state=latest
      with_items:
        - firefox
        - lubuntu-desktop
        - vnc4server
        - xinetd

    - name: Create novnc install dir
      file: path={{ novnc_install_dir }} state=directory

    - name: Clone novnc
      git: repo=https://github.com/novnc/noVNC.git dest="{{ novnc_install_dir }}" version="stable/v0.6"

    - name: Clone websockify
      git: repo=https://github.com/novnc/websockify dest="{{ novnc_install_dir }}/utils/websockify"

    - name: Create ~/.vnc dir
      file: path=~/.vnc state=directory
      become_user: "{{ default_user }}"

    - name: Remove old vncpasswd dir
      file: path=~/vncpasswd state=absent
      become_user: "{{ default_user }}"

    - name: Clone vncpasswd
      git: repo=https://github.com/trinitronx/vncpasswd.py dest=~/vncpasswd
      become_user: "{{ default_user }}"

    - name: Create vnc password bridge
      command: python ~/vncpasswd/vncpasswd.py {{ vnc_password }} -f ~/.vnc/passwd
      become_user: "{{ default_user }}"
      when: vnc_password is defined

    - name: Chmod on vnc password file
      file: path=~/.vnc/passwd mode=0600
      become_user: "{{ default_user }}"
      when: vnc_password is defined

      # Move vnc_lite.html which takes vnc_password as query argument
      # to index.html and rewrite it so that password is autoset, no
      # need to specify via query parameter.
    - name: Autoset novnc passwd to match the vnc one
      shell: sed 's/password\ =/password\ = "{{ vnc_password }}";\/\//' {{ novnc_install_dir }}/vnc_auto.html > {{ novnc_install_dir }}/index.html #"

    - name: Remove temp vncpasswd dir
      file: path=~/vncpasswd state=absent
      become_user: "{{ default_user }}"

    - name: Copy script for starting X
      copy: src=xstartup dest=~/.vnc/xstartup mode=0755
      become_user: "{{ default_user }}"

    - name: Make sure /etc/X11/xinit/xinitrc has execute permissions set
      file: path=/etc/X11/xinit/xinitrc mode=0755

    - name: Copy novnc init file
      copy: src=novnc_init dest=/etc/init.d/novnc mode=0755

    - name: Copy novnc default file
      template: src=novnc_default.j2 dest=/etc/default/novnc

    - name: Copy vncserver default file
      template: src=vncserver_default.j2 dest=/etc/default/vncserver

    - name: Copy lightdm config file
      copy: src=lightdm.conf dest=/etc/lightdm/lightdm.conf

    - name: "Make sure lightdm respawns - fix for race condition: https://bugs.launchpad.net/ubuntu/+source/lightdm/+bug/969489"
      lineinfile: dest=/etc/init/lightdm.conf line="respawn" insertafter="stop on runlevel [016]"

    - name: "Remove a11y indicator to fix: https://bugs.launchpad.net/lightdm/+bug/1322275"
      replace: dest=/etc/lightdm/lightdm-gtk-greeter.conf.d/30_lubuntu.conf regexp="show-indicators=~host;~spacer;~clock;~spacer;~session;~language;~a11y;~power" replace="show-indicators=~host;~spacer;~clock;~spacer;~session;~language;~power"

    - name: Copy vnc config file for xinetd
      template: src=xinetd_vnc.j2 dest=/etc/xinetd.d/vnc

    - include: create_simple_service.yml src_file='novnc'

