From 40ff77eee2836b222ebd488b30b1d06b685940ee Mon Sep 17 00:00:00 2001
From: Nina Blanson <nina.m.blanson@gmail.com>
Date: Thu, 13 Jul 2023 06:02:08 -0500
Subject: [PATCH] Add workaround for #3102 until cross-compile work complete
 (#3472)

* Add workaround for #3102 until cross-compile work complete

* Minor cleanup

---------

Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
---
 docker/docker-compose.yml | 17 +++++++++-----
 docker/docker_update.sh   | 49 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 59 insertions(+), 7 deletions(-)

diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml
index 33a2d84e..3a68ea13 100644
--- a/docker/docker-compose.yml
+++ b/docker/docker-compose.yml
@@ -24,9 +24,12 @@ services:
     logging: *default-logging
 
   lemmy:
+    # use "image" to pull down an already compiled lemmy. make sure to comment out "build".
     # image: dessalines/lemmy:0.18.1
-    # use this to build your local lemmy server image for development
-    # run docker compose up --build
+    # platform: linux/x86_64 # no arm64 support. uncomment platform if using m1.
+    # use "build" to build your local lemmy server image for development. make sure to comment out "image".
+    # run: docker compose up --build
+
     build:
       context: ../
       dockerfile: docker/Dockerfile
@@ -51,12 +54,14 @@ services:
     logging: *default-logging
 
   lemmy-ui:
+    # use "image" to pull down an already compiled lemmy-ui. make sure to comment out "build".
     image: dessalines/lemmy-ui:0.18.1
-    # use this to build your local lemmy ui image for development
-    # run docker compose up --build
-    # assuming lemmy-ui is cloned besides lemmy directory
+    # platform: linux/x86_64 # no arm64 support. uncomment platform if using m1.
+    # use "build" to build your local lemmy ui image for development. make sure to comment out "image".
+    # run: docker compose up --build
+
     # build:
-    #   context: ../../lemmy-ui
+    #   context: ../../lemmy-ui # assuming lemmy-ui is cloned besides lemmy directory
     #   dockerfile: dev.dockerfile
     environment:
       # this needs to match the hostname defined in the lemmy service
diff --git a/docker/docker_update.sh b/docker/docker_update.sh
index f2d7fa43..d64025cc 100755
--- a/docker/docker_update.sh
+++ b/docker/docker_update.sh
@@ -1,6 +1,53 @@
 #!/bin/sh
 set -e
 
+Help()
+{
+  # Display help
+  echo "Usage: ./docker_update.sh [OPTIONS]"
+  echo ""
+  echo "Start all docker containers required to run Lemmy."
+  echo ""
+  echo "Options:"
+  echo "-u Docker username. Only required if managing Docker via Docker Desktop with a personal access token."
+  echo "-h Print this help."
+}
+
+while getopts ":hu:" option; do
+  case $option in
+    h) Help
+       exit;;
+    u) DOCKER_USER=$OPTARG
+       ;;
+    *) echo "Invalid option $OPTARG."
+       exit;;
+  esac
+done
+
+LOG_PREFIX="[🐀 lemmy]"
+ARCH=$(uname -m 2>/dev/null || echo 'unknown') # uname may not exist on windows machines; default to unknown to be safe.
+
 mkdir -p volumes/pictrs
+
+echo "$LOG_PREFIX Please provide your password to change ownership of the pictrs volume."
 sudo chown -R 991:991 volumes/pictrs
-sudo docker compose up -d --build
+
+if [ "$ARCH" = 'arm64' ]; then
+  echo "$LOG_PREFIX WARN: If building from images, make sure to uncomment 'platform' in the docker-compose.yml file!"
+
+  # You need a Docker account to pull images. Otherwise, you will get an error like: "error getting credentials"
+  if [ -z "$DOCKER_USER" ]; then
+      echo "$LOG_PREFIX Logging into Docker Hub..."
+      docker login
+  else
+      echo "$LOG_PREFIX Logging into Docker Hub. Please provide your personal access token."
+      docker login --username="$DOCKER_USER"
+  fi
+
+  echo "$LOG_PREFIX Initializing images in the background. Please be patient if compiling from source..."
+  docker compose up -d --build
+else
+  sudo docker compose up -d --build
+fi
+
+echo "$LOG_PREFIX Complete! You can now access the UI at http://localhost:1236."
-- 
2.44.1