version: 2
jobs:
  build:
    machine: true
    # This would be for if we didn't have machine: true. Could help with circleci local execute, which doesn't support VMs?
    # docker:
    #   - image: cimg/go:1.13
    environment:
      GO_BRANCH: release-branch.go1.16
    steps:
      - run: echo $CIRCLE_WORKING_DIRECTORY
      - run: echo $PWD
      - run: echo $GOPATH
      - run: echo 'export GOPATH=$HOME/go' >> $BASH_ENV
      - run: echo 'export PATH="$GOPATH/bin:$PATH"' >> $BASH_ENV
      - run: echo $GOPATH
      - run: which go || sudo apt install golang-go
      - run: go version
      - run: |
          cd /usr/local
          sudo mkdir go.local
          sudo chown `whoami` go.local
      - restore_cache:
          key: go-local-
      - run: |
          cd /usr/local
          git clone git://github.com/golang/go go.local || true
          cd go.local
          git fetch
          git checkout "$GO_BRANCH"
          [[ -x bin/go && `git rev-parse HEAD` == `cat anacrolix.built` ]] && exit
          cd src
          ./make.bash || exit
          git rev-parse HEAD > ../anacrolix.built
      - save_cache:
          paths: /usr/local/go.local
          key: go-local-{{ checksum "/usr/local/go.local/anacrolix.built" }}
      - run: echo 'export PATH="/usr/local/go.local/bin:$PATH"' >> $BASH_ENV
      - run: go version
      - checkout
      - run: sudo apt-get update
      - run: sudo apt install fuse pv
      - restore_cache:
          keys:
            - go-pkg-
      - restore_cache:
          keys:
            - go-cache-
      - run: go get -d ./...
      - run: go test -v -race ./... -count 2
      - run: go test -bench . ./...
      - run: set +e; CGO_ENABLED=0 go test -v ./...; true
      - run: GOARCH=386 go test ./... -count 2 -bench . || true
      - run: go install github.com/anacrolix/godo@latest
      - save_cache:
          key: go-pkg-{{ checksum "go.mod" }}
          paths:
            - ~/go/pkg
      - run: sudo modprobe fuse
      - run: fs/test.sh
      - save_cache:
          key: go-cache-{{ .Revision }}
          paths:
            - ~/.cache/go-build
