FROM node:20-slim as prepare

RUN apt update && apt install -y git

# Taken from tauri docs https://beta.tauri.app/guides/prerequisites/#rust
RUN apt install libwebkit2gtk-4.1-dev -y \
  build-essential \
  curl \
  wget \
  file \
  libssl-dev \
  libayatana-appindicator3-dev \
  librsvg2-dev

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y


FROM prepare as build

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
ENV PATH="/root/.cargo/bin:${PATH}"

#RUN corepack prepare pnpm@latest --activate

RUN corepack enable

ADD src /lume/src
ADD public /lume/public

ADD index.html \ 
package.json \
postcss.config.js \
tailwind.config.js \
vite.config.ts \
pnpm-lock.yaml \
tsconfig.json \
/lume

ADD src-tauri/icons /lume/src-tauri/icons
ADD src-tauri/migrations /lume/src-tauri/migrations
ADD src-tauri/src /lume/src-tauri/src

ADD src-tauri/build.rs \ 
src-tauri/Cargo.lock \
src-tauri/Cargo.toml \
src-tauri/tauri.conf.json \
src-tauri/tauri.linux.conf.json \
/lume/src-tauri

WORKDIR /lume

RUN pnpm install --frozen-lockfile

# Path for disable updater
ADD flatpak/0001-disable-tauri-updater.patch .
RUN patch -p1 -t -i 0001-disable-tauri-updater.patch

# debian build
RUN pnpm tauri build -b deb

ARG VERSION=2.2.3
ARG ARCH=amd64

RUN cp -r ./src-tauri/target/release/bundle/deb/lume_${VERSION}_${ARCH}/data lume-package
 
FROM scratch as final

COPY --from=build lume/lume-package prepare-dist
#ADD flatpak/*.xml flatpak/*.desktop flatpak/*.yml prepare-dist
