distributable:
  url: https://hackage.haskell.org/packages/archive/cabal-install/{{ version.raw }}/cabal-install-{{ version.raw }}.tar.gz
  strip-components: 1

versions:
  github: haskell/cabal/releases
  strip: /^[Cc]abal /

dependencies:
  haskell.org: 9
  gnu.org/gmp: 6
  zlib.net: 1

provides:
  - bin/cabal

build:
  dependencies:
    haskell.org: <9.6.4 # later versions pass invalid linker flags on darwin clang-14
    curl.se: '*'
    linux/aarch64:
      github.com/numactl/numactl: ^2 # downloaded cabal needs this
    tukaani.org/xz: 5
  script:
    # utf-8 issues
    - run: export HOME="$(mktemp -d)"
      if: linux

    # FIXME: this isn't great, but it'll fix any ghc install at build time
    # needs install-time customization for a true fix
    - run: |
        if ! grep -q 'rpath,{{pkgx.prefix}}' settings; then
          sed -i \
            -e 's|\(C compiler flags.*\)")|\1 -Wl,-rpath,{{pkgx.prefix}}")|' \
            -e 's|\(C++ compiler flags.*\)")|\1 -Wl,-rpath,{{pkgx.prefix}}")|' \
            -e 's|\(C compiler link flags.*\)")|\1 -Wl,-rpath,{{pkgx.prefix}}")|' \
            settings
        fi
      if: darwin
      working-directory: ${{deps.haskell.org.prefix}}/.ghcup/ghc/{{deps.haskell.org.version}}/lib/ghc-{{deps.haskell.org.version}}/lib

    - run: |
        if test ! -e .bootstrap/cabal; then
          if test ! -e .bootstrap; then
            mkdir .bootstrap
          fi
          curl -L "${BOOTSTRAP}" | tar Jxf - -C .bootstrap
        fi

    ## This seems dirty, but building is an internal-only gig atm.
    # Likely fix is to use CABAL_DIR in build scripts
    - run: |
        if test -e ~/.cabal/bin; then
          rm -r ~/.cabal/bin
        fi

    # this is not an acceptable solution, do you know a better one? pls PR 🙏
    #FIXME also this sed line sucks but I got bored trying to make it work
    # zlib 0.7.0.0 doesn't respect our rpaths with its intermediate build products
    - |
      sed -i \
        -e 's/build-depends: base.*/build-depends: base >=4.10 \&\& <5/' \
        cabal-install.cabal
    # ^^ SEE https://github.com/haskell/cabal/issues/8118
    - mkdir -p "{{prefix}}/bin"

    - ./.bootstrap/cabal v2-update

    - ./.bootstrap/cabal v2-install
      --install-method=copy
      --installdir={{prefix}}/bin
      $ADDITIONAL_CABAL_FLAGS
  env:
    darwin/aarch64:
      BOOTSTRAP: https://downloads.haskell.org/~cabal/cabal-install-3.10.2.0/cabal-install-3.10.2.0-aarch64-darwin.tar.xz
    darwin/x86-64:
      BOOTSTRAP: https://downloads.haskell.org/~cabal/cabal-install-3.10.2.0/cabal-install-3.10.2.0-x86_64-darwin.tar.xz
    linux/x86-64:
      BOOTSTRAP: https://downloads.haskell.org/~cabal/cabal-install-3.10.2.0/cabal-install-3.10.2.0-x86_64-linux-deb10.tar.xz
    linux/aarch64:
      BOOTSTRAP: https://downloads.haskell.org/~cabal/cabal-install-3.10.2.0/cabal-install-3.10.2.0-aarch64-linux-deb10.tar.xz
    linux:
      ADDITIONAL_CABAL_FLAGS:
        - -v3
        # else segfaults
        - --enable-relocatable
        - --ghc-option=-fPIC
        - --ghc-option=-Wl,-pie

test:
  - cabal --config-file=./config user-config init
  - cabal --config-file=./config info Cabal
