---
- name: Dependencies installed
  community.general.pkgng:
    name: git,rust,py38-cffi,py38-cryptography,py38-lxml,py38-joblib,py38-numpy,py38-pip,py38-pycparser,py38-pyinotify,py38-scikit-learn,py38-scipy,py38-setuptools,py38-six,py38-sqlite3,python38,py38-psycopg,ImageMagick7-nox11,mime-support,optipng,gnupg,liberation-fonts-ttf,xorg-fonts-truetype,unpaper,ghostscript9-base,qpdf,icc-profiles-adobe-cs4,icc-profiles-openicc,icc-profiles-basiccolor,libxml2,pngquant,tesseract,go,gnuplot,chromium,pdftk,libreoffice,unoconv,npm
    state: latest

- name: Paperless-ng source cloned
  git:
    repo: "https://github.com/jonaswinkler/paperless-ng"
    dest: /usr/local/src/paperless-ng/
    update: yes
    force: yes

- name: Scikit and scipy commented out of requierments
  shell:
    chdir: /usr/local/src/paperless-ng
    cmd: 'sed -i -e "/scikit/d"  requirements.txt && sed -i -e "/scipy/d" requirements.txt'
    warn: false

- name: Requirements up to date
  pip:
    requirements: /usr/local/src/paperless-ng/requirements.txt
    executable: pip

- name: Nginx location file installed
  copy:
    src: nginx-location.conf
    dest: /usr/local/etc/nginx/locations/paperless.conf
    mode: 0644
  notify: Nginx restarted

- name: Paperless conf file installed
  template:
    src: paperless.conf.j2
    dest: /usr/local/etc/paperless.conf
    owner: root
    mode: 0644

- name: Paperless frontend built
  shell:
    cmd: setenv NG_CLI_ANALYTICS ci && npm install && ./node_modules/.bin/ng build --prod
    chdir: /usr/local/src/paperless/src-ui

- name: Paperless migrations up to date
  command: python3.8 /usr/local/src/paperless-ng/src/manage.py migrate

- name: Paperless static files copied
  command: python3.8 /usr/local/src/paperless-ng/src/manage.py collectstatic --noinput

- name: Gotenberg source cloned
  git:
    repo: "https://github.com/gotenberg/gotenberg.git"
    dest: /usr/local/src/gotenberg/
    update: yes
    force: yes

- name: Gotenberg binary built
  shell:
    chdir: /usr/local/src/gotenberg/cmd/gotenberg
    cmd: go build .

- name: Gotenberg installed
  ansible.builtin.copy:
    src: /usr/local/src/gotenberg/cmd/gotenberg/gotenberg
    dest: /usr/local/bin/gotenberg
    remote_src: yes
    mode: a+x

- name: Tika server installed
  get_url:
    url: https://dlcdn.apache.org/tika/2.3.0/tika-server-standard-2.3.0.jar
    dest: /usr/local/bin/
    mode: a+x

- name: Paperless immortal file installed
  ansible.builtin.copy:
    src: immortal-paperless.yml
    dest: /usr/local/etc/immortal/paperless.yml
  notify: Immortal restarted

- name: qCluster immortal file installed
  ansible.builtin.copy:
    src: immortal-qcluster.yml
    dest: /usr/local/etc/immortal/qcluster.yml
  notify: Immortal restarted

- name: Tika immortal file installed
  ansible.builtin.copy:
    src: immortal-tika.yml
    dest: /usr/local/etc/immortal/tika.yml
  notify: Immortal restarted

- name: Gotenberg immortal file installed
  ansible.builtin.copy:
    src: immortal-gotenberg.yml
    dest: /usr/local/etc/immortal/gotenberg.yml
  notify: Immortal restarted

- name: Consumer immortal file installed
  ansible.builtin.copy:
    src: immortal-consumer.yml
    dest: /usr/local/etc/immortal/consumer.yml
  notify: Immortal restarted
