version: 2.0

jobs:
  attention:
    docker:
      - image: $CIRCLE_PROJECT_USERNAME/opencog-deps
        user: root
        environment:
          CCACHE_DIR: /ws/ccache
    working_directory: /ws/attention
    steps:
      - attach_workspace:
          at: /ws
      - run:
          name: Start restoring ccache
          command: date +%d-%m-%Y > /tmp/date
      - restore_cache:
          keys:
            - ccache-{{ checksum "/tmp/date" }}
            - ccache-
      - run:
          name: Install CogUtil
          command: |
            git clone --depth 1 https://github.com/$CIRCLE_PROJECT_USERNAME/cogutil /ws/cogutil
            mkdir -p /ws/cogutil/build
            cd /ws/cogutil/build && cmake .. && make -j2 && make -j2 install
            ldconfig
      - run:
          name: Checkout AtomSpace
          command: git clone --depth 1 https://github.com/$CIRCLE_PROJECT_USERNAME/atomspace /ws/atomspace
      - restore_cache:
          name: Restore GCH Cache
          keys:
            - ghc-{{checksum "/ws/atomspace/opencog/haskell/stack.yaml"}}-{{ arch }}
      - restore_cache:
          name: Restore Haskell Deps Cache
          keys:
            - haskelldeps-{{checksum "/ws/atomspace/opencog/haskell/stack.yaml"}}-{{checksum "/ws/atomspace/opencog/haskell/opencog-atomspace.cabal"}}-{{ arch }}
      - run:
          name: Install AtomSpace
          command: |
            mkdir -p /ws/atomspace/build
            cd /ws/atomspace/build && cmake .. && make -j2 && make -j2 install
            ldconfig
      - save_cache:
          name: Save GHC Cache
          key: ghc-{{checksum "/ws/atomspace/opencog/haskell/stack.yaml"}}-{{ arch }}
          paths:
            - /root/.stack
      - save_cache:
          name: Save Haskell Deps Cache
          key: haskelldeps-{{checksum "/ws/atomspace/opencog/haskell/stack.yaml"}}-{{checksum "/ws/atomspace/opencog/haskell/opencog-atomspace.cabal"}}-{{ arch }}
          paths:
            - /ws/atomspace/opencog/haskell/.stack-work
      - run:
          name: Install CogServer
          command: |
            git clone --depth 1 https://github.com/$CIRCLE_PROJECT_USERNAME/cogserver /ws/cogserver
            mkdir -p /ws/cogserver/build
            cd /ws/cogserver/build && cmake .. && make -j2 && make -j2 install
            ldconfig
      - checkout
      - run:
          name: CMake Configure
          command: mkdir build && cd build && cmake ..
      - run:
          name: Build
          command: cd build && make -j2
      - run:
          name: Build tests
          command: cd build && make -j2 tests
      - run:
          name: Run tests
          command: cd build && make -j2 test ARGS=-j2
      - run:
          name: Install Attention
          command: cd build && make -j2 install && ldconfig
      - run:
          name: Print test log
          command: cat build/tests/Testing/Temporary/LastTest.log
          when: always
      - persist_to_workspace:
          root: /ws/
          paths:
            - cogutil
            - atomspace
            - cogserver
            - attention
            - ccache

  opencog:
    docker:
      - image: $CIRCLE_PROJECT_USERNAME/opencog-deps
        user: root
        environment:
          CCACHE_DIR: /ws/ccache
    working_directory: /ws/opencog
    steps:
      - attach_workspace:
          at: /ws/
      - restore_cache:
          name: Restore GCH Cache
          keys:
            - ghc-{{checksum "/ws/atomspace/opencog/haskell/stack.yaml"}}-{{ arch }}
      - restore_cache:
          name: Restore Haskell Deps Cache
          keys:
            - haskelldeps-{{checksum "/ws/atomspace/opencog/haskell/stack.yaml"}}-{{checksum "/ws/atomspace/opencog/haskell/opencog-atomspace.cabal"}}-{{ arch }}
      - run:
          name: Install cogutil
          command: cd /ws/cogutil/build && make -j2 install && ldconfig
      - run:
          name: Install AtomSpace
          command: cd /ws/atomspace/build && make -j2 install && ldconfig
      - run:
          name: Install CogServer
          command: cd /ws/cogserver/build && make -j2 install && ldconfig
      - run:
          name: Install Attention
          command: cd /ws/attention/build && make -j2 install && ldconfig
      - run:
          name: Install URE
          command: |
            git clone --depth 1 https://github.com/$CIRCLE_PROJECT_USERNAME/ure /ws/ure
            mkdir -p /ws/ure/build
            cd /ws/ure/build && cmake .. && make -j2 && make -j2 install
            ldconfig
      - run:
          name: Install PLN
          command: |
            git clone --depth 1 https://github.com/$CIRCLE_PROJECT_USERNAME/pln /ws/pln
            mkdir -p /ws/pln/build
            cd /ws/pln/build && cmake .. && make -j2 && make -j2 install
            ldconfig
      - run:
          name: Install Link Grammar Atomese
          command: |
            git clone --depth 1 https://github.com/$CIRCLE_PROJECT_USERNAME/lg-atomese /ws/lg-atomese
            mkdir -p /ws/lg-atomese/build
            cd /ws/lg-atomese/build && cmake .. && make -j2 && make -j2 install
            ldconfig
      - run:
          name: Checkout OpenCog
          command: git clone --depth 1 https://github.com/$CIRCLE_PROJECT_USERNAME/opencog .
      - run:
          name: CMake Configure
          command: mkdir build && cd build && cmake ..
      - run:
          name: Build
          command: cd build && make -j2
      - run:
          name: Build tests
          command: cd build && make -j2 tests
      - run:
          name: Run tests
          command: cd build && make -j2 test ARGS=-j2
      - run:
          name: Install OpenCog
          command: cd build && make -j2 install && ldconfig
      - run:
          name: Print test log
          command: cat build/tests/Testing/Temporary/LastTest.log
          when: always
      - persist_to_workspace:
          root: /ws/
          paths:
            - opencog
            - ccache

  package: #Place holder
    docker:
      - image: $CIRCLE_PROJECT_USERNAME/opencog-deps
        user: root
    working_directory: /ws/attention
    steps:
      - attach_workspace:
          at: /ws/
      - run:
          name: Start storing ccache
          command: date +%d-%m-%Y > /tmp/date
      - save_cache:
          key: ccache-{{ checksum "/tmp/date" }}
          paths:
            - /ws/ccache


workflows:
  version: 2
  build-test-package:
    jobs:
      - attention
      - opencog:
          requires:
            - attention
      - package:
          requires:
            - opencog
          filters:
            branches:
              only: master
