name: Build Lyrion Music Server
description: Build Lyrion Music Server with the given parameters
inputs:
  build-params:
    description: Parameters needed to run the `buildme.pl` script
    required: true
    default: ubuntu-latest
  build-type:
    description: "`nightly` or `release`"
    required: true
    default: nightly
  AWS_KEY_ID:
    description: The key ID to use to upload to S3
  AWS_SECRET_ACCESS_KEY:
    description: The secret to use to upload to S3
  SIGNPATH_API_TOKEN:
    description: The SignPath API token to use for code signing
  SIGNPATH_ORG_ID:
    description: The SignPath organization ID to use for code signing
  SIGNPATH_PROJECT_SLUG:
    description: The SignPath project slug to use for code signing
  SIGNPATH_SIGNING_POLICY_SLUG:
    description: The SignPath signing policy slug to use for code signing
  SIGNPATH_ARTIFACT_CONFIG_SLUG:
    description: The SignPath artifact config slug to use for code signing

runs:
  using: composite
  steps:
    - name: Get LMS version number
      shell: bash
      id: getversion
      run: |
        echo "::group::Get LMS version number"
        MAJOR=$(grep "our \$VERSION" -m1 server/slimserver.pl | cut -d"'" -f2 | cut -d. -f1)
        MINOR=$(grep "our \$VERSION" -m1 server/slimserver.pl | cut -d"'" -f2 | cut -d. -f2)
        PATCH=$(grep "our \$VERSION" -m1 server/slimserver.pl | cut -d"'" -f2 | cut -d. -f3)

        echo "LMS_VERSION=$MAJOR.$MINOR" >> $GITHUB_OUTPUT
        echo "LMS_FULL_VERSION=$MAJOR.$MINOR.$PATCH" >> $GITHUB_OUTPUT
        echo "GIT_BRANCH=public/$MAJOR.$MINOR" >> $GITHUB_OUTPUT
        echo "::endgroup::"


    - name: Check out LMS platform code
      uses: actions/checkout@v6
      with:
        repository: ${{ github.repository_owner }}/slimserver-platforms
        path: platforms
        ref: ${{ steps.getversion.outputs.GIT_BRANCH }}


    - name: Check out pCP platform code
      if: ${{ startsWith(inputs.build-params, 'pcp') }}
      uses: actions/checkout@v6
      with:
        repository: picoreplayer/lms-update-script
        path: platforms/pcp

    - name: Set up Docker environment
      if: ${{ startsWith(inputs.build-params, 'docker') }}
      id: dockersetup
      shell: bash
      # TODO - needs to be smarter!
      run: |
        if [ "${{ inputs.build-type }}" = "release" ]; then
          echo "LMS_TAG=--tag=rc" >> $GITHUB_OUTPUT
        elif [ "${{ steps.getversion.outputs.LMS_VERSION }}" = "9.1" ]; then
          echo "LMS_TAG=--tag=dev,${{ steps.getversion.outputs.LMS_FULL_VERSION }}-dev" >> $GITHUB_OUTPUT
        elif [ "${{ steps.getversion.outputs.LMS_VERSION }}" = "9.0" ]; then
          echo "LMS_TAG=--tag=stable,${{ steps.getversion.outputs.LMS_FULL_VERSION }}-stable" >> $GITHUB_OUTPUT
        else
          echo "UNKNOWN DOCKER TARGET TAG"
          exit 1
        fi


    - name: Set up macOS environment
      if: ${{ inputs.build-params == 'macos' }}
      shell: bash
      run: |
        echo "::group::Set up macOS environment"
        # set up Platypus - see https://github.com/sveinbjornt/Platypus/issues/270#issuecomment-2312235079
        echo "Creating directory structures"
        mkdir -p /usr/local/bin || echo $?
        mkdir -p /usr/local/share/platypus || echo $?

        cd platforms/osx/platypus

        echo "Copying resources to share directory"
        sudo cp -nfr ScriptExec MainMenu.nib /usr/local/share/platypus
        sudo chmod -R 755 /usr/local/share/platypus

        echo "Installing command line tool"
        sudo cp -nf platypus /usr/local/bin
        sudo chmod +x /usr/local/bin/platypus
        echo "::endgroup::"


    - name: Set up Windows environment
      if: ${{ startsWith(inputs.build-params, 'win64') }}
      shell: bash
      run: |
        echo "::group::Set up Windows environment"
        cp -nf platforms/win32/tools/* 'c:\Program Files\Git\usr\bin\'
        echo "::endgroup::"


    - name: Build ${{ inputs.build-params }} package on ${{ runner.os }}
      shell: bash
      run: |
        echo "::group::Build ${{ inputs.build-params }} package on ${{ runner.os }}"
        SRC_PATH=$PWD

        BUILD_PATH=$SRC_PATH/work
        DEST_PATH=$SRC_PATH/publish

        mkdir -p $BUILD_PATH
        mkdir -p $DEST_PATH

        platforms/buildme.pl --build ${{ inputs.build-params }} ${{ steps.dockersetup.outputs.LMS_TAG }} --buildDir $BUILD_PATH --sourceDir $SRC_PATH --destDir $DEST_PATH --releaseType ${{ inputs.build-type }}
        echo "::endgroup::"

    - name: Verify results
      if: ${{ !startsWith(inputs.build-params, 'docker') }}
      shell: bash
      run: |
        if [ ! "$(ls -A publish/)" ]; then
          exit 1;
        fi

    - name: Cache s5cmd
      if: ${{ !startsWith(inputs.build-params, 'docker') }}
      uses: actions/cache@v5
      with:
        key: s5cmd-${{ runner.os }}
        path: ${{ runner.temp }}/s5cmd-bin

    - name: Check if s5cmd is installed
      if: ${{ !startsWith(inputs.build-params, 'docker') }}
      id: check-s5cmd
      continue-on-error: true
      shell: bash
      run: |
        PATH="${{ runner.temp }}/s5cmd-bin:$PATH"
        if command -v s5cmd &> /dev/null; then
          echo "s5cmd is already installed"
          echo "installed=true" >> $GITHUB_OUTPUT
          echo "${{ runner.temp }}/s5cmd-bin" >> $GITHUB_PATH
        else
          echo "s5cmd is not installed"
          echo "installed=false" >> $GITHUB_OUTPUT
        fi

    - name: Set-up s5cmd
      if: ${{ !startsWith(inputs.build-params, 'docker') && steps.check-s5cmd.outputs.installed != 'true' }}
      # don't fail, as we can fall back to aws-cli
      continue-on-error: true
      uses: ./server/.github/actions/setup-s5cmd
      # uses: peak/action-setup-s5cmd@main
      with:
        version: v2.3.0

    - name: Archive artifacts
      if: ${{ !startsWith(inputs.build-params, 'docker') }}
      uses: actions/upload-artifact@v6
      id: upload-artifact
      with:
        name: ${{ inputs.build-params }}
        path: publish

    - name: Prepare Windows release signing
      if: ${{ startsWith(inputs.build-params, 'win64') && inputs.build-type == 'release' }}
      uses: signpath/github-action-submit-signing-request@v2
      with:
        api-token: '${{ inputs.SIGNPATH_API_TOKEN }}'
        organization-id: '${{ inputs.SIGNPATH_ORG_ID }}'
        project-slug: '${{ inputs.SIGNPATH_PROJECT_SLUG }}'
        signing-policy-slug: '${{ inputs.SIGNPATH_SIGNING_POLICY_SLUG }}'
        artifact-configuration-slug: '${{ inputs.SIGNPATH_ARTIFACT_CONFIG_SLUG }}'
        github-artifact-id: '${{ steps.upload-artifact.outputs.artifact-id }}'
        wait-for-completion: true
        output-artifact-directory: publish

    - name: Calculate hash for signed Windows binary
      if: ${{ startsWith(inputs.build-params, 'win64') && inputs.build-type == 'release' }}
      shell: bash
      run: |
        cd publish
        for f in LyrionMusicServer-*exe;
        do
          md5sum $f > $f.md5
        done

    - name: Upload artifacts to R2
      if: ${{ !startsWith(inputs.build-params, 'docker') && github.repository_owner == 'LMS-Community' }}
      shell: bash
      env:
        AWS_ACCESS_KEY_ID: ${{ inputs.AWS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ inputs.AWS_SECRET_ACCESS_KEY }}
        AWS_REGION: us-east-1
      run: |
        echo "::group::Upload artifacts to R2"
        TARGET_FOLDER="${{ inputs.build-type }}"

        if [ "${{ inputs.build-type }}" = "release" ]; then
          TARGET_FOLDER="LyrionMusicServer_v${{ steps.getversion.outputs.LMS_FULL_VERSION }}"
        fi

        ENDPOINT_URL="https://e83bc03c881257fada7ca16c243e9298.eu.r2.cloudflarestorage.com"
        S3_CMD="aws --endpoint-url $ENDPOINT_URL s3"
        if command -v s5cmd 2>&1 >/dev/null; then
          S3_CMD="s5cmd --endpoint-url $ENDPOINT_URL"
        fi

        for f in publish/?yrion?usic?erver*;
        do
          $S3_CMD cp $f s3://downloads/$TARGET_FOLDER/
        done
        echo "::endgroup::"
