#! /usr/bin/env bash

# Creates macOS boot disk.

printf "%s\n" "macOS Boot Disk Setup"
printf "%s\n" "  1. Insert a USB drive (8GB or higher)."
printf "%s\n" "  2. Use Disk Utility to format as \"Mac OS Extended (Journaled)\"."
printf "%s\n" "  3. Use Disk Utility to set the schema, if available, as \"GUID Partition Map\"."
printf "%s\n" "  4. Use Disk Utility to label as \"Untitled\"."
printf "%s\n" "  5. Run this script to install the OS and create a bootable USB drive."
printf "\n%s\n" "macOS Boot Disk Usage:"
printf "%s\n" "  1. Insert the USB drive, created above, into the machine to be upgraded."
printf "%s\n" "  2. Reboot the machine."
printf "%s\n" "  3. Press and hold the POWER key before the Apple logo appears."
printf "%s\n" "  4. Select the USB boot disk from the menu."
printf "%s\n" "  5. Use Disk Utility to delete and/or erase the hard drive including associated partitions."
printf "%s\n" "  6. Use Disk Utility to create a single \"APFS\" drive."
printf "%s\n" "  7. Install the new operating system."
printf "\n%s\n" "macOS Boot Disk Recovery:"
printf "%s\n" "  1. Start/restart the machine."
printf "%s\n" "  2. Press and hold the POWER key before the Apple logo appears."
printf "%s\n" "  3. Wait for the macOS installer to load from the recovery partition."
printf "%s\n" "  4. Use the dialog options to launch Disk Utility, reinstall the system, etc."

printf "\n%s\n" "Creating macOS boot disk..."

if [[ ! -e "$MAC_OS_BOOT_DISK_CREATOR" ]]; then
  printf "%s\n" "ERROR: macOS installer does not exist: $MAC_OS_BOOT_DISK_CREATOR. Use System Preferences → Software Update to download."
  exit 1
fi

if [[ ! -d "$MAC_OS_BOOT_DISK_PATH" ]]; then
  printf "%s\n" "ERROR: Boot disk must be mounted at: $MAC_OS_BOOT_DISK_PATH."
  exit 1
fi

sudo "$MAC_OS_BOOT_DISK_CREATOR" --volume "$MAC_OS_BOOT_DISK_PATH" --downloadassets --nointeraction
