version: 2.0

jobs:
  atomspace:
    docker:
      - image: $CIRCLE_PROJECT_USERNAME/opencog-deps
        user: root
        environment:
          PGHOST: opencog-postgres
          PGUSER: opencog_test
          PGPASSWORD: cheese
          CCACHE_DIR: /ws/ccache
      - image: $CIRCLE_PROJECT_USERNAME/postgres
        name: opencog-postgres
    working_directory: /ws/atomspace
    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
      - checkout
      - restore_cache:
          name: Restore GHC 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: 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
      - run:
          name: Install AtomSpace
          command: cd build && make -j2 install && ldconfig
      - run:
          name: Build examples
          command: cd build && make -j2 examples
      - run:
          name: Print test log
          command: cat build/tests/Testing/Temporary/LastTest.log
          when: always
      - 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
      - persist_to_workspace:
          root: /ws/
          paths:
            - cogutil
            - atomspace
            - ccache

  atomspace-rocks:
    docker:
      - image: $CIRCLE_PROJECT_USERNAME/opencog-deps
        user: root
        environment:
          CCACHE_DIR: /ws/ccache
    working_directory: /ws/atomspace-rocks
    steps:
      - attach_workspace:
          at: /ws/
      - 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: Checkout AtomSpaceRocks
          command: git clone --depth 1 https://github.com/$CIRCLE_PROJECT_USERNAME/atomspace-rocks .
      - 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 test
      - run:
          name: Install AtomSpaceRocks
          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:
            - atomspace-rocks
            # See https://github.com/opencog/cogutil/commit/da53c4079c3972904857a365a7761ac5923e4e92
            # - ccache

  ure:
    docker:
      - image: $CIRCLE_PROJECT_USERNAME/opencog-deps
        user: root
        environment:
          CCACHE_DIR: /ws/ccache
    working_directory: /ws/ure
    steps:
      - attach_workspace:
          at: /ws/
      - 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: Checkout URE
          command: git clone --depth 1 https://github.com/$CIRCLE_PROJECT_USERNAME/ure .
      - 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 URE
          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:
            - ure
            # See https://github.com/opencog/cogutil/commit/da53c4079c3972904857a365a7761ac5923e4e92
            # - ccache

  spacetime:
    docker:
      - image: $CIRCLE_PROJECT_USERNAME/opencog-deps
        user: root
        environment:
          CCACHE_DIR: /ws/ccache
    working_directory: /ws/spacetime
    steps:
      - attach_workspace:
          at: /ws/
      - 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: Checkout SpaceTime
          command: git clone --depth 1 https://github.com/$CIRCLE_PROJECT_USERNAME/spacetime .
      - 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 spacetime
          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:
            - spacetime
            # See https://github.com/opencog/cogutil/commit/da53c4079c3972904857a365a7761ac5923e4e92
            # - ccache

  pln:
    docker:
      - image: $CIRCLE_PROJECT_USERNAME/opencog-deps
        user: root
        environment:
          CCACHE_DIR: /ws/ccache
    working_directory: /ws/pln
    steps:
      - attach_workspace:
          at: /ws/
      - 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 URE
          command: cd /ws/ure/build && make -j2 install && ldconfig
      - run:
          name: Install SpaceTime
          command: cd /ws/spacetime/build && make -j2 install && ldconfig
      - run:
          name: Checkout PLN
          command: git clone --depth 1 https://github.com/$CIRCLE_PROJECT_USERNAME/pln .
      - run:
          name: CMake Configure
          command: mkdir build && cd build && cmake ..
      - run:
          name: Build
          command: cd build && make -j2
      - run:
          name: Install PLN
          command: cd build && make -j2 install && ldconfig
      - run:
          name: Build tests
          command: cd build && make -j2 tests
      - run:
          name: Run tests
          command: cd build && make -j2 test ARGS=-j2
      - run:
          name: Print test log
          command: cat build/tests/Testing/Temporary/LastTest.log
          when: always
      - persist_to_workspace:
          root: /ws/
          paths:
            - pln
            # See https://github.com/opencog/cogutil/commit/da53c4079c3972904857a365a7761ac5923e4e92
            # - ccache

  miner:
    docker:
      - image: $CIRCLE_PROJECT_USERNAME/opencog-deps
        user: root
        environment:
          CCACHE_DIR: /ws/ccache
    working_directory: /ws/miner
    steps:
      - attach_workspace:
          at: /ws/
      - restore_cache:
          name: Restore GHC 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 URE
          command: cd /ws/ure/build && make -j2 install && ldconfig
      - run:
          name: Checkout Miner
          command: git clone --depth 1 https://github.com/$CIRCLE_PROJECT_USERNAME/miner .
      - 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 Miner
          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:
            - miner
            - ccache

  cogserver:
    docker:
      - image: $CIRCLE_PROJECT_USERNAME/opencog-deps
        user: root
        environment:
          CCACHE_DIR: /ws/ccache
    working_directory: /ws/cogserver
    steps:
      - attach_workspace:
          at: /ws
      - run:
          name: Set number of make jobs
          command: echo "export MAKEFLAGS=-j2" >> $BASH_ENV
      - run:
          name: Install CogUtil
          command: cd /ws/cogutil/build && make -j2 install && ldconfig
      - restore_cache:
          name: Restore GHC 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: cd /ws/atomspace/build && make install && ldconfig
      - run:
          name: Checkout CogServer
          command: git clone --depth 1 https://github.com/$CIRCLE_PROJECT_USERNAME/cogserver .
      - run:
          name: CMake Configure
          command: mkdir build && cd build && cmake ..
      - run:
          name: Build
          command: cd build && make
      - run:
          name: Build tests
          command: cd build && make tests
#
# Disable, until https://github.com/opencog/cogserver/issues/5
# is fixed. The failing ShellUTest makes circleci report false
# negatives.
#      - run:
#          name: Run tests
#          command: cd build && make test ARGS="$MAKEFLAGS"
#      - run:
#          name: Print test log
#          command: cat build/tests/Testing/Temporary/LastTest.log
#          when: always
      - run:
          name: Install CogServer
          command: cd build && make install && ldconfig
      - persist_to_workspace:
          root: /ws/
          paths:
            - cogserver
            - ccache

  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: Set number of make jobs
          command: echo "export MAKEFLAGS=-j2" >> $BASH_ENV
      - run:
          name: Install CogUtil
          command: cd /ws/cogutil/build && make install && ldconfig
      - restore_cache:
          name: Restore GHC 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: cd /ws/atomspace/build && make install && ldconfig
      - run:
          name: Install CogServer
          command: cd /ws/cogserver/build && make install && ldconfig
      - run:
          name: Checkout Attention
          command: git clone --depth 1 https://github.com/$CIRCLE_PROJECT_USERNAME/attention .
      - run:
          name: CMake Configure
          command: mkdir build && cd build && cmake ..
      - run:
          name: Build
          command: cd build && make
      - run:
          name: Build tests
          command: cd build && make tests
      - run:
          name: Run tests
          command: cd build && make test ARGS="$MAKEFLAGS"
      - run:
          name: Install Attention
          command: cd build && make install && ldconfig
      - run:
          name: Print test log
          command: cat build/tests/Testing/Temporary/LastTest.log
          when: always
      - persist_to_workspace:
          root: /ws/
          paths:
            - 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 GHC 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 URE
          command: cd /ws/ure/build && make -j2 install && ldconfig
      - run:
          name: Install CogServer
          command: cd /ws/cogserver/build && make install && ldconfig
      - run:
          name: Install Attention
          command: cd /ws/attention/build && make 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/atomspace
    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:
      - atomspace
      - atomspace-rocks:
          requires:
            - atomspace
      - cogserver:
          requires:
            - atomspace
      - ure:
          requires:
            - atomspace
      - spacetime:
          requires:
            - atomspace
      - pln:
          requires:
            - atomspace
            - ure
            - spacetime
      - miner:
          requires:
            - ure
      - attention:
          requires:
            - cogserver
      - opencog:
          requires:
            - atomspace
            - ure
            - cogserver
            - attention
      - package:
          requires:
            - opencog
          filters:
            branches:
              only: master
