---
- name: Create config directory
  file:
    path: '~/.config/mpv/{{ item }}'
    state: directory
    mode: 0755
  loop: [ shaders, scripts, script-opts ]

- name: Copy config
  template:
    src: mpv.j2
    dest: ~/.config/mpv/mpv.conf
    force: yes
    mode: 0644

# Busybox has 'unzip' so use that
# 'zipinfo' is required for 'unarchive' module, but it is only available in 'unzip' package
- block:
    - name: Download Anime4K zip file
      get_url:
        url: 'https://github.com/bloc97/Anime4K/releases/download/v{{ anime4k_shaders_version }}/Anime4K_v4.0.zip'
        dest: /tmp/Anime4K.zip
        mode: 0644

    - name: Unzip Anime4K shaders
      command: unzip /tmp/Anime4K.zip -d ~/.config/mpv/shaders
      args:
        creates: ~/.config/mpv/shaders/Anime4K_Clamp_Highlights.glsl
  tags: [ mpv-extend, anime4k_shaders ]

- block:
    - name: Download mpv_thumbnail_script
      get_url:
        url: 'https://github.com/marzzzello/mpv_thumbnail_script/releases/download/{{ thumbnail_script_version }}/{{ item }}'
        dest: ~/.config/mpv/scripts/
        mode: 0644
      loop:
        - mpv_thumbnail_script_client_osc.lua
        - mpv_thumbnail_script_server.lua

    - name: Copy mpv_thumbnail_script configuration
      copy:
        src: mpv_thumbnail_script.conf
        dest: ~/.config/mpv/script-opts/mpv_thumbnail_script.conf
        force: yes
        mode: 0644

    - name: Ensure OSC is disabled in mpv config file
      lineinfile:
        path: ~/.config/mpv/mpv.conf
        state: present
        regexp: '^osc='
        line: osc=no
  tags: [ mpv-extend, thumbnail ]

- block:
    # Need python3-dev
    - name: Install ffsubsync with pip
      pip:
        name: ffsubsync
        virtualenv: ~/.config/mpv/venv

    - name: Symlink ffsubsync executable
      file:
        src: ~/.config/mpv/venv/bin/ffsubsync
        dest: ~/.local/bin/ffsubsync
        state: link

    - name: Clone autosubsync-mpv repositories
      git:
        depth: 1
        repo: https://github.com/joaquintorres/autosubsync-mpv.git
        dest: ~/.config/mpv/scripts/autosubsync

    - name: Copy autosubsync configuration
      copy:
        src: autosubsync.conf
        dest: ~/.config/mpv/script-opts/autosubsync.conf
        force: yes
        mode: 0644
  tags: [ mpv-extend, subsync ]

# Doesn't work well, so disable for now
- block:
    - name: Install subliminal with pip
      pip:
        name: subliminal
        virtualenv: ~/.config/mpv/venv

    - name: Symlink subliminal executable
      file:
        src: ~/.config/mpv/venv/bin/subliminal
        dest: ~/.local/bin/subliminal
        state: link

    # https://github.com/davidde/mpv-autosub
    - name: Copy modified autosub.lua
      copy:
        src: autosub.lua
        dest: ~/.config/mpv/scripts/autosub.lua
        force: yes
        mode: 0644
  tags: [ never, mpv-extend, autosub ]

- name: Install mpv-webm script
  get_url:
    url: https://github.com/ekisu/mpv-webm/releases/download/latest/webm.lua
    dest: ~/.config/mpv/scripts/webm.lua
    mode: 0644
  tags: [ mpv-extend, webm ]

- name: Copy input config
  copy:
    src: input.conf
    dest: ~/.config/mpv/input.conf
    force: yes
    mode: 0644
  tags: mpv-extend
