|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +#!/bin/bash |
| 4 | + |
| 5 | +## region ############################################## Destination |
| 6 | + |
| 7 | +# Allow setting of destination via prefix, verify that it's writable |
| 8 | +destDir="/usr/local/sbin" |
| 9 | +[ -n "$LOCAL_SBIN" ] && destDir="$LOCAL_SBIN" |
| 10 | +if [ ! -w "$destDir" ]; then |
| 11 | + |
| 12 | + # Exit with error if this was specified path |
| 13 | + [ -n "$LOCAL_SBIN" ] && exit 1 |
| 14 | + |
| 15 | + # Otherwise use an alternate path |
| 16 | + userDir=$(/usr/bin/dscl . -read /Users/"$USER" NFSHomeDirectory 2>/dev/null | /usr/bin/awk -F ': ' '{print $2}') |
| 17 | + if [ -z "$userDir" ] && [ -d "/Users/$USER/Desktop" ]; then |
| 18 | + userDir="/Users/$USER/Desktop" |
| 19 | + fi |
| 20 | + |
| 21 | + destDir="$userDir/.local/sbin" |
| 22 | +fi |
| 23 | + |
| 24 | +## endregion ########################################### End Destination |
| 25 | + |
| 26 | +## region ############################################## Main Code |
| 27 | + |
| 28 | +installed="" |
| 29 | +if [ -f "$destDir/user-defaults" ]; then |
| 30 | + installed="$("$destDir/user-defaults" --version | cut -d' ' -f2)" |
| 31 | + if [ "$1" == "--replace" ]; then |
| 32 | + installed="" |
| 33 | + rm "$destDir/user-defaults" || exit 1 |
| 34 | + fi |
| 35 | +fi |
| 36 | + |
| 37 | +repoUrl="https://github.com/jonesiscoding/user-defaults/releases/latest" |
| 38 | +effectiveUrl=$(curl -Ls -o /dev/null -I -w '%{url_effective}' "$repoUrl") |
| 39 | +tag=$(echo "$effectiveUrl" | /usr/bin/rev | /usr/bin/cut -d'/' -f1 | /usr/bin/rev) |
| 40 | +[ "$tag" == "releases" ] && tag="v1.0" |
| 41 | +if [ -n "$tag" ]; then |
| 42 | + # Exit successfully if same version |
| 43 | + [ "$tag" == "$installed" ] && exit 0 |
| 44 | + dlUrl="https://github.com/jonesiscoding/user-defaults/archive/refs/tags/${tag}.zip" |
| 45 | + repoFile=$(basename "$dlUrl") |
| 46 | + tmpDir="/private/tmp/user-defaults/${tag}" |
| 47 | + [ -d "$tmpDir" ] && rm -R "$tmpDir" |
| 48 | + if mkdir -p "$tmpDir"; then |
| 49 | + if curl -Ls -o "$tmpDir/$repoFile" "$dlUrl"; then |
| 50 | + cd "$tmpDir" || exit 1 |
| 51 | + if unzip -qq "$tmpDir/$repoFile"; then |
| 52 | + rm "$tmpDir/$repoFile" |
| 53 | + if cp "$tmpDir/user-defaults-${tag//v/}/src/user-defaults" "$destDir/"; then |
| 54 | + rm -R "$tmpDir" |
| 55 | + # Success - Exit Gracefully |
| 56 | + exit 0 |
| 57 | + fi |
| 58 | + fi |
| 59 | + fi |
| 60 | + fi |
| 61 | +fi |
| 62 | + |
| 63 | +# All Paths that lead here indicate we couldn't install |
| 64 | +exit 1 |
| 65 | + |
| 66 | +## endregion ########################################### End Main Code |
0 commit comments