---

- name: Install fdroid server
  become: yes
  register: install_fdroid
  apt:
    pkg:
      - python3-pip
      - fdroidserver

- name: Install gplaycli
  become: yes
  register: install_gplaycli
  pip:
    name: gplaycli

- name: check for fdroid user
  user:
    name: "{{ fdroid_user }}"
    home: "{{ fdroid_base_dir }}"
    state: present

- name: Create directories
  file:
    path: "{{ item }}"
    state: directory
    owner: "{{ fdroid_user }}"
    mode: 0775
  with_items:
    - "{{ fdroid_base_dir }}"
    - "{{ fdroid_android_dir }}"
    - "{{ fdroid_repo_dir }}"
    - "{{ fdroid_utils_dir }}"
    - "{{ fdroid_repo_dir }}/repo"
    - "{{ fdroid_html_dir }}"

- name: Unarchive android sdk
  become: yes
  become_user: "{{ fdroid_user }}"
  register: download_sdk
  ansible.builtin.unarchive:
    src: https://dl.google.com/android/repository/commandlinetools-linux-6858069_latest.zip
    dest: "{{ fdroid_android_dir }}"
    remote_src: yes

- name: Initialize android sdk
  become: yes
  become_user: "{{ fdroid_user }}"
  register: download_build_tools
  shell: 'yes | {{ fdroid_android_dir }}/cmdline-tools/bin/sdkmanager --sdk_root="$ANDROID_HOME" platform-tools "build-tools;30.0.3"'
  environment:
    ANDROID_HOME: "{{ fdroid_android_dir }}/android-sdk-linux"
  tags: ['skip_ansible_lint']

- name: Upload gplaycli conf
  template:
    src: gplaycli.conf.j2
    dest: "{{ fdroid_base_dir }}/gplaycli.conf"
    owner: "{{ fdroid_user }}"
    mode: 0600

- name: Upload settings
  template:
    src: config.py.j2
    dest: "{{ fdroid_repo_dir }}/config.py"
    owner: "{{ fdroid_user }}"
    mode: 0600

- name: Upload cisti img
  copy:
    src: files/cisti.png
    dest: "{{ fdroid_repo_dir }}/cisti.png"
    owner: "{{ fdroid_user }}"
    mode: 0660

- name: Local decrypt keystore
  delegate_to: localhost
  shell: "pass show cisti.org/ansible/fdroid/keystore > /tmp/cisti.keystore"
  tags: ['skip_ansible_lint']

- name: Upload keystore
  copy:
    src: /tmp/cisti.keystore
    dest: "{{ fdroid_repo_dir }}/cisti.keystore"
    owner: "{{ fdroid_user }}"
    mode: 0600

- name: Remove file (delete file)
  delegate_to: localhost
  file:
    path: /tmp/cisti.keystore
    state: absent

- name: Upload apk list
  copy:
    src: files/applist
    dest: "{{ fdroid_base_dir }}/apklist"
    owner: "{{ fdroid_user }}"
    mode: 0660

- name: Upload apk-dl
  copy:
    src: files/apk-dl
    dest: "{{ fdroid_utils_dir }}/apk-dl"
    owner: "{{ fdroid_user }}"
    mode: 0700

- name: Upload service for apk poller
  template:
    src: fdroid.service.j2
    dest: /etc/systemd/system/fdroid.service
    owner: "{{ fdroid_user }}"
    mode: 0660

- name: Upload timer for apk poller
  template:
    src: fdroid.timer.j2
    dest: /etc/systemd/system/fdroid.timer
    owner: "{{ fdroid_user }}"
    mode: 0660

- name: Make sure apk poller is running
  systemd:
    state: restarted
    daemon_reload: yes
    name: fdroid

- name: Link repo into public dir
  file:
    src: "{{ fdroid_repo_dir }}/repo"
    path: "{{ fdroid_html_dir }}/repo"
    state: link
    owner: "{{ fdroid_user }}"
