---
- hosts: localhost

  vars:
    ruby_version: 2.6.5

  handlers:
    - name: restart nginx
      service: name=nginx state=restarted

  tasks:
    - name: Install packages
      apt: name="{{item}}" state=present
      with_items:
        - htop
        - python3-psycopg2
        - ruby
        - sudo
        - nginx-extras
        - apt-transport-https
        - ca-certificates
        - libssl-dev
        - libreadline-dev
        - zlib1g-dev
        - libbz2-dev
        - libpq-dev
        - git
        - imagemagick
        - build-essential
        - certbot

    # - name: Remove conflicting packages
    #   apt: name=python-openssl state=absent

    # - name: Enable Jessie backports repository
    #   apt_repository: repo="deb http://ftp.debian.org/debian jessie-backports main"
    - name: Set up american locales to avoid warnings about missing locales
      locale_gen: name=en_US.UTF-8 state=present
    - name: Install PostgreSQL
      apt: name=postgresql state=present
    - name: Create PostgreSQL user
      become: yes
      become_user: postgres
      postgresql_user: name=debshots
    - name: Create PostgreSQL database
      become: yes
      become_user: postgres
      postgresql_db: name=debshots owner=debshots encoding=UTF-8 template=template0
    - name: Create application user
      user: name=debshots groups=adm home=/opt/debshots shell=/bin/bash

    # Install newer Ruby version using rbenv
    - name: Get rbenv
      become: yes
      become_user: debshots
      git:
        repo: https://github.com/rbenv/rbenv.git
        dest: /opt/debshots/.rbenv
        force: yes
    - name: Get rbenv-install
      become: yes
      become_user: debshots
      git:
        repo: https://github.com/rbenv/ruby-build.git
        dest: /opt/debshots/.rbenv/plugins/ruby-build
        force: yes
    - name: enable rbenv
      copy:
        src: files/debshots-bash-profile
        dest: /opt/debshots/.profile
        owner: debshots

    - name: Copy project to destination
      synchronize:
        # Path defaults to ansible/files
        src: ../..
        dest: /opt/debshots
        #delete: yes
        rsync_opts: --exclude=.git/,--exclude=log/,--exclude=tmp/cache,--exclude=storage/

    - name: Build Ruby version
      shell: rbenv install -v {{ruby_version}} creates=/opt/debshots/.rbenv/versions/{{ruby_version}}
      become: yes
      become_user: debshots
      become_flags: "-i"

    - name: Set Ruby version to version {{ruby_version}}
      shell: rbenv local {{ruby_version}}
      become: yes
      become_user: debshots
      become_flags: "-i"

    - name: Install Bundler
      shell: gem install bundler
      become: yes
      become_user: debshots
      become_flags: "-i"

    - name: Switch Bundler into deployment mode
      shell: bundle config set deployment 'true'
      args:
        chdir: /opt/debshots
      become: yes
      become_user: debshots
      become_flags: "-i"

    - name: Install dependencies
      shell: bundle install
      args:
        chdir: /opt/debshots
      become: yes
      become_user: debshots
      become_flags: "-i"

    - name: Pre-render the static assets
      shell: bundle exec rails assets:precompile RAILS_ENV=production
      args:
        chdir: /opt/debshots
      become: yes
      become_user: debshots
      become_flags: "-i"

    - name: Deploy nginx config
      copy:
        src: files/nginx.conf
        dest: /etc/nginx/sites-available/default
      notify: restart nginx
#  - debug: msg="Please run: certbot certonly --webroot -w /opt/debshots/public -d screenshots.debian.net"
