---

- name: Install system dependencies
  package: name={{ clojure_packages }}
  tags: clojure

- when: clojure_install_mode != 'none'
  block:

    - name: Download clojure
      get_url:
        url: "{{ clojure_archive_url }}"
        dest: /tmp/
        checksum: sha256:{{ clojure_archive_sha256 }}

    - name: Extract clojure archive
      unarchive:
        src: /tmp/clojure-tools-{{ clojure_version }}.tar.gz
        dest: /tmp/
        remote_src: True

    - name: Create lib dir
      file: path=/usr/local/lib/clojure/libexec state=directory

    - name: Install clojure
      copy: src=/tmp/clojure-tools/{{ item.src }} dest={{ item.dest }} mode={{ (item.dest is search('/bin/?')) | ternary(755, omit) }} remote_src=True
      loop:
        - src: deps.edn
          dest: /usr/local/lib/clojure/
        - src: example-deps.edn
          dest: /usr/local/lib/clojure/
        - src: tools.edn
          dest: /usr/local/lib/clojure/
        - src: exec.jar
          dest: /usr/local/lib/clojure/libexec/
        - src: clojure-tools-{{ clojure_version }}.jar
          dest: /usr/local/lib/clojure/libexec/
        - src: clojure
          dest: /usr/local/bin/
        - src: clj
          dest: /usr/local/bin/
        - src: clojure.1
          dest: /usr/local/share/man/man1/
        - src: clj.1
          dest: /usr/local/share/man/man1/

    - name: Set libdir
      replace:
        path: /usr/local/bin/clojure
        regexp: PREFIX
        replace: /usr/local/lib/clojure

    - name: Set bindir
      replace:
        path: /usr/local/bin/clj
        regexp: BINDIR
        replace: /usr/local/bin

  tags: clojure

