version: "2.1"
jobs:
  build:
    macos:
      xcode: 9.4.1
    environment:
      HOMEBREW_NO_AUTO_UPDATE: 1
    steps:
    - checkout
    - restore_cache:
        # See https://circleci.com/docs/2.0/caching/
        name: Restore Cached Dependencies
        keys:
          - stack-dependencies-{{ checksum "stack.yaml" }}
    - run:
        name: Install stack
        command: stack --version || (curl -sSL https://get.haskellstack.org/ | sh -s - -f)
    - run:
        name: Resolve/Update Dependencies
        command: stack --no-terminal setup
    - run:
        name: Run tests
        command: stack --no-terminal test
    - run:
        name: Install executable
        command: stack --no-terminal install
    - run:
        name: Create artifacts
        command: |
          export VERSION=$(grep '^[Vv]ersion:' pandoc.cabal | awk '{print $$2;}')
          export ARTIFACTS=macos-release-candidate
          export RESOURCES=${ARTIFACTS}/Resources
          export ROOT=${ARTIFACTS}/pandoc
          export DEST=${ROOT}/usr/local
          export ME=$(whoami)
          export BASE=pandoc-$VERSION
          mkdir -p ${ARTIFACTS}
          mkdir -p ${RESOURCES}
          mkdir -p ${DEST}/bin
          mkdir -p ${DEST}/share/man/man1
          cp ~/.local/bin/pandoc ${DEST}/bin/
          strip ${DEST}/bin/pandoc
          cp man/pandoc.1 ${DEST}/share/man/man1/pandoc.1
          ~/.local/bin/pandoc -s COPYING.md -Vpagetitle=License -o ${RESOURCES}/license.html
          chown -R $ME:staff ${ROOT}
          sed -e "s/PANDOCVERSION/${VERSION}/" macos/distribution.xml.in > ${ARTIFACTS}/distribution.xml
          cp macos/Makefile ${ARTIFACTS}/
          echo ${VERSION} > ${ARTIFACTS}/version.txt
          zip -r macos-release-candidate.zip ${ARTIFACTS}

    - save_cache:
        name: Cache Dependencies
        key: stack-dependencies-{{ checksum "stack.yaml" }}
        paths:
        - "/root/.stack"
        - ".stack-work"
        - "/usr/local/bin/stack"

    - store_artifacts:
        # https://circleci.com/docs/2.0/artifacts/
        path: macos-release-candidate.zip
        destination: macos-release-candidate.zip

workflows:
  version: 2
  release:
    jobs:
    - build:
        filters:
          branches:
            only:
            - /rc\/.*/
