---
- name: Dependencies installed
  community.general.pkgng:
    name: git,go,openblas,cblas,dlib-cpp,lapacke,libjpeg-turbo,npm-node14,darktable,ffmpeg,p5-Image-ExifTool,pkgconf,libheif,bash
    state: latest

- name: Source folder exists
  file:
    path: /usr/local/src
    owner: root
    mode: 0755
    state: directory

- name: Libraries for jpeglib linked
  file: src=/usr/local/include/jpeglib.h dest=/usr/include/jpeglib.h state=link

- name: Libraries for jconfig linked
  file: src=/usr/local/include/jconfig.h dest=/usr/include/jconfig.h state=link

- name: Libraries for jmorecfg linked
  file: src=/usr/local/include/jmorecfg.h dest=/usr/include/jmorecfg.h state=link

- name: Libraries for dlib linked
  file: src=/usr/local/include/dlib dest=/usr/include/dlib state=link

- name: Libraries for lapacke linked
  file: src=/usr/local/include/lapacke.h dest=/usr/include/lpacke.h state=link

- name: Libraries for lapacke linked
  file: src=/usr/local/include/lapacke_utils.h dest=/usr/include/lapacke_utils.h state=link

- name: Libraries for lapacke linked
  file: src=/usr/local/include/lapacke_mangling.h dest=/usr/include/lapacke_mangling.h state=link

- name: Library for dlib linked
  file: src=/usr/local/lib/libdlib.so dest=/usr/lib/libdlib.so state=link

- name: Libraries for cblas linked
  file: src=/usr/local/include/cblas.h dest=/usr/include/cblas.h state=link

- name: Link lapack libraries
  shell: "ln -s /usr/local/lib/liblapack* /usr/lib/"
  ignore_errors: true

- name: Link jpeg libraries
  shell: "ln -s /usr/local/lib/libjpeg* /usr/lib/"
  ignore_errors: true

- name: Link cblas libraries
  shell: "ln -s /usr/local/lib/libcblas* /usr/lib/"
  ignore_errors: true

- name: Link openblas libraries
  shell: "ln -s /usr/local/lib/libblas* /usr/lib/"
  ignore_errors: true

- name: Photoview source @ master
  git:
    repo: "https://github.com/photoview/photoview.git"
    dest: /usr/local/src/photoview
    update: yes
    force: yes

# We have to comment out:
#    vi ~/go/pkg/mod/github.com/strukturag/libheif@v1.12.0/go/heif/heif.go
#    line 269
- name: Hack libheif to work with FreeBSD
  shell:
    cmd: 'sed -i -e "/suberrorwrongtileimagepixeldepth = C.heif_suberror_Wrong_tile_image_pixel_depth/d"  /root/go/pkg/mod/github.com/strukturag/libheif@v1.12.0/go/heif/heif.go'
    warn: false

- name: API env file installed
  template:
    src: api.env.j2
    dest: /usr/local/src/photoview/api/.env
    owner: root
    mode: 0600

- name: UI env file installed
  template:
    src: ui.env.j2
    dest: /usr/local/src/photoview/ui/.env
    owner: root
    mode: 0600

- name: Photoview UI installed
  shell:
    cmd: npm install && npm run build
    chdir: /usr/local/src/photoview/ui

- name: Photoview API built
  shell:
    cmd: go build .
    chdir: /usr/local/src/photoview/api

- name: Nginx location file installed
  ansible.builtin.copy:
    src: nginx.conf
    dest: /usr/local/etc/nginx/locations/photoview.conf
  notify: Nginx restarted

- name: Photoview immortal file installed
  template:
    src: immortal.yml.j2
    dest: /usr/local/etc/immortal/photoview.yml
    owner: root
    mode: 0664
  notify: Immortal restarted
