app-id: io.github.foxxmd.multiscrobbler
runtime: org.freedesktop.Platform
runtime-version: '22.08'
sdk: org.freedesktop.Sdk
command: multiscrobbler
finish-args:
  - --share=network
  # used for access to MPRIS in order to scrobble
  - --own-name=org.mpris.MediaPlayer2.*
sdk-extensions:
  - org.freedesktop.Sdk.Extension.node18
modules:
  - name: multiscrobbler
    buildsystem: simple
    build-options:
      append-path: /usr/lib/sdk/node18/bin
      env:
        XDG_CACHE_HOME: /run/build/multiscrobbler/flatpak-node/cache
        npm_config_cache: /run/build/multiscrobbler/flatpak-node/npm-cache
        npm_config_nodedir: /usr/lib/sdk/node18
        npm_config_offline: 'true'
    build-commands:
      # install npm dependencies
      - npm ci --offline
      - cd docsite && npm ci --offline && cd ../

      # build app
      - NODE_ENV=production npm run build

      # makes node and npm available
      - mkdir -p /app/bin /app/lib /app/lib/dist /app/lib/src /app/lib/assets
      - cp -a /usr/lib/sdk/node18/bin/{node,npm} /app/bin
      - cp -a /usr/lib/sdk/node18/lib/* /app/lib
      - rm -r /app/lib/node_modules/npm/{docs,man} # remove this when updating to newer runtime-version

      # remove dev dependencies
      - npm prune --production
      # remove docsite dependencies since we've built static site
      - rm -r docsite/node_modules

      # copy node_modules needed to run app
      - cp -r node_modules/. /app/lib/node_modules

      # even if not using package.json for scripts it must be present
      # so that tsx/node runs as esm (needs to see "type": "module" in file)
      - cp package.json /app/lib/package.json

      # copy app files to runtime dir
      - cp -r dist/. /app/lib/dist
      - cp -r src/. /app/lib/src
      - cp -r docsite/. /app/lib/docsite
      - cp -r config /app/lib/config
      - cp -r assets /app/lib/assets
      - cp -r public /app/lib/public
      - cp vite.config.ts /app/lib/vite.config.ts
      - cp tsconfig.json /app/lib/tsconfig.json
      - cp tailwind.config.js /app/lib/tailwind.config.js
      - cp postcss.config.js /app/lib/postcss.config.js
      - cp flatpak/defaultConfig.json /app/lib/config/config.json

      - install -Dm644 flatpak/${FLATPAK_ID}.desktop /app/share/applications/${FLATPAK_ID}.desktop
      - install -Dm644 flatpak/icon.png /app/share/icons/hicolor/512x512/apps/${FLATPAK_ID}.png
      - install -Dm644 assets/icon.svg /app/share/icons/hicolor/scalable/apps/${FLATPAK_ID}.svg
      - install -Dm644 flatpak/${FLATPAK_ID}.metainfo.xml /app/share/metainfo/${FLATPAK_ID}.metainfo.xml

      - install -Dm755 ../run.sh /app/bin/multiscrobbler
    subdir: main
    sources:
      # use for official releases
#      - type: git
#        url: https://github.com/FoxxMD/multi-scrobbler
#        tag: 0.6.3
#        commit: 4bd996eb466137ffc5c3f48305afabde03720525
#        dest: main

      # use if developing locally
#      - type: dir
#        path: /home/yourUser/multi-scrobbler
#        dest: main
      # Wrapper to launch the app
      - type: script
        dest-filename: run.sh
        commands:
          # Run config check and copy examples + default config (disable file logging)
          - |
            echo -e "Checking CONFIG_DIR for existing configs: ${XDG_CONFIG_HOME}"
            if [ "$(ls -A ${XDG_CONFIG_HOME})" ]; then
              echo "Directory is not empty! Will not add default config/examples"
            else
              echo "Directory is empty! Adding default config/examples"
              cp -r /app/lib/config/. "${XDG_CONFIG_HOME}"/
              #cp /app/lib/defaultConfig.json $XDG_CONFIG_HOME/config.json
            fi
          - cd /app/lib && CONFIG_DIR=$XDG_CONFIG_HOME LOG_DIR=$XDG_CONFIG_HOME IS_LOCAL=true NODE_ENV=production node_modules/.bin/tsx src/backend/index.ts

      - generated-sources.0.json
      - generated-sources.1.json
