From ff7bf31822c3a248d74f80a848dcd2345eb5d505 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 29 May 2025 19:31:41 +1200 Subject: [PATCH] :-'Generic Commit' --- .../install_dropshell_build_host.sh | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/dropshell-build/install_dropshell_build_host.sh b/dropshell-build/install_dropshell_build_host.sh index a97c92a..9946ee2 100755 --- a/dropshell-build/install_dropshell_build_host.sh +++ b/dropshell-build/install_dropshell_build_host.sh @@ -4,6 +4,21 @@ set -euo pipefail SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +# If the user is not root, use sudo +SUDOCMD="" +[ "$EUID" -eq 0 ] || SUDOCMD="sudo" + +# If the user is not root, use the home directory of the user who ran the script +USER_HOME="$HOME" +if [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then + USER_HOME=$(eval echo "~${SUDO_USER}") +fi + +# test sudo is working or we're root (return non-zero if not root!) +if ! "$SUDOCMD" true; then + echo "Error: This script must be run as root or with sudo privileges." >&2 + exit 1 +fi _die() { echo -e "Error: $1" >&2 @@ -82,7 +97,7 @@ function install_packages() { HAVE_UPDATED=1 fi - if ! sudo apt-get install -y "$pkg"; then + if ! "$SUDOCMD" apt-get install -y "$pkg"; then print_error "Failed to install $pkg" exit 1 fi @@ -102,7 +117,7 @@ function install_headers() { cd "libassert/build" || exit 1 cmake .. -DCMAKE_BUILD_TYPE=Release make -j - sudo make install + "$SUDOCMD" make install cd ../.. rm -rf libassert fi @@ -259,11 +274,6 @@ function installmusl() { echo "Installing musl toolchain" # Set install directory - if [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then - USER_HOME=$(eval echo "~${SUDO_USER}") - else - USER_HOME="$HOME" - fi INSTALL_DIR="$USER_HOME/.musl-cross" mkdir -p "$INSTALL_DIR"