.get_vars:
    script:
        - IS_DEV=$([[ $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH && $CI_COMMIT_BRANCH != "test" ]] && echo true || echo false)
        - IS_TEST=$([[ $CI_COMMIT_BRANCH == "test" ]] && echo true || echo false)
        - |
            if [[ $IS_TEST == true ]]; then
                DOCKER_REGISTRY_USER="dojohessotest"
                DOCKER_REGISTRY_IMAGE="dojohesso/dojo-test_assignment_checker"
                DOCKER_REGISTRY_PASSWORD=$DOCKER_TEST_REGISTRY_PASSWORD
                DOTENV_PROD_KEY=$DOTENV_TEST_KEY
            fi


.get_version:
    script:
        - VERSION=$(jq -r .version $PROJECT_FOLDER/package.json)$([[ $IS_TEST == true ]] && echo $VERSION_TEST_SUFFIX || echo '' )$([[ $IS_DEV == true ]] && echo $VERSION_DEV_SUFFIX || echo '')


.get_packages_url:
    script:
        # Wiki
        - WIKI_ARCHIVE_NAME="${PROJECT_NAME}_Wiki_${VERSION}.tar.xz"
        - PACKAGE_URL_WIKI="${PACKAGE_REGISTRY_URL}_Wiki/${VERSION}/${WIKI_ARCHIVE_NAME}"


.init_dind_script:
    script:
        # Install dependencies
        - apk update
        - apk add git
        - apk add jq
        - apk add curl

        # Init docker buildx
        - |
            if [[ $CI_COMMIT_REF_PROTECTED == "true" || $IS_TEST == true ]]; then
                docker login -u $DOCKER_REGISTRY_USER -p $DOCKER_REGISTRY_PASSWORD $DOCKER_REGISTRY
            fi
        - docker buildx create --use

        # Get version from package.json
        - !reference [ .get_version, script ]
        - CONTAINER_IMAGE=$DOCKER_REGISTRY_IMAGE:$VERSION


.build_script:
    script:
        - !reference [ .init_dind_script, script ]
        - mkdir -p $ARTIFACTS_FOLDER

        # Decrypt env vars for production
        - apk add npm sed
        - cd $PROJECT_FOLDER
        - sed -i -r "s/\{\{VERSION\}\}/${VERSION}/g" src/app.ts # Needed for have the -dev ou -test suffix in the version
        - |
            if [[ $CI_COMMIT_REF_PROTECTED == "true" || $IS_TEST == true ]]; then
                echo "Decrypt production env vars"
                sed -i -r "s/(DOTENV_KEY[ ]*:[ ]*[\'\"\`])[^'\"\`]*([\'\"\`])([ ]*\,)?//g" src/init.ts
                sed -i -r "s/,[\ \n]*\}/\}/g" src/init.ts
            
                echo "DOTENV_KEY_PRODUCTION=\"${DOTENV_PROD_KEY}\"" > .env.keys
                npx @dotenvx/dotenvx@0.45.0 decrypt
                mv .env.production .env
                rm .env.keys
            fi
        - cd ..

        # Rename executable for test
        - |
            if [[ $IS_TEST == true ]]; then
                sed -i -r "s%dojo_assignment_checker%dojo-test_assignment_checker%g" $DOCKERFILE
            fi

        # Need to build for each platform separately because of multi-stage builds (docker buildx don't use cache same way as docker build)
        - >
            platform_array=$(echo $DOCKER_PLATFORMS | tr "," "\n");
            for platform in $platform_array; do
              echo "Buildind for : $platform"
              docker buildx build --pull --platform $platform --file $DOCKERFILE --tag $CONTAINER_IMAGE .
            done


.clean_release:
    script:
        # Delete release if it already exists
        - 'curl --request DELETE --header "JOB-TOKEN: $CI_JOB_TOKEN" "${GITLAB_API_PROJECT_URL}/releases/${VERSION}"'

        # Delete tag if it already exists (use private-token because job-token don't have permission to delete tags)
        - 'curl --request DELETE --header "PRIVATE-TOKEN: $GITLAB_PROJECT_ACCESS_TOKEN" "${GITLAB_API_PROJECT_URL}/repository/tags/${VERSION}"'


.clean_packages:
    script:
        # Get all packages of the project
        - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" "${GITLAB_API_PROJECT_URL}/packages" > gitlabPackages.json'

        # Filter and select packages to delete (based on version)
        - packagesToDelete=`jq -r '.[] | select(.version=="'${VERSION}'") | ._links.delete_api_path' gitlabPackages.json`

        # Delete packages by calling Gitlab API
        - >
            for deletePath in $packagesToDelete; do
              echo "Deleting package at path : ${deletePath}"
              curl --request DELETE --header "JOB-TOKEN: $CI_JOB_TOKEN" "${deletePath}"
            done