---
# Pip install frappe-bench
- name: Installing pip frappe-bench
  pip:
    name:
      - frappe-bench

# Create {{ base }} directory
- name: Creating {{ base }} directory
  file:
    path: '{{ base }}'
    state: directory
    owner: '{{ superuser }}'
    group: '{{ superuser }}'
    mode: '0755'

# Run frappe bench init
- name: Running frappe bench init
  # NOTICE: running as superuser
  become_user: '{{ superuser }}'
  shell: bench init {{ projectname }} --frappe-branch version-12 --no-backups
  args:
    chdir: '{{ base }}'
    # Ensures this command only runs if the dir does NOT exist
    creates: '{{ path }}/apps/frappe'
  when: frappe_repo is undefined

# Run frappe bench init with custom repo
- name: Running frappe bench init with custom repo
  # NOTICE: running as superuser
  become_user: '{{ superuser }}'
  shell: bench init {{ projectname }} --frappe-path {{ frappe_repo }} --frappe-branch version-12 --no-backups
  args:
    chdir: '{{ base }}'
    # Ensures this command only runs if the dir does NOT exist
    creates: '{{ path }}/apps/frappe'
  when: frappe_repo is defined

# Create frappe-bench supervisor configs
- name: Creating frappe-bench supervisor config
  become_user: '{{ superuser }}'
  shell: bench setup supervisor
  args:
    chdir: '{{ path }}'
    creates: '{{ path }}/config/supervisor.conf'

# Symlink frappe-bench supervisor config
- name: Symlinking frappe-bench supervisor config
  file:
    src: '{{ path }}/config/supervisor.conf'
    dest: /etc/supervisor/conf.d/frappe-bench.conf
    state: link
  notify: reload supervisor

# Get ERPNext app
- name: Getting ERPNext app
  become_user: '{{ superuser }}'
  shell: bench get-app erpnext --branch version-12
  args:
    chdir: '{{ path }}'
    creates: '{{ path }}/apps/erpnext'
  notify: reload supervisor
  when: erpnext_repo is undefined

# Get ERPNext app from custom repo
- name: Getting ERPNext app from custom repo
  become_user: '{{ superuser }}'
  shell: bench get-app erpnext {{ erpnext_repo }} --branch version-12
  args:
    chdir: '{{ path }}'
    creates: '{{ path }}/apps/erpnext'
  notify: reload supervisor
  when: erpnext_repo is defined

# Settings frappe bench mariadb-host
- name: Setting frappe bench mariadb-host
  become_user: '{{ superuser }}'
  shell: bench set-mariadb-host {{ db_host }}
  args:
    chdir: '{{ path }}'
  when: db_host is defined

# Create new site using local database
- name: Creating new frappe site using local database
  become_user: '{{ superuser }}'
  shell: bench new-site {{ site }} --admin-password {{ erpnext_admin_password }} --db-name {{ db_name }} --db-password {{ erpnext_db_password }} --mariadb-root-username root --mariadb-root-password {{ root_db_password }} --force
  args:
    chdir: '{{ path }}'
    creates: '{{ path }}/sites/{{ site }}'
  notify: reload supervisor
  when: db_host is undefined

# Create new site using remote database
- name: Creating new frappe site using remote database
  become_user: '{{ superuser }}'
  shell: bench new-site {{ site }} --admin-password {{ erpnext_admin_password }} --db-name {{ db_name }} --db-password {{ erpnext_db_password }} --mariadb-root-username root --mariadb-root-password {{ root_db_password }} --force --no-mariadb-socket
  args:
    chdir: '{{ path }}'
    creates: '{{ path }}/sites/{{ site }}'
  notify: reload supervisor
  when: db_host is defined

# Adding ERPNext to site
- name: Adding ERPNext to site
  become_user: '{{ superuser }}'
  shell: bench --site {{ site }} install-app erpnext
  args:
    chdir: '{{ path }}'
    creates: '{{ path }}/sites/{{ site }}/ansible-flag-app-erpnext-added'
  notify: reload supervisor

# Create frappe-bench nginx configs
- name: Creating frappe-bench nginx config
  become_user: '{{ superuser }}'
  shell: bench setup nginx
  args:
    chdir: '{{ path }}'
    creates: '{{ path }}/config/nginx.conf'

# Delete /etc/nginx/sites-enabled/default
- name: Deleting /etc/nginx/sites-enabled/default
  file:
    path: /etc/nginx/sites-enabled/default
    state: absent
  notify: reload nginx

# Symlink frappe-bench nginx config
- name: Symlinking frappe-bench nginx config
  file:
    src: '{{ path }}/config/nginx.conf'
    dest: /etc/nginx/sites-enabled/erpnext.conf
    state: link
  notify: reload nginx
