version: 2
jobs:
  update_unity_repo:
    docker:
      - image: circleci/buildpack-deps:latest

    working_directory: ~/repo

    environment:
      UniJSON_unity_REPO: "git@github.com:ousttrue/UniJSON-unity.git"

    steps:
      - add_ssh_keys:
          fingerprints:
            - "5b:16:4a:8a:c9:a5:2c:80:37:d4:a5:b8:1b:0d:60:27"
      - run:
          name: Avoid hosts unknown for github
          command: echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config

      - run:
          name: Clone UniJSON-unity (NOT UniJSON)
          command: |
            git clone ${UniJSON_unity_REPO}
            cd UniJSON-unity
            git submodule update --init

      - run:
          name: Update submodule(UniJSON) in UniJSON-unity
          command: |
            cd UniJSON-unity/Assets/UniJSON
            git fetch && git checkout --force ${CIRCLE_SHA1}

      - run:
          command: |
            cd UniJSON-unity
            git config user.email "yutopp+unijson-unity@users.noreply.github.com"
            git config user.name "unijson-unity job"

      - run:
          command: |
            cd UniJSON-unity
            git add Assets/UniJSON
            git commit -m "[skip ci] Checkout UniJSON (${CIRCLE_SHA1})"

      - run:
          command: |
            cd UniJSON-unity
            git status
          when: on_fail

      - run:
          command: |
            cd UniJSON-unity
            git push origin master

workflows:
  version: 2
  build_test_deploy:
    jobs:
      - update_unity_repo:
          filters:
            branches:
              only: master
