---
 - name: Base packages
   become: yes
   package:
     name:
       - gitea

 - name: BitBot
   become: yes
   git:
     repo: https://github.com/jesopo/bitbot.git
     dest: /opt/bitbot

 - name: Make directories
   become: yes
   file:
     path: "/var/lib/gitea/{{ item }}"
     owner: gitea
     group: gitea
     mode: 0750
   loop:
     - "custom/bin"
     - "web-snippets"

 - name: Populate config
   become: yes
   register: config
   template:
     src: app.ini.j2
     dest: /var/lib/gitea/custom/conf/app.ini
     owner: gitea
     group: gitea
     mode: 0750

 - name: Copy web-snippets
   become: yes
   copy:
     src: web-snippets/
     dest: /var/lib/gitea/web-snippets
     owner: gitea
     group: gitea
     mode: 0640

 - name: Copy scripts
   become: yes
   copy:
     src: custom/
     dest: /var/lib/gitea/custom/
     owner: gitea
     group: gitea

 - name: Publish AniNIX/Yggdrasil CSS
   become: yes
   get_url:
     url: https://github.com/BenZuser/Emby-Web-Dark-Themes-CSS/raw/master/RED/theme.css
     dest: /var/lib/gitea/custom/public/css/emby-web-dark-theme-BenZuser.css
     owner: gitea
     group: gitea

 - name: Copy hook
   become: yes
   copy:
     src: gitea.hook
     dest: /etc/pacman.d/hooks/gitea.hook
     owner: gitea
     group: gitea

 - name: Service file
   become: yes
   register: servicefile
   copy:
     src: foundation.service
     dest: /usr/lib/systemd/system
     owner: root
     group: root
     mode: 0755

 - name: Ensure default service disabled
   become: yes
   service:
     name: gitea
     state: stopped
     enabled: no

 - name: Generate pages
   become: yes
   register: custompages
   command: /usr/bin/runuser -u gitea -- /usr/bin/bash /var/lib/gitea/custom/bin/gen-aninix-custom

 - name: Restart service
   become: yes
   when: config.changed or servicefile.changed or custompages.changed
   service:
     name: foundation
     state: restarted
     enabled: yes
