#!/usr/bin/env python3
# This file is automatically generated by fs-package

import os
import sys

sys.path.insert(0, ".")
from fsbuild import (
    checkNotarizationResult,
    getBundlePath,
    getPackageInformation,
    isMacOS,
    notarizeApp,
    run,
    shell,
)


def notarizeForMacOS():
    bundleId = getPackageInformation().bundleId
    bundlePath = getBundlePath()
    bundleName = os.path.basename(bundlePath)
    bundleParentDir = os.path.dirname(bundlePath)
    shell("rm -f fsbuild/_build/notarize.zip")
    zip = "../../../notarize.zip"
    shell(
        f'cd {bundleParentDir} && ditto -c -k --keepParent "{bundleName}" "{zip}"'
    )
    requestUuid = notarizeApp("fsbuild/_build/notarize.zip", bundleId)
    checkNotarizationResult(requestUuid)

    if bundlePath.endswith(".framework"):
        print(
            "Does not seem to be possible to staple tickets to frameworks? (error 73)"
        )
        print("Exiting...")
        sys.exit(0)

    run(["xcrun", "stapler", "staple", bundlePath])


def main():
    if getPackageInformation().type == "fs-data-plugin":
        print("Not notarizing data plugin")
    elif isMacOS():
        notarizeForMacOS()
    else:
        print("Skipping sign step (no signatures for this platform)")


if __name__ == "__main__":
    main()
