commands:
    setup_docker_compose_override:
        steps:
            -   run:
                    name: Copy docker-compose.override.yml.dist
                    command: cp .circleci/docker-compose.override.yml.dist docker-compose.override.yml

    fix_files_permissions:
        steps:
            -   run:
                    name: Create missing directories
                    command: mkdir -p ~/.composer ~/.cache/yarn ~/.cache/Cypress
            -   run:
                    name: Change owner on project dir (docker needs uid 1000, circleci can be another uid)
                    command: sudo chown -R 1000:1000 ../project ~/.composer ~/.cache/yarn ~/.cache/Cypress
            -   run:
                    name: Change permissions on project dir
                    command: sudo chmod -R 777 ../project ~/.cache/yarn ~/.cache/Cypress

    load_docker_image_php:
        steps:
            -   run:
                    name: Load php docker image
                    command: |
                        [ ! -f ~/project/php-pim-image.tar ] || docker load -i php-pim-image.tar
                        [ -f ~/project/php-pim-image.tar ] || docker-compose pull php

    load_docker_image_node:
        steps:
            -   run:
                    name: Load node docker image
                    command: docker-compose pull node

    start_test_containers:
        steps:
            -   run:
                    name: Pull docker images
                    command: docker-compose pull mysql elasticsearch object-storage pubsub-emulator
            -   run:
                    name: Start containers
                    command: |
                        APP_ENV=test C='fpm mysql elasticsearch object-storage pubsub-emulator' make up
                        docker/wait_docker_up.sh
            -   run:
                    name: Install database
                    command: APP_ENV=test O="--withoutFixtures" make database

    restore_frontend_dependency_cache:
        steps:
            -   run:
                    name: Create hash - front dependencies
                    command: cat yarn.lock > ~/front-dependency.hash && date +%F >> ~/front-dependency.hash
            -   restore_cache:
                    name: Restore cache - front dependencies
                    key: frontend-dependency-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/front-dependency.hash" }}

    restore_frontend_dsm_cache:
        parameters:
            path_to_front_packages:
                description: "Path to the front-packages directory, eg: front-packages"
                type: string
        steps:
            -   run:
                    name: Create hash - DSM
                    command: |
                        find << parameters.path_to_front_packages >>/akeneo-design-system -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum > ~/akeneo-design-system.hash
                        echo "$(date +%F) << parameters.path_to_front_packages >>" | tee -a ~/akeneo-design-system.hash
            -   restore_cache:
                    name: Restore cache - DSM
                    key: front-packages-dsm-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/akeneo-design-system.hash" }}

    restore_frontend_shared_cache:
        parameters:
            path_to_front_packages:
                description: "Path to the front-packages directory, eg: front-packages"
                type: string
        steps:
            -   run:
                    name: Create hash - Shared
                    command: |
                        find << parameters.path_to_front_packages >>/shared -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum > ~/shared.hash
                        echo "$(date +%F) << parameters.path_to_front_packages >>" | tee -a ~/shared.hash
            -   restore_cache:
                    name: Restore cache - Shared
                    key: front-packages-shared-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/shared.hash" }}

    restore_backend_dependency_cache:
        steps:
            -   run:
                    name: Creating cache key for back dependencies
                    command: cat composer.json > ~/back-dependency.hash && date +%F >> ~/back-dependency.hash
            -   restore_cache:
                    name: Restore cache - back dependencies
                    keys:
                        - backend-dependency-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/back-dependency.hash" }}

    build_docker_image_php:
        steps:
            -   run:
                    name: Creating hash key for PHP Docker image
                    command: |
                        find Dockerfile docker/ -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum > ~/php-docker-image.hash
                        date +%F >> ~/php-docker-image.hash
            -   restore_cache:
                    name: Restore PHP docker image cache
                    key: php-docker-image-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/php-docker-image.hash" }}
            -   run:
                    name: Build the latest EE Docker images
                    command: |
                        [ ! -f ~/project/php-pim-image.tar ] || docker load -i php-pim-image.tar
                        [ -f ~/project/php-pim-image.tar ] || make php-image-dev
                        [ -f ~/project/php-pim-image.tar ] || docker save -o php-pim-image.tar akeneo/pim-dev/php:8.1
            -   save_cache:
                    name: Save PHP docker image cache
                    key: php-docker-image-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/php-docker-image.hash" }}
                    paths:
                        - php-pim-image.tar
