From 365328457e0ed2ca99dd14978a8cade900782b07 Mon Sep 17 00:00:00 2001
From: Googol Lee <i@googol.im>
Date: Tue, 9 Jul 2024 12:34:55 +0200
Subject: [PATCH] Building scripts for apt/homebrew platforms. (#993)

---
 Dockerfile                                    | 42 +++++++------------
 .../{ => apt}/install_build_dependencies.sh   |  0
 scripts/apt/install_runtime_dependencies.sh   | 23 ++++++++++
 .../set_compiler_env.sh}                      | 33 ++++-----------
 .../homebrew/install_build_dependencies.sh    |  4 ++
 .../homebrew/install_runtime_dependencies.sh  |  4 ++
 scripts/homebrew/set_compiler_env.sh          |  5 +++
 7 files changed, 60 insertions(+), 51 deletions(-)
 rename scripts/{ => apt}/install_build_dependencies.sh (100%)
 create mode 100755 scripts/apt/install_runtime_dependencies.sh
 rename scripts/{set_go_env.sh => apt/set_compiler_env.sh} (73%)
 create mode 100755 scripts/homebrew/install_build_dependencies.sh
 create mode 100755 scripts/homebrew/install_runtime_dependencies.sh
 create mode 100755 scripts/homebrew/set_compiler_env.sh

diff --git a/Dockerfile b/Dockerfile
index affc430..b8ecc60 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -24,8 +24,8 @@ ENV COMMIT_SHA=${COMMIT_SHA:-}
 ENV REACT_APP_BUILD_COMMIT_SHA=${COMMIT_SHA:-}
 
 # Download dependencies
-COPY ui /app
-WORKDIR /app
+COPY ui /app/ui
+WORKDIR /app/ui
 RUN npm ci --omit=dev --ignore-scripts \
   # Build frontend
   && npm run build -- --base=$UI_PUBLIC_URL
@@ -37,18 +37,18 @@ ARG TARGETPLATFORM
 # See for details: https://github.com/hadolint/hadolint/wiki/DL4006
 SHELL ["/bin/bash", "-o", "pipefail", "-c"]
 
-COPY scripts /tmp/scripts
-COPY api /app
-WORKDIR /app
+COPY scripts/apt /app/scripts/apt
+COPY api /app/api
+WORKDIR /app/api
 
 ENV GOPATH="/go"
 ENV PATH="${GOPATH}/bin:${PATH}"
 ENV CGO_ENABLED=1
 
 # Download dependencies
-RUN chmod +x /tmp/scripts/*.sh \
-  && /tmp/scripts/install_build_dependencies.sh \
-  && source /tmp/scripts/set_go_env.sh \
+RUN chmod +x /app/scripts/apt/*.sh \
+  && source /app/scripts/apt/set_compiler_env.sh \
+  && /app/scripts/apt/install_build_dependencies.sh \
   && go env \
   && go mod download \
   # Patch go-face
@@ -61,37 +61,25 @@ RUN chmod +x /tmp/scripts/*.sh \
   && go build -v -o photoview .
 
 ### Copy api and ui to production environment ###
-FROM debian:bookworm-slim
+FROM --platform=${BUILDPLATFORM:-linux/amd64} debian:bookworm-slim AS final
 ARG TARGETPLATFORM
 
 # See for details: https://github.com/hadolint/hadolint/wiki/DL4006
 SHELL ["/bin/bash", "-o", "pipefail", "-c"]
 
+COPY scripts/apt/install_runtime_dependencies.sh /app/scripts/apt/
+
 # Create a user to run Photoview server
 RUN groupadd -g 999 photoview \
   && useradd -r -u 999 -g photoview -m photoview \
   # Required dependencies
-  && apt-get update \
-  && apt-get install -y curl gnupg gpg libdlib19.1 ffmpeg exiftool libheif1 sqlite3 \
-  # Install Darktable if building for a supported architecture
-  && if [ "${TARGETPLATFORM}" = "linux/amd64" ] || [ "${TARGETPLATFORM}" = "linux/arm64" ]; then \
-    echo 'deb https://download.opensuse.org/repositories/graphics:/darktable/Debian_12/ /' \
-      | tee /etc/apt/sources.list.d/graphics:darktable.list; \
-    curl -fsSL https://download.opensuse.org/repositories/graphics:/darktable/Debian_12/Release.key \
-      | gpg --dearmor | tee /etc/apt/trusted.gpg.d/graphics_darktable.gpg > /dev/null; \
-    apt-get update; \
-    apt-get install -y darktable; \
-  fi \
-  # Remove build dependencies and cleanup
-  && apt-get purge -y gnupg gpg \
-  && apt-get autoremove -y \
-  && apt-get clean \
-  && rm -rf /var/lib/apt/lists/*
+  && chmod +x /app/scripts/apt/*.sh \
+  && /app/scripts/apt/install_runtime_dependencies.sh
 
 WORKDIR /home/photoview
 COPY api/data /app/data
-COPY --from=ui /app/dist /app/ui
-COPY --from=api /app/photoview /app/photoview
+COPY --from=ui /app/ui/dist /app/ui
+COPY --from=api /app/api/photoview /app/photoview
 
 ENV PHOTOVIEW_LISTEN_IP=127.0.0.1
 ENV PHOTOVIEW_LISTEN_PORT=80
diff --git a/scripts/install_build_dependencies.sh b/scripts/apt/install_build_dependencies.sh
similarity index 100%
rename from scripts/install_build_dependencies.sh
rename to scripts/apt/install_build_dependencies.sh
diff --git a/scripts/apt/install_runtime_dependencies.sh b/scripts/apt/install_runtime_dependencies.sh
new file mode 100755
index 0000000..f40c622
--- /dev/null
+++ b/scripts/apt/install_runtime_dependencies.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+BUILD_DEPENDS=(gnupg2 gpg)
+
+apt-get update
+apt-get install -y ${BUILD_DEPENDS[@]} curl libdlib19.1 ffmpeg exiftool libheif1
+
+# Install Darktable if building for a supported architecture
+if [ "${TARGETPLATFORM}" = "linux/amd64" ] || [ "${TARGETPLATFORM}" = "linux/arm64" ]; then
+  echo 'deb https://download.opensuse.org/repositories/graphics:/darktable/Debian_12/ /' \
+    | tee /etc/apt/sources.list.d/graphics:darktable.list
+  curl -fsSL https://download.opensuse.org/repositories/graphics:/darktable/Debian_12/Release.key \
+    | gpg --dearmor | tee /etc/apt/trusted.gpg.d/graphics_darktable.gpg > /dev/null
+
+  apt-get update
+  apt-get install -y darktable
+fi
+
+# Remove build dependencies and cleanup
+apt-get purge -y ${BUILD_DEPENDS[@]}
+apt-get autoremove -y
+apt-get clean
+rm -rf /var/lib/apt/lists/*
diff --git a/scripts/set_go_env.sh b/scripts/apt/set_compiler_env.sh
similarity index 73%
rename from scripts/set_go_env.sh
rename to scripts/apt/set_compiler_env.sh
index 155eb06..a7744e6 100755
--- a/scripts/set_go_env.sh
+++ b/scripts/apt/set_compiler_env.sh
@@ -7,34 +7,19 @@
 : ${TARGETOS=}
 : ${TARGETARCH=}
 : ${TARGETVARIANT=}
-: ${CGO_ENABLED=}
-: ${GOARCH=}
-: ${GOOS=}
-: ${GOARM=}
-: ${GOBIN=}
+
+CGO_ENABLED="$(go env CGO_ENABLED)"
+GOARCH="$(go env GOARCH)"
+GOOS="$(go env GOOS)"
+GOARM="$(go env GOARM)"
+GOBIN="$(go env GOBIN)"
 
 set -eu
 
 if [ ! -z "$TARGETPLATFORM" ]; then
-  os="$(echo $TARGETPLATFORM | cut -d"/" -f1)"
-  arch="$(echo $TARGETPLATFORM | cut -d"/" -f2)"
-  if [ ! -z "$os" ] && [ ! -z "$arch" ]; then
-    export GOOS="$os"
-    export GOARCH="$arch"
-    if [ "$arch" = "arm" ]; then
-      case "$(echo $TARGETPLATFORM | cut -d"/" -f3)" in
-      "v5")
-        export GOARM="5"
-        ;;
-      "v6")
-        export GOARM="6"
-        ;;
-      *)
-        export GOARM="7"
-        ;;
-      esac
-    fi
-  fi
+  TARGETOS="$(echo $TARGETPLATFORM | cut -d"/" -f1)"
+  TARGETARCH="$(echo $TARGETPLATFORM | cut -d"/" -f2)"
+  TARGETVARIANT="$(echo $TARGETPLATFORM | cut -d"/" -f3)"
 fi
 
 if [ ! -z "$TARGETOS" ]; then
diff --git a/scripts/homebrew/install_build_dependencies.sh b/scripts/homebrew/install_build_dependencies.sh
new file mode 100755
index 0000000..cf63350
--- /dev/null
+++ b/scripts/homebrew/install_build_dependencies.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+brew update
+brew install libheif dlib jpeg
diff --git a/scripts/homebrew/install_runtime_dependencies.sh b/scripts/homebrew/install_runtime_dependencies.sh
new file mode 100755
index 0000000..ed2c19c
--- /dev/null
+++ b/scripts/homebrew/install_runtime_dependencies.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+brew update
+brew install exiftool ffmpeg darktable
diff --git a/scripts/homebrew/set_compiler_env.sh b/scripts/homebrew/set_compiler_env.sh
new file mode 100755
index 0000000..98653b4
--- /dev/null
+++ b/scripts/homebrew/set_compiler_env.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+export CPLUS_INCLUDE_PATH="$(brew --prefix)/opt/jpeg/include:$(brew --prefix)/opt/dlib/include"
+export LD_LIBRARY_PATH="$(brew --prefix)/opt/jpeg/lib:$(brew --prefix)/opt/dlib/lib"
+export LIBRARY_PATH="$(brew --prefix)/opt/jpeg/lib:$(brew --prefix)/opt/dlib/lib"
