- name: Copy GHC apt key
  copy:
    src: apt-ghc.key
    dest: /root/apt-ghc.key

- name: Add apt-key for GHC
  apt_key:
    file: /root/apt-ghc.key
    # gpg --keyserver keyserver.ubuntu.com --export BA3CBA3FFE22B574
    # gpg --armor --export BA3CBA3FFE22B574

- name: add GHC repository
  apt_repository:
    repo: deb http://downloads.haskell.org/debian buster main
    filename: ghc
    state: present

- name: "auto install ghc"
  block:
    - name: install ghc 8.10 .. 9.2 & cabal 3.4
      apt:
        pkg:
          - cabal-install-3.4
          - ghc-8.10.2
          - ghc-9.0.2
          - ghc-9.2.1
        state: present

    - name: drop old versions
      apt:
        pkg:
          - ghc-8.10.1
          - ghc
          - cabal-install
        state: absent

    - name: setup links to up-to-date GHC
      alternatives:
        name: "global-haskell-{{item}}"
        link: "/usr/local/bin/{{item}}"
        path: "/opt/ghc/bin/{{item}}"
        priority: 50
      with_items:
        - cabal
        - ghc
        - ghci
        - ghc-pkg
        - haddock
        - runghc
  when: auto_install_ghc
