---
- name: Does GHC exist?
  stat:
    path: "/opt/ghc/{{version.ghc}}/bin/ghci-{{version.ghc}}"
  register: ghc

- name: Does Cabal exist?
  stat:
    path: "/opt/cabal/{{version.cabal}}/bin/cabal"
  when: version.cabal is defined
  register: cabal

- name: GHC Dir
  file:
    state: directory
    path: "/opt/ghc/{{version.ghc}}"

- name: Cabal Dir
  file:
    state: directory
    path: "/opt/cabal/{{version.cabal}}/bin"
  when: version.cabal is defined

- name: Build GHC & cabal if it does not exist
  block:
    - name: Build GHC under the builder user
      block:
        - name: "Download GHC"
          unarchive:
            src: 'https://downloads.haskell.org/~ghc/{{version.ghc}}/ghc-{{version.ghc}}-x86_64-deb9-linux.tar.xz'
            dest: "/home/builder/ghc/"
            remote_src: True

        - name: Build GHC
          shell:
            chdir: '/home/builder/ghc/ghc-{{version.ghc}}'
            cmd: "./configure --prefix=/opt/ghc/{{version.ghc}}/"
      become_user: builder
      become: true

    - name: Install GHC
      shell:
        chdir: '/home/builder/ghc/ghc-{{version.ghc}}'
        cmd: make install -j8

  when: not ghc.stat.exists

- name: Add cabal
  unarchive:
    src: "https://downloads.haskell.org/~cabal/cabal-install-{{version.cabal}}/cabal-install-{{version.cabal}}-x86_64-linux-deb10.tar.xz"
    dest: "/opt/cabal/{{version.cabal}}/bin/"
    remote_src: true
  when: version.cabal is defined and not cabal.stat.exists

- name: Setup opt-GHC alteranative
  deb_alternatives:
    name: "opt-ghc{{item}}"
    link: "/opt/ghc/bin/ghc{{item}}"
    path: "/opt/ghc/{{version.ghc}}/bin/ghc"
    priority: "{{version_idx + 100}}"
    state: present
    slaves:
      - link: "/opt/ghc/bin/ghci{{item}}"
        name: "opt-ghci{{item}}"
        path: "/opt/ghc/{{version.ghc}}/bin/ghci"
      - link: "/opt/ghc/bin/runghc{{item}}"
        name: "opt-runghc{{item}}"
        path: "/opt/ghc/{{version.ghc}}/bin/runghc"
      - link: "/opt/ghc/bin/ghc-pkg{{item}}"
        name: "opt-ghc-pkg{{item}}"
        path: "/opt/ghc/{{version.ghc}}/bin/ghc-pkg"
      - link: "/opt/ghc/bin/haddock{{item}}"
        name: "opt-haddock{{item}}"
        path: "/opt/ghc/{{version.ghc}}/bin/haddock"
  loop:
    - ""
    - "-{{version.ghc}}"

- name: Setup GHC alternative
  deb_alternatives:
    name: "ghc{{item}}"
    link: "/usr/bin/ghc{{item}}"
    path: "/etc/alternatives/opt-ghc{{item}}"
    priority: 100
    state: present
    slaves:
      - link: "/usr/bin/ghci{{item}}"
        name: "ghci{{item}}"
        path: "/etc/alternatives/opt-ghci{{item}}"
      - link: "/usr/bin/runghc{{item}}"
        name: "runghc{{item}}"
        path: "/etc/alternatives/opt-runghc{{item}}"
      - link: "/usr/bin/ghc-pkg{{item}}"
        name: "ghc-pkg{{item}}"
        path: "/etc/alternatives/opt-ghc-pkg{{item}}"
      - link: "/usr/bin/haddock{{item}}"
        name: "haddock{{item}}"
        path: "/etc/alternatives/opt-haddock{{item}}"
  loop:
    - ""
    - "-{{version.ghc}}"

- name: Setup opt-cabal alternative
  deb_alternatives:
    name: "opt-cabal{{item}}"
    link: "/opt/ghc/bin/cabal{{item}}"
    path: "/opt/cabal/{{version.cabal}}/bin/cabal"
    priority: "{{version_idx + 100}}"
    state: present
  loop:
    - ""
    - "-{{version.cabal}}"
  when: version.cabal is defined

- name: Setup cabal alternative
  deb_alternatives:
    name: "cabal{{item}}"
    link: "/usr/bin/cabal{{item}}"
    path: "/etc/alternatives/opt-cabal{{item}}"
    priority: 100
    state: present
  loop:
    - ""
    - "-{{version.cabal}}"
  when: version.cabal is defined
