version: 2

jobs:
  build:
    docker:
      - image: "ubuntu:latest"
    steps:
      - run:
          name: Installing GCC, git and cmake
          command: |
              apt-get update 
              apt-get install -yqq --no-install-recommends software-properties-common \
                 git cmake gcc g++ curl libunbound-dev libunwind8-dev libcurl4-openssl-dev \
                 libhidapi-dev \
                 libssl-dev libsodium-dev libzmq3-dev pkg-config libpgm-dev \
                 libboost-program-options-dev libboost-chrono-dev libboost-filesystem-dev \
                 libboost-serialization-dev libboost-regex-dev  libboost-locale-dev \
                 libboost-thread-dev libboost-fiber-dev \
                 libjsoncpp-dev uuid-dev libmysql++-dev 
      - checkout
      - run:
          name: Get Last monero commit hash 
          command: |
              export MONERO_COMMIT=`git ls-remote https://github.com/monero-project/monero HEAD | head -n1 | awk '{print $1;}'`
              echo $MONERO_COMMIT > ~/monerocommit
              cat ~/monerocommit
      - restore_cache:
          key: v01-monero-{{ checksum "~/monerocommit" }}
      - run:
          name: Compile Monero
          command: |
                  if [ ! -d "monero" ]; then 
                  git clone --recurse-submodules https://github.com/monero-project/monero.git 
                  cd monero
                  mkdir -p build/release && cd build/release
                  cmake -D CMAKE_BUILD_TYPE=Release ../..
                  cmake --build . --target cryptonote_core -- -j 2
                  cd ../..
                  fi
      - save_cache: 
          key: v01-monero-{{ checksum "~/monerocommit" }}
          paths: 
            - "monero"
      - run:
          name: Get submodules
          command: |
              git submodule update --init --recursive
      - run:
          name: Creating Build Files 
          command: 'cmake -DMONERO_DIR=/root/project/monero -DBUILD_TESTS=ON -H. -Bbuild'
      - run:
          name: Creating Binary Files
          command: 'cmake --build build -- -j 2'
