<?xml version="1.0" encoding="UTF-8"?>
<!--
   Copyright 2022-2026 DATA @ UHN. See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>io.uhndata.cards</groupId>
    <artifactId>yourexperience-parent</artifactId>
    <version>1.2.1</version>
  </parent>

  <artifactId>yourexp-docker-packaging</artifactId>
  <packaging>pom</packaging>
  <name>YourExperience - Docker image packaging</name>
  <properties>
    <docker.name>cards/cards4yourexp:%l</docker.name>
    <cardsBaseImage>cards/cards:${cards.version}</cardsBaseImage>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-remote-resources-plugin</artifactId>
        <dependencies>
          <dependency>
            <groupId>io.uhndata.cards</groupId>
            <artifactId>cards-license-resources</artifactId>
            <version>1</version>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <id>generate-dependencies-resources</id>
            <goals>
              <goal>process</goal>
            </goals>
            <configuration>
              <resourceBundles>
                <resourceBundle>io.uhndata.cards:cards-project-dependencies-resources:3</resourceBundle>
              </resourceBundles>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.5.0</version>
        <executions>
          <execution>
            <id>attach-artifacts</id>
            <phase>package</phase>
            <goals>
              <goal>attach-artifact</goal>
            </goals>
            <configuration>
              <artifacts>
                <artifact>
                  <file>${project.build.directory}/maven-shared-archive-resources/sling-features.json</file>
                  <type>dependencies</type>
                </artifact>
              </artifacts>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>latest</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <properties>
        <cardsBaseImage>cards/cards:latest</cardsBaseImage>
      </properties>
    </profile>
    <profile>
      <id>local</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <properties>
        <cardsBaseImage>cards/cards:${cards.version}</cardsBaseImage>
      </properties>
    </profile>
    <profile>
      <id>published</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <properties>
        <cardsBaseImage>ghcr.io/data-team-uhn/cards:${cards.version}</cardsBaseImage>
      </properties>
    </profile>

    <profile>
      <id>development</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <inherited>false</inherited>
            <executions>
              <execution>
                <configuration>
                  <executable>bash</executable>
                  <workingDirectory>.</workingDirectory>
                  <commandlineArgs>-c "rm -rf .m2/repository ; mkdir -p .m2/repository"</commandlineArgs>
                </configuration>
                <id>fake_project_jars</id>
                <phase>prepare-package</phase>
                <goals>
                   <goal>exec</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.43.4</version>
            <executions>
              <execution>
                <id>build</id>
                <phase>package</phase>
                <goals>
                  <goal>build</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <imagePullPolicy>IfNotPresent</imagePullPolicy>
              <verbose>all</verbose>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>production</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <inherited>false</inherited>
            <executions>
              <execution>
                <configuration>
                  <executable>bash</executable>
                  <workingDirectory>.</workingDirectory>
                  <commandlineArgs>check_production_base_image.sh</commandlineArgs>
                  <environmentVariables>
                    <cardsBaseImage>${cardsBaseImage}</cardsBaseImage>
                    <cardsBaseVersion>${cards.version}</cardsBaseVersion>
                  </environmentVariables>
                  <useMavenLogger>true</useMavenLogger>
                </configuration>
                <id>check_production_base_image</id>
                <phase>validate</phase>
                <goals>
                   <goal>exec</goal>
                </goals>
              </execution>
              <execution>
                <configuration>
                  <executable>bash</executable>
                  <workingDirectory>.</workingDirectory>
                  <commandlineArgs>get_project_jars.sh</commandlineArgs>
                </configuration>
                <id>get_project_jars</id>
                <phase>prepare-package</phase>
                <goals>
                   <goal>exec</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.43.4</version>
            <executions>
              <execution>
                <id>build</id>
                <phase>package</phase>
                <goals>
                  <goal>build</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <imagePullPolicy>IfNotPresent</imagePullPolicy>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
