def main(ctx):
    archs = ["amd64", "arm64"]   ## arm
    glpi_version = "10.0.18"
    alpine_version = "3.21"

    out = []
    for arch in archs:
        out += onpush(ctx, glpi_version, alpine_version, arch)

    for arch in archs:
        out += build_publish(ctx, glpi_version, alpine_version, arch)

    out += manifest_publish(ctx, glpi_version, archs)

    return out

def onpush(ctx, glpi_version, alpine_version, arch):
    return [{
        "kind": "pipeline",
        "type": "docker",
        "name": "build-onpush-%s-%s" % (glpi_version, arch),
        "platform": {
            "arch": arch,
            "os": "linux",
        },
        "steps": [{
            "name": "build_on_push",
            "image": "plugins/docker:linux-%s" % (arch),
            "settings": {
                "context": "src",
                "dockerfile": "src/Dockerfile",
                "dry_run": True,
                "repo": "docker.asperti.com/paspo/glpi", 
                "build_args": [
                    "GLPI_VERSION=%s" % (glpi_version),
                    "ALPINE_VERSION=%s" % (alpine_version)
                ],
            },
        }],
        "trigger": {
            "event": ['push'],
        }
    }]

def build_publish(ctx, glpi_version, alpine_version, arch):
    major = glpi_version.partition(".")[0];	
    return [{
        "kind": "pipeline",
        "type": "docker",
        "name": "build-publish-%s-%s" % (glpi_version, arch),
        "platform": {
            "arch": arch,
            "os": "linux",
        },
        "steps": [{
            "name": "build_on_push",
            "image": "plugins/docker:linux-%s" % (arch),
            "settings": {
                "context": "src",
                "dockerfile": "src/Dockerfile",
                "build_args": [
                    "GLPI_VERSION=%s" % (glpi_version),
                    "ALPINE_VERSION=%s" % (alpine_version)
                ],
                "username": {
                    "from_secret": "docker_username",
                },
                "password": {
                    "from_secret": "docker_password",
                },
                "registry": "docker.asperti.com",
                "repo": "docker.asperti.com/paspo/glpi",    
                "tags": [ 
                    "latest-%s" % (arch), 
                    "%s-%s" % (major, arch), 
                    "%s-%s" % (glpi_version, arch)
                ],
                "auto_tag": False,
                "force_tag": True,
                "daemon_off": False,            
            },
        }],
        "trigger": {
            "ref": [
                "refs/heads/master",
                "refs/tags/**",
            ],
        },
    }]

def manifest_publish(ctx, glpi_version, archs):
    major = glpi_version.partition(".")[0];	
    return [{
        "kind": "pipeline",
        "type": "docker",
        "name": "manifest-%s" % (glpi_version),
        "platform": {
            "arch": archs[0],
            "os": "linux",
        },
        "steps": [{
            "name": "manifest",
            "image": "plugins/manifest",
            "settings": {
                "spec": "manifest.tmpl",
                "username": {
                    "from_secret": "docker_username",
                },
                "password": {
                    "from_secret": "docker_password",
                }, 
                "tags": [ 
                    "latest", 
                    major, 
                    glpi_version
                ],
                "ignore_missing": True,
                "force_tag": True,
            },
        }],
        "trigger": {
            "ref": [
                "refs/heads/master",
                "refs/tags/**",
            ],
        },
        "depends_on": ["build-publish-%s-%s" % (glpi_version, arch) for arch in archs]
    }]