name: Package for Linux
description: Create Linux packages for Prism Launcher

inputs:
  version:
    description: Launcher version
    required: true
  build-type:
    description: Type for the build
    required: true
    default: Debug
  artifact-name:
    description: Name of the uploaded artifact
    required: true
    default: Linux
  qt-version:
    description: Version of Qt to use
    required: true
  gpg-private-key:
    description: Private key for AppImage signing
    required: false
  gpg-private-key-id:
    description: ID for the gpg-private-key, to select the signing key
    required: false

runs:
  using: composite

  steps:
    - name: Setup build variables
      shell: bash
      run: |
        # Fixup architecture naming for AppImages
        dpkg_arch="$(dpkg-architecture -q DEB_HOST_ARCH_CPU)"
        case "$dpkg_arch" in
          "amd64")
            APPIMAGE_ARCH="x86_64"
            ;;
          "arm64")
            APPIMAGE_ARCH="aarch64"
            ;;
          *)
            echo "# 🚨 The Debian architecture \"$deb_arch\" is not recognized!" >> "$GITHUB_STEP_SUMMARY"
            exit 1
            ;;
        esac
        echo "APPIMAGE_ARCH=$APPIMAGE_ARCH" >> "$GITHUB_ENV"

        # Used for the file paths of libraries
        echo "DEB_HOST_MULTIARCH=$(dpkg-architecture -q DEB_HOST_MULTIARCH)" >> "$GITHUB_ENV"

    - name: Package AppImage
      shell: bash
      env:
        VERSION: ${{ github.ref_type == 'tag' && github.ref_name || inputs.version }}
        BUILD_DIR: build
        INSTALL_APPIMAGE_DIR: install-appdir

        GPG_PRIVATE_KEY: ${{ inputs.gpg-private-key }}
      run: |
        cmake --install ${{ env.BUILD_DIR }} --config ${{ inputs.build-type }} --prefix ${{ env.INSTALL_APPIMAGE_DIR }}

        if [ '${{ inputs.gpg-private-key-id }}' != '' ]; then
          echo "$GPG_PRIVATE_KEY" > privkey.asc
          gpg --import privkey.asc
          gpg --export --armor 9C7A2C9B62603299 > pubkey.asc
        else
          echo ":warning: Skipped code signing for Linux AppImage, as gpg key was not present." >> $GITHUB_STEP_SUMMARY
        fi

        sharun lib4bin \
          --hard-links \
          --with-hooks \
          --dst-dir "$INSTALL_APPIMAGE_DIR" \
          "$INSTALL_APPIMAGE_DIR"/bin/* "$QT_PLUGIN_PATH"/*/*.so

        cp ~/bin/AppImageUpdate.AppImage "$INSTALL_APPIMAGE_DIR"/bin/
        # FIXME(@getchoo): gamemode doesn't seem to be very portable with DBus. Find a way to make it work!
        find "$INSTALL_APPIMAGE_DIR" -name '*gamemode*' -exec rm {} +

        ln -s org.prismlauncher.PrismLauncher.metainfo.xml "$INSTALL_APPIMAGE_DIR"/share/metainfo/org.prismlauncher.PrismLauncher.appdata.xml
        ln -s share/applications/org.prismlauncher.PrismLauncher.desktop "$INSTALL_APPIMAGE_DIR"
        ln -s share/icons/hicolor/256x256/apps/org.prismlauncher.PrismLauncher.png "$INSTALL_APPIMAGE_DIR"
        mv "$INSTALL_APPIMAGE_DIR"/{sharun,AppRun}
        ls -la "$INSTALL_APPIMAGE_DIR"

        if [[ "${{ github.ref_type }}" == "tag" ]]; then
          APPIMAGE_DEST="PrismLauncher-Linux-$APPIMAGE_ARCH.AppImage"
        else
          APPIMAGE_DEST="PrismLauncher-Linux-$VERSION-${{ inputs.build-type }}-$APPIMAGE_ARCH.AppImage"
        fi

        mkappimage \
          --updateinformation "gh-releases-zsync|${{ github.repository_owner }}|${{ github.event.repository.name }}|latest|PrismLauncher-Linux-$APPIMAGE_ARCH.AppImage.zsync" \
          "$INSTALL_APPIMAGE_DIR" \
          "$APPIMAGE_DEST"

    - name: Package portable tarball
      shell: bash
      env:
        BUILD_DIR: build

        INSTALL_PORTABLE_DIR: install-portable
      run: |
        cmake --install ${{ env.BUILD_DIR }} --config ${{ inputs.build-type }} --prefix ${{ env.INSTALL_PORTABLE_DIR }}
        cmake --install ${{ env.BUILD_DIR }} --config ${{ inputs.build-type }} --prefix ${{ env.INSTALL_PORTABLE_DIR }} --component portable

        sharun lib4bin \
          --with-hooks \
          --hard-links \
          --dst-dir "$INSTALL_PORTABLE_DIR" \
          "$INSTALL_PORTABLE_DIR"/bin/* "$QT_PLUGIN_PATH"/*/*.so

        # FIXME(@getchoo): gamemode doesn't seem to be very portable with DBus. Find a way to make it work!
        find "$INSTALL_PORTABLE_DIR" -name '*gamemode*' -exec rm {} +

        for l in $(find ${{ env.INSTALL_PORTABLE_DIR }} -type f -o -type l); do l=${l#$(pwd)/}; l=${l#${{ env.INSTALL_PORTABLE_DIR }}/}; l=${l#./}; echo $l; done > ${{ env.INSTALL_PORTABLE_DIR }}/manifest.txt
        cd ${{ env.INSTALL_PORTABLE_DIR }}
        tar -czf ../PrismLauncher-portable.tar.gz *

    - name: Upload binary tarball
      uses: actions/upload-artifact@v6
      with:
        name: PrismLauncher-${{ inputs.artifact-name }}-Qt6-Portable-${{ inputs.version }}-${{ inputs.build-type }}
        path: PrismLauncher-portable.tar.gz

    - name: Upload AppImage
      uses: actions/upload-artifact@v6
      with:
        name: PrismLauncher-${{ runner.os }}-${{ inputs.version }}-${{ inputs.build-type }}-${{ env.APPIMAGE_ARCH }}.AppImage
        path: PrismLauncher-${{ runner.os }}-*${{ env.APPIMAGE_ARCH }}.AppImage

    - name: Upload AppImage Zsync
      uses: actions/upload-artifact@v6
      with:
        name: PrismLauncher-${{ runner.os }}-${{ inputs.version }}-${{ inputs.build-type }}-${{ env.APPIMAGE_ARCH }}.AppImage.zsync
        path: PrismLauncher-${{ runner.os }}-*${{ env.APPIMAGE_ARCH }}.AppImage.zsync
