From 832ecb6d1f2cf3a532a67140b754e4f9264d79be Mon Sep 17 00:00:00 2001 From: southerntofu Date: Sun, 13 Sep 2020 22:51:19 +0200 Subject: [PATCH] New setup.sh script --- git-build.sh => forgebuild.sh | 0 setup.sh | 70 +++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) rename git-build.sh => forgebuild.sh (100%) create mode 100755 setup.sh diff --git a/git-build.sh b/forgebuild.sh similarity index 100% rename from git-build.sh rename to forgebuild.sh diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..fbbae58 --- /dev/null +++ b/setup.sh @@ -0,0 +1,70 @@ +#! /bin/bash + +# Make sure the setup script is running from this directory +ORIGDIR="$(pwd)" +cd "$(dirname "$0")" + +# Ensure the spec submodule was cloned +if [ ! -d spec ]; then + echo "git submodule was not initialized before. Doing it now" + git submodule init + if ! git submodule update; then + echo "Failed to download git submodule. Aborting setup!" + exit 1 + fi +fi + +# Copy the translations to ~/.local/share/forgebuild +FORGEBUILD_DATA=$HOME/.local/share/forgebuild +if [ ! -d $FORGEBUILD_DATA ]; then + if ! mkdir -p $FORGEBUILD_DATA; then + echo "Failed to create folder $FORGEBUILD_DATA to store translations" + exit 2 + fi + if ! cp -r spec/i18n $FORGEBUILD_DATA; then + echo "Failed to copy translations to $FORGEBUILD_DATA" + exit 3 + fi +fi + +I18N_FOLDER=$FORGEBUILD_DATA/i18n + +# Check if forgebuild already exists in $PATH +FORGEBUILD_BIN="$(command -v forgebuild)" +if [ $? -eq 0 ]; then + echo "forgebuild is already setup on this system. Performing an update." + owner="$(ls -ld $FORGEBUILD_BIN | awk '{print $3}')" + # Check ownership to ensure we can overwrite the file + if [[ $owner = "$USER" ]]; then + cp forgebuild.sh $FORGEBUILD_BIN || exit 3 + echo "Successfully updated forgebuild" + else + # File is not ours, we need to escalate privileges + if ! sudo cp forgebuild.sh $FORGEBUILD_BIN; then + echo "Failed to escalate privileges to setup forgebuild. Are you sure sudo is configured properly?" + exit 4 + fi + echo "Successfully updated forgebuild" + fi +else + echo "forgebuild was not setup before. Installing it in the first writable folder in \$PATH." + SETUP=0 + while read -d ':' -r dir; do + if [ -w $dir ]; then + cp forgebuild.sh $dir/forgebuild + echo "Successfully installed forgebuild to $dir" + SETUP=1 + break + fi + done <<< $PATH + if [ $SETUP -eq 0 ]; then + echo "No writable folder in \$PATH. Installing to /usr/local/bin (using sudo)" + if ! sudo cp forgebuild.sh /usr/local/bin/forgebuild; then + echo "Failed to escalate privileges to setup forgebuild. Are you sure sudo is configured properly?" + exit 5 + fi + fi +fi + +# Return to the folder the script was called from +cd "$ORIGDIR"