---
- name: Dependencies installed
  community.general.pkgng:
    name: py39-pip py39-sqlite3 py39-gstreamer1 gstreamer1-plugins-flac gstreamer1-plugins-wavpack gstreamer1-plugins-mpg123 gstreamer1-libav gstreamer1-plugins-theora gstreamer1-plugins-faad gstreamer1-plugins-bad gstreamer1-plugins-lame gstreamer1-plugins-twolame gstreamer1-plugins-soup gstreamer1-plugins-shout2
    state: latest

- name: Mopidy plugins installed
  pip:
    name: mopidy,mopidy-bandcamp,mopidy-soundcloud,mopidy-mpd,mopidy-Iris,mopidy-tunein,mopidy-local,mopidy-scrobbler,mopidy-mowecl,mopidy-musicbox-webclient,mopidy-party,mopidy-muse,mopidy-Lagukan,mopidy-somafm,mopidy-internetarchive
    state: latest

- name: Install webhooks
  shell: pip install https://code.unbl.ink/secstate/mopidy-webhooks/archive/0.2.2.zip

- name: Local folder exists
  file:
    path: /var/lib/mopidy/local
    state: directory
    mode: 0755
    recurse: yes

- name: Symlink image directory
  shell: rm -rf /var/lib/mopidy/local/images && ln -s /var/lib/mopidy/media/music/.mopidy-images /var/lib/mopidy/local/images

- name: Today playlist generator installed
  copy:
    src: generate-random-playlist.sh
    dest: /usr/local/bin/generate-random-playlist
    owner: root
    mode: 0700

- name: Scan mopidy library once a day
  ansible.builtin.cron:
    name: "scan mopidy"
    special_time: daily
    job: "mopidy --config /usr/local/etc/mopidy.conf local scan"

- name: Update random playlists once a day
  ansible.builtin.cron:
    name: "generate random playlists"
    special_time: daily
    job: "generate-random-playlist"

- name: Backup mopidy DB to postgres
  ansible.builtin.cron:
    name: "backup db"
    special_time: daily
    job: "pgloader --with 'prefetch rows=100000' --logfile /var/log/pgloader.log /usr/local/etc/pgl
oader-mopidy.conf"


- name: Mopidy settings installed
  template:
    src: mopidy.conf.j2
    dest: /usr/local/etc/mopidy.conf
    owner: root
    mode: 0664
  notify: Immortal restarted

- name: Mopidy immortal file installed
  copy:
    src: immortal.yml
    dest: /usr/local/etc/immortal/mopidy.yml
    owner: root
    mode: 0600
  notify: Immortal restarted
