Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save scottd3v/3903de684a5f9a6566afb04d803bc8fe to your computer and use it in GitHub Desktop.

Select an option

Save scottd3v/3903de684a5f9a6566afb04d803bc8fe to your computer and use it in GitHub Desktop.
# Test if Chrome installed
ChromeSearch=$(find "/Applications/" -name "Google Chrome.app" -depth 1)
if [ -n "$ChromeSearch" ]; then
echo "Chrome already present in /Applications folder"
exit 1
else
echo "Chrome not present in /Applications folder"
fi
# DMG Download URL
DownloadUrl="https://dl.google.com/dl/chrome/mac/universal/stable/gcea/googlechrome.dmg"
### Modify below this line at your own risk!
# Locate DMG Download Link From URL
regex='^https.*.dmg$'
if [[ $DownloadUrl =~ $regex ]]; then
echo "URL points to direct DMG download"
validLink="True"
else
echo "Searching headers for download links"
urlHead=$(curl -s --head $DownloadUrl)
locationSearch=$(echo "$urlHead" | grep https:)
if [ -n "$locationSearch" ]; then
locationRaw=$(echo "$locationSearch" | cut -d' ' -f2)
locationFormatted="$(echo "${locationRaw}" | tr -d '[:space:]')"
regex='^https.*'
if [[ $locationFormatted =~ $regex ]]; then
echo "Download link found"
DownloadUrl=$(echo "$locationFormatted")
else
echo "No https location download link found in headers"
exit 1
fi
else
echo "No location download link found in headers"
exit 1
fi
fi
#Create Temp Folder
DATE=$(date '+%Y-%m-%d-%H-%M-%S')
TempFolder="Download-$DATE"
mkdir /tmp/$TempFolder
# Navigate to Temp Folder
cd /tmp/$TempFolder
# Download File into Temp Folder
curl -s -O "$DownloadUrl"
# Capture name of Download File
DownloadFile="$(ls)"
echo "Downloaded $DownloadFile to /tmp/$TempFolder"
# Verifies DMG File
regex='\.dmg$'
if [[ $DownloadFile =~ $regex ]]; then
DMGFile="$(echo "$DownloadFile")"
echo "DMG File Found: $DMGFile"
else
echo "File: $DownloadFile is not a DMG"
rm -r /tmp/$TempFolder
echo "Deleted /tmp/$TempFolder"
exit 1
fi
# Mount DMG File -nobrowse prevents the volume from popping up in Finder
hdiutilAttach=$(hdiutil attach /tmp/$TempFolder/$DMGFile -nobrowse)
echo "Used hdiutil to mount $DMGFile "
err=$?
if [ ${err} -ne 0 ]; then
echo "Could not mount $DMGFile Error: ${err}"
rm -r /tmp/$TempFolder
echo "Deleted /tmp/$TempFolder"
exit 1
fi
regex='\/Volumes\/.*'
if [[ $hdiutilAttach =~ $regex ]]; then
DMGVolume="${BASH_REMATCH[@]}"
echo "Located DMG Volume: $DMGVolume"
else
echo "DMG Volume not found"
rm -r /tmp/$TempFolder
echo "Deleted /tmp/$TempFolder"
exit 1
fi
# Identify the mount point for the DMG file
DMGMountPoint="$(hdiutil info | grep "$DMGVolume" | awk '{ print $1 }')"
echo "Located DMG Mount Point: $DMGMountPoint"
# Capture name of App file
cd "$DMGVolume"
AppName="$(ls | Grep .app)"
cd ~
echo "Located App: $AppName"
# Test to ensure App is not already installed
ExistingSearch=$(find "/Applications/" -name "$AppName" -depth 1)
if [ -n "$ExistingSearch" ]; then
echo "$AppName already present in /Applications folder"
hdiutil detach $DMGMountPoint
echo "Used hdiutil to detach $DMGFile from $DMGMountPoint"
rm -r /tmp/$TempFolder
echo "Deleted /tmp/$TempFolder"
exit 1
else
echo "$AppName not present in /Applications folder"
fi
DMGAppPath=$(find "$DMGVolume" -name "*.app" -depth 1)
# Copy the contents of the DMG file to /Applications/
# Preserves all file attributes and ACLs
cp -pPR "$DMGAppPath" /Applications/
err=$?
if [ ${err} -ne 0 ]; then
echo "Could not copy $DMGAppPath Error: ${err}"
hdiutil detach $DMGMountPoint
echo "Used hdiutil to detach $DMGFile from $DMGMountPoint"
rm -r /tmp/$TempFolder
echo "Deleted /tmp/$TempFolder"
exit 1
fi
echo "Copied $DMGAppPath to /Applications"
# Unmount the DMG file
hdiutil detach $DMGMountPoint
echo "Used hdiutil to detach $DMGFile from $DMGMountPoint"
err=$?
if [ ${err} -ne 0 ]; then
abort "Could not detach DMG: $DMGMountPoint Error: ${err}"
fi
# Remove Temp Folder and download
rm -r /tmp/$TempFolder
echo "Deleted /tmp/$TempFolder"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment