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
  cmake-preset:
    description: Base CMake preset previously used for the build
    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: ${{ inputs.version }}
        BUILD_DIR: build
        INSTALL_APPIMAGE_DIR: install-appdir

        GPG_PRIVATE_KEY: ${{ inputs.gpg-private-key }}
      run: |
        cmake --install ${{ env.BUILD_DIR }} --prefix ${{ env.INSTALL_APPIMAGE_DIR }}/usr

        mv ${{ env.INSTALL_APPIMAGE_DIR }}/usr/share/metainfo/org.prismlauncher.PrismLauncher.metainfo.xml ${{ env.INSTALL_APPIMAGE_DIR }}/usr/share/metainfo/org.prismlauncher.PrismLauncher.appdata.xml
        export "NO_APPSTREAM=1" # we have to skip appstream checking because appstream on ubuntu 20.04 is outdated

        export OUTPUT="PrismLauncher-Linux-$APPIMAGE_ARCH.AppImage"

        chmod +x linuxdeploy-*.AppImage

        mkdir -p ${{ env.INSTALL_APPIMAGE_DIR }}/usr/lib
        mkdir -p ${{ env.INSTALL_APPIMAGE_DIR }}/usr/plugins/iconengines

        cp -r ${{ runner.workspace }}/Qt/${{ inputs.qt-version }}/gcc_*64/plugins/iconengines/* ${{ env.INSTALL_APPIMAGE_DIR }}/usr/plugins/iconengines

        cp /usr/lib/"$DEB_HOST_MULTIARCH"/libcrypto.so.* ${{ env.INSTALL_APPIMAGE_DIR }}/usr/lib/
        cp /usr/lib/"$DEB_HOST_MULTIARCH"/libssl.so.* ${{ env.INSTALL_APPIMAGE_DIR }}/usr/lib/
        cp /usr/lib/"$DEB_HOST_MULTIARCH"/libOpenGL.so.0* ${{ env.INSTALL_APPIMAGE_DIR }}/usr/lib/

        LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${{ env.INSTALL_APPIMAGE_DIR }}/usr/lib"
        export LD_LIBRARY_PATH

        chmod +x AppImageUpdate-"$APPIMAGE_ARCH".AppImage
        cp AppImageUpdate-"$APPIMAGE_ARCH".AppImage ${{ env.INSTALL_APPIMAGE_DIR }}/usr/bin

        export UPDATE_INFORMATION="gh-releases-zsync|${{ github.repository_owner }}|${{ github.event.repository.name }}|latest|PrismLauncher-Linux-$APPIMAGE_ARCH.AppImage.zsync"

        if [ '${{ inputs.gpg-private-key-id }}' != '' ]; then
          export SIGN=1
          export SIGN_KEY=${{ inputs.gpg-private-key-id }}
          mkdir -p ~/.gnupg/
          echo "$GPG_PRIVATE_KEY" > ~/.gnupg/private.key
          gpg --import ~/.gnupg/private.key
        else
          echo ":warning: Skipped code signing for Linux AppImage, as gpg key was not present." >> $GITHUB_STEP_SUMMARY
        fi

        ./linuxdeploy-"$APPIMAGE_ARCH".AppImage --appdir ${{ env.INSTALL_APPIMAGE_DIR }} --output appimage --plugin qt -i ${{ env.INSTALL_APPIMAGE_DIR }}/usr/share/icons/hicolor/scalable/apps/org.prismlauncher.PrismLauncher.svg

        mv "PrismLauncher-Linux-$APPIMAGE_ARCH.AppImage" "PrismLauncher-Linux-${{ env.VERSION }}-${{ inputs.build-type }}-$APPIMAGE_ARCH.AppImage"

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

        CMAKE_PRESET: ${{ inputs.cmake-preset }}

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

        for l in $(find ${{ env.INSTALL_PORTABLE_DIR }} -type f); 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@v4
      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@v4
      with:
        name: PrismLauncher-${{ runner.os }}-${{ inputs.version }}-${{ inputs.build-type }}-${{ env.APPIMAGE_ARCH }}.AppImage
        path: PrismLauncher-${{ runner.os }}-${{ inputs.version }}-${{ inputs.build-type }}-${{ env.APPIMAGE_ARCH }}.AppImage

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