name: renpy-distribute
description: "Build a RenPy project for various platforms."
inputs:
  project:
    description: "Path to the RenPy project."
    required: true
  package:
    description: "The specific package to build. Values: win, mac, linux, android, pc, market" #TODO: android
    required: true
  dest:
    description: "Destination folder for the builds."
    required: false
  packageDest:
    description: "Destination folder for the individual package."
    required: false
  archive:
    description: "Whether or not to compress the build into a zip file."
    required: true
    default: "true"
  format:
    description: "The archive format to use. Values: zip, tar.bz2 Default: zip"
    required: true
    default: "zip"
  additionalArgs:
    description: "Additional arguments to pass into RenPy."
    required: false
outputs:
  package:
    description: "The path to the package or packages."
    value: ${{ steps.command.outputs.output }}
runs:
  using: "composite"
  steps:
  - name: "Build command."
    id: command
    run: |
      # Replace each space with a backslash and a space
      PROJECT="${{ inputs.project }}"
      PROJECT="${PROJECT// /\\ }"
      COMMAND="distribute $PROJECT --package ${{ inputs.package }} ${{ inputs.additionalArgs }}"

      ARCHIVE="${{ inputs.archive }}"
      if [[ "$ARCHIVE" == "false" ]]; then
        COMMAND="$COMMAND --no-archive"
        EXT=""
      else
        EXT=".${{ inputs.format }}"
      fi

      DEST="${{ inputs.dest }}"
      if [[ -n "$DEST" ]]; then
        OUTPUT="$DEST"
        mkdir -p "$DEST"
        COMMAND="$COMMAND --destination $DEST"
      fi
      FORMAT="${{ inputs.format }}"
      if [[ -n "$FORMAT" ]]; then
        COMMAND="$COMMAND --format $FORMAT"
      fi
      PACKAGE_DEST="${{ inputs.packageDest }}"
      if [[ -n "$PACKAGE_DEST" ]]; then
        OUTPUT="${PACKAGE_DEST}${EXT}"
        mkdir -p "$PACKAGE_DEST"
        COMMAND="$COMMAND --packagedest $PACKAGE_DEST"
      fi
      echo "command=$COMMAND" >> "$GITHUB_OUTPUT"
      echo "output=$OUTPUT" >> "$GITHUB_OUTPUT"
    shell: bash
  - name: "Run RenPy distribute command."
    uses: act/common/renpy/renpy@master
    with:
      command: ${{ steps.command.outputs.command }}
      catchErrors: true
