Skip to content

Instantly share code, notes, and snippets.

@durkinza
Forked from afgomez/dmginstall.sh
Last active May 20, 2024 09:57
Show Gist options
  • Save durkinza/ca7e1aa02bd4901c08c496d97d2e0daa to your computer and use it in GitHub Desktop.
Save durkinza/ca7e1aa02bd4901c08c496d97d2e0daa to your computer and use it in GitHub Desktop.
Download and install a .dmg
#!/bin/bash --login
# Downloads and installs a .dmg from a URL
#
# Usage
# $ dmginstall [url]
#
# For example, for installing wireshark version 3.0.2
# $ dmginstall https://1.na.dl.wireshark.org/osx/Wireshark%203.0.2%20Intel%2064.dmg
# or for firefox version 68.0
# $ dmginstall https://download-installer.cdn.mozilla.net/pub/firefox/releases/68.0/mac/en-US/Firefox%2068.0.dmg
# TODO
# - handle .zip files as well
VERBOSE=false
Failed=false
apps_folder='/Applications'
function usage() { echo "Usage: $0 [-v] [-h] <url_to_package>
-h This help menu
-v Verbose mode, show debugging info" 1>&2; exit 1; }
while getopts dpvh opts; do
case ${opts} in
v) VERBOSE=true ;;
h) usage ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
URL=$*
# some functions to use for output.
function Announce(){
Spacer='\n=======================================\n'
printf "\n$Spacer$1$Spacer"
}
function Say(){
Spacer='\n---------------------------------------\n'
printf "\n$Spacer$1$Spacer"
}
function Debug(){
if $VERBOSE; then
printf "\nDEBUG: $1\n"
fi
}
# Generate a random file name
tmp_file=/tmp/`openssl rand -base64 10 | tr -dc '[:alnum:]'`.dmg
# bootstrapping done, lets start intalling
# Download file
Announce "Downloading $url..."
curl -# -L -o $tmp_file $URL
Debug "Temporary file is $tmp_file"
Announce "Mounting image..."
# Mount the package and save disk and volume locations
app_location=$(hdiutil attach $tmp_file | tail -n 1 | tr -s "[:blank:]" "\n")
# Get disk of the mounted package. e.x. /dev/disk3s3
app_disk=$(echo $app_location | tr -s " " "\n" | head -n 1)
# Get the Volume of the package. e.x. /Volumes/Wireshark
if [ $(echo $app_location | tr -s " " "\n" | wc -l ) -gt 3 ]; then
# If the same disk is mounted multiple times, an additional number will be returned, we don't want that
app_volume=$(echo $app_location | tr -s " " "\n" | tail -n 2 | head -n 1)
else
# If this is the first mount of the image, just get the volume.
app_volume=$(echo $app_location | tr -s " " "\n" | tail -n 1)
fi
# Determine the file we want to install and get file type of that file
file_type=$(find $app_volume/ -regex '.*.[app|pkg]' -maxdepth 1 | head -n 1 | tr "." "\n" | tail -n 1)
# Some Debugging to make sure we're on the right track.
Debug "App Location: $app_location"
Debug "App Disk: $app_disk"
Debug "App Volume: $app_volume"
Debug "File Type: $file_type"
Announce "Installing"
if [ ! $file_type != 'pkg' ]; then
echo "Installing pkg..."
# pkg can be installed with mac's built in installer
/usr/sbin/installer -pkg $app_volume/*.pkg -target /
elif [ ! $file_type != 'app' ]; then
echo "Installing app..."
# app can be installed by copying directory over to applications directory
app=`find $app_volume/. -name *.app -maxdepth 1 -type d -print0`
cp -ir $app $apps_folder
else
echo "Installation failed."
echo "Error: $file_type is not supported."
Failed=true
fi
Announce "Cleaning up..."
Debug "un-mounting image"
hdiutil detach $app_disk
Debug "Removing temp file"
rm $tmp_file
echo "Done!"
# Wait until after un-mounting to throw error
if Failed; then
exit 1;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment