#!/usr/bin/ansible-playbook
# vim:ft=ansible:
---
- name: Set up Minecraft user
  block:
    - name: Install required packages
      apt:
        name: "{{ packages }}"
      vars:
        packages:
          - openjdk-8-jre
          - screen
          - unzip
    - name: Assure minecraft admin group
      group:
        name: mc-admin
    - name: Assure server user
      user:
        name: minecraft
        group: mc-admin
        system: yes
        home: /var/minecraft
    - name: Assure var directory
      file:
        path: /var/minecraft
        state: directory
        owner: root
        group: mc-admin
        # Sticky, SetGID
        mode: "3770"
    - name: Assure systemd unit
      copy:
        src: "minecraft@.service"
        dest: "/etc/systemd/system/minecraft@.service"
    - name: Reload available daemons
      systemd:
        daemon_reload: yes
  become: yes
- name: Set up Minecraft server
  block:
    - name: Create server directory
      file:
        path: "/var/minecraft/{{ mcname }}"
        state: directory
    - name: Check for existing install
      stat:
        path: "/var/minecraft/{{ mcname }}/forge-{{ mcforge }}-universal.jar"
      register: stat_mcroot_forgejar
    - name: Unpack archive
      block:
        - name: Download zip
          get_url:
            dest: "/var/minecraft/{{ mcname }}/modpack.zip"
            url: "{{ mczip }}"
        - name: Unpack zip
          unarchive:
            src: "/var/minecraft/{{ mcname }}/modpack.zip"
            remote_src: yes
            dest: "/var/minecraft/{{ mcname }}"
        - name: Remove zip
          file:
            path: "/var/minecraft/{{ mcname }}/modpack.zip"
            state: absent
        - name: Download Forge
          get_url:
            dest: "/var/minecraft/{{ mcname }}/installer.jar"
            # Accurate as of 2020-02-23
            url: "https://files.minecraftforge.net/maven/net/minecraftforge/forge/{{ mcforge }}/forge-{{ mcforge }}-installer.jar"
        - name: Install Forge
          # Note: This also installs a Minecraft server, thankfully
          command:
            chdir: "/var/minecraft/{{ mcname }}"
            argv:
              - /usr/bin/java
              - "-jar"
              - "/var/minecraft/{{ mcname }}/installer.jar"
              - "--installServer"
          args:
            creates: "/var/minecraft/{{ mcname }}/forge-{{ mcforge }}-universal.jar"
        - name: Link server jar
          file:
            path: "/var/minecraft/{{ mcname }}/server.jar"
            src: "/var/minecraft/{{ mcname }}/forge-{{ mcforge }}-universal.jar"
            state: link
      when: not stat_mcroot_forgejar.stat.exists
    - name: Configure server
      block:
        - name: Template out server.properties
          template:
            src: server.properties
            dest: "/var/minecraft/{{ mcname }}/server.properties"
        - name: Accept EULA
          copy:
            src: "eula.txt"
            dest: "/var/minecraft/{{ mcname }}/eula.txt"
  become: yes
  become_user: minecraft
- name: Enable service
  systemd:
    name: "minecraft@{{ mcname }}"
    enabled: yes
    state: started
  become: yes