- name: Record (initial) disk space used
  shell: df -B1 --output=used / | tail -1
  register: df1


- name: "Install packages for map installation: python3-geojson, python3-pil, python3-wget, php{{ php_version }}-sqlite3 (can also be installed by www_base/tasks/php-stem.yml)"
  package:
    state: present
    name:
      - python3-geojson
      - python3-pil
      - python3-wget
      #- php{{ php_version }}-common    # Auto-installed as an apt dependency.  REGARDLESS: php{{ php_version }}-common superset php{{ php_version }}-cli is auto-installed by php{{ php_version }}-fpm in nginx/tasks/install.yml
      - php{{ php_version }}-sqlite3

- name: Make 5 directories (0755 by default)
  file:
    path: "{{ item }}"
    state: directory
    # mode: 0755
  with_items:
    - "{{ vector_map_path }}/test-page/assets"    # /library/www/osm-vector-maps
    - "{{ vector_map_path }}/maplist/assets"
    - "{{ vector_map_path }}/viewer/assets"
    - "{{ vector_map_path }}/viewer/tiles"
    - "{{ vector_map_path }}/installer"

- name: Download 26MB {{ maps_assets_url }}/cities1000.sqlite to {{ vector_map_path }}/viewer/
  get_url:
    url: "{{ maps_assets_url }}/cities1000.sqlite"    # e.g. https://raw.githubusercontent.com/iiab/maps-assets/main
    dest: "{{ vector_map_path }}/viewer/"
    timeout: "{{ download_timeout }}"


- name: Download {{ osm_repo_url }}/{{ maps_branch }}/2020/map-catalog.json to {{ iiab_etc_path }}
  get_url:
    url: "{{ osm_repo_url }}/{{ maps_branch }}/2020/map-catalog.json"    # e.g. https://raw.githubusercontent.com/iiab/maps/master
    dest: "{{ iiab_etc_path }}"    # /etc/iiab
    timeout: "{{ download_timeout }}"

- name: Symlink {{ doc_root }}/common/assets/map-catalog.json -> /etc/iiab/map-catalog.json
  file:
    src: /etc/iiab/map-catalog.json
    path: "{{ doc_root }}/common/assets/map-catalog.json"    # /library/www/html
    state: link

- name: Symlink {{ vector_map_path }}/test-page/assets/map-catalog.json -> /etc/iiab/map-catalog.json
  file:
    src: /etc/iiab/map-catalog.json
    path: "{{ vector_map_path }}/test-page/assets/map-catalog.json"
    state: link


- name: Download OpenLayers test page stuff (JavaScript bundle etc) from {{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/test-page/build/* to {{ vector_map_path }}/test-page/ -- for test page http://box/osm-vector-maps/installer/
  get_url:
    url: "{{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/test-page/build/{{ item }}"
    dest: "{{ vector_map_path }}/test-page/"
    timeout: "{{ download_timeout }}"
  with_items:
    - test-page-bundle.js
    - index.html

- name: Download OpenLayers viewer page stuff (JavaScript bundle etc) from {{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/viewer/build/* to {{ vector_map_path }}/viewer/
  get_url:
    url: "{{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/viewer/build/{{ item }}"
    dest: "{{ vector_map_path }}/viewer/"
    timeout: "{{ download_timeout }}"
  with_items:
    - index.html
    - viewer-bundle.js
    - viewer-bundle.js.map

- name: Download 4 .css and .php helper files from {{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/viewer/* to {{ vector_map_path }}/viewer/ -- for viewer
  get_url:
    url: "{{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/viewer/{{ item }}"
    dest: "{{ vector_map_path }}/viewer/"
    timeout: "{{ download_timeout }}"
  with_items:
    - mbtileinfo.php
    - popup.css
    - searchapi.php
    - tileserver.php


- name: Download 48MB {{ maps_assets_url }}/{{ installer_planet }} to {{ vector_map_path }}/installer/ -- for map installer
  get_url:
    url: "{{ maps_assets_url }}/{{ installer_planet }}"    # e.g. planet_z0-z6_2020.mbtiles
    dest: "{{ vector_map_path }}/installer/"
    timeout: "{{ download_timeout }}"

- name: Symlink {{ vector_map_path }}/installer/detail.mbtiles -> {{ vector_map_path }}/installer/{{ installer_planet }}
  file:
    src: "{{ vector_map_path }}/installer/{{ installer_planet }}"
    path: "{{ vector_map_path }}/installer/detail.mbtiles"
    state: link

- name: Symlink {{ vector_map_path }}/viewer/tiles/{{ installer_planet }} -> {{ vector_map_path }}/installer/{{ installer_planet }}
  file:
    src: "{{ vector_map_path }}/installer/{{ installer_planet }}"
    path: "{{ vector_map_path }}/viewer/tiles/{{ installer_planet }}"
    state: link

- name: Download 25MB {{ maps_assets_url }}/{{ installer_satellite }} to {{ vector_map_path }}/viewer/tiles/ -- basic satellite photos
  get_url:
    url: "{{ maps_assets_url }}/{{ installer_satellite }}"    # e.g. satellite_z0-z6_2020.mbtiles
    dest: "{{ vector_map_path }}/viewer/tiles/"
    timeout: "{{ download_timeout }}"


- name: Download {index.html, installer-bundle.js} from {{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/installer/build/* to {{ vector_map_path }}/installer/ -- for map installer
  get_url:
    url: "{{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/installer/build/{{ item }}"
    dest: "{{ vector_map_path }}/installer/"
    timeout: "{{ download_timeout }}"
  with_items:
    - index.html
    - installer-bundle.js

- name: Download 4 action routines from {{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/installer/* to {{ vector_map_path }}/installer/ -- for installer
  get_url:
    url: "{{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/installer/{{ item }}"
    dest: "{{ vector_map_path }}/installer/"
    timeout: "{{ download_timeout }}"
  with_items:
    - map.css
    - style-osm.json
    - installer-functions.js
    - tileserver.php

- name: Download 15 common assets from {{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/viewer/assets/* to {{ vector_map_path }}/viewer/assets/ -- for the general purpose map viewer
  get_url:
    url: "{{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/viewer/assets/{{ item }}"
    dest: "{{ vector_map_path }}/viewer/assets/"
    timeout: "{{ download_timeout }}"
  with_items:
    - bboxes.geojson
    - center.png
    - countries.json
    # - fonts.css
    - ol-layerswitcher.css
    - ol-contextmenu.css
    - pin_drop.png
    - sprite@2x.json
    - sprite@2x.png
    - sprite.json
    - sprite.png
    - style-cdn.json
    - style-osm.json
    - style-sat.json
    - view_list.png

- name: Force Symlink {{ vector_map_path }}/maplist/assets/bboxes.geojson -> {{ vector_map_path }}/viewer/assets/bboxes.geojson for Admin Console
  file:
    src: "{{ vector_map_path }}/viewer/assets/bboxes.geojson"
    path: "{{ vector_map_path }}/maplist/assets/bboxes.geojson"
    state: link
    force: yes

- name: Force Symlink {{ vector_map_path }}/maplist/assets/countries.json -> {{ vector_map_path }}/viewer/assets/countries.json for Admin Console
  file:
    src: "{{ vector_map_path }}/viewer/assets/countries.json"
    path: "{{ vector_map_path }}/maplist/assets/countries.json"
    state: link
    force: yes


- name: Copy noto-sans fonts (15 files) to {{ doc_root }}/common/fonts/ for the general purpose map viewer (root:root, 0644 by default)
  copy:
    src: "{{ item }}"
    dest: "{{ doc_root }}/common/fonts/"
  with_fileglob:
    - fonts/noto-sans*

- name: Copy fonts.css to {{ vector_map_path }}/viewer/assets/
  copy:
    src: fonts/fonts.css
    dest: "{{ vector_map_path }}/viewer/assets/"


- name: Force Download redirect {{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/viewer/installer-index.redirect to test page {{ vector_map_path }}/maplist/index.html
  get_url:
    url: "{{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/viewer/installer-index.redirect"
    dest: "{{ vector_map_path }}/maplist/index.html"
    force: yes
    timeout: "{{ download_timeout }}"

- name: Copy 6 scripts to /usr/bin, for downloading tiles (0755)
  get_url:
    url: "{{ osm_repo_url }}/{{ maps_branch }}/osm-source/pages/viewer/scripts/{{ item }}"
    dest: /usr/bin/
    mode: 0755
    timeout: "{{ download_timeout }}"
  with_items:
    - iiab-install-map-region
    - iiab-extend-sat.py
    - iiab-summarize.sh
    - iiab-maps-finish.py
    - iiab-make-init.py
    - iiab-update-map


# RECORD OSM Vector Maps AS INSTALLED

- name: Record (final) disk space used
  shell: df -B1 --output=used / | tail -1
  register: df2

- name: Add 'osm_vector_maps_disk_usage = {{ df2.stdout|int - df1.stdout|int }}' to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"    # /etc/iiab/iiab.ini
    section: osm-vector-maps
    option: osm_vector_maps_disk_usage
    value: "{{ df2.stdout|int - df1.stdout|int }}"

- name: "Set 'osm_vector_maps_installed: True'"
  set_fact:
    osm_vector_maps_installed: True

- name: "Add 'osm_vector_maps_installed: True' to {{ iiab_state_file }}"
  lineinfile:
    path: "{{ iiab_state_file }}"    # /etc/iiab/iiab_state.yml
    regexp: '^osm_vector_maps_installed'
    line: 'osm_vector_maps_installed: True'