#!/bin/bash
#
## Update BaseImage for use as WK

set -o errexit
set -o errtrace
set -o nounset
set -o pipefail

# Prep
echo "Initializing..."
BASE_IMAGE="$1"
IMAGE_DEV="$(hdiutil attach -mountpoint /Volumes/BaseSystem "${BASE_IMAGE}" -nobrowse | grep -Eo '(/dev/disk[0-9]+)\b')"
OS_NAME="$(ls /Volumes/BaseSystem | grep -E 'OS X|macOS' | sed -E 's/Install (OS X|macOS) (.*)\.app/\2/')"
OS_VERSION="$(defaults read /Volumes/BaseSystem/System/Library/CoreServices/SystemVersion ProductVersion)"
OUT_NAME="${HOME}/Desktop/WK ${OS_NAME} ($(date +"%Y-%m-%d"))"
TIMEZONE="$(fgrep LINUX_TIME_ZONE ../../scripts/wk/cfg/main.py | cut -d '=' -f 2 | sed "s/'//g")"
WK_PATH="/Volumes/1201_UFD"

# Convert to a 4 GB R/W image
echo "Creating read-write copy of the ${OS_NAME} Base System image..."
hdiutil create -srcdevice "${IMAGE_DEV}s1" -format UDSB "${OUT_NAME}.sparsebundle"
hdiutil detach "${IMAGE_DEV}"
hdiutil resize -size 4g "${OUT_NAME}.sparsebundle"
WK_IMAGE_DEV="$(hdiutil attach "${OUT_NAME}.sparsebundle" -nobrowse | grep -Eo '(/dev/disk[0-9]+)\b')"
if ! diskutil rename "OS X Base System" "1201_UFD"; then
  diskutil rename "macOS Base System" "1201_UFD"
fi

# Remove Install app
echo "Removing Install app..."
rm -R "${WK_PATH}"/Install*.app

# Update Utilities menu
echo "Updating Utilities menu..."
if [[ "${OS_VERSION:0:5}" == "10.11" ]]; then
  cp -a "El Capitan/Utilities.plist" "${WK_PATH}/System/Installation/CDIS/OS X Utilities.app/Contents/Resources/Utilities.plist"
elif [[ "${OS_VERSION:0:5}" == "10.13" ]]; then
  cp -a "High Sierra/Utilities.plist" "${WK_PATH}/System/Installation/CDIS/macOS Utilities.app/Contents/Resources/Utilities.plist"
elif [[ "${OS_VERSION:0:5}" == "10.15" ]]; then
  cp -a "Catalina/Utilities.plist" "${WK_PATH}/System/Installation/CDIS/macOS Utilities.app/Contents/Resources/Utilities.plist"
fi

# Dropbear (SSH)
echo "Installing Dropbear..."
mkdir -p "${WK_PATH}/etc/dropbear"
for t in rsa dss ecdsa ed25519; do
  dropbearkey -t "${t}" -f "${WK_PATH}/etc/dropbear/dropbear_${t}_host_key"
done
ln -s /Volumes/RAM_Disk/.ssh "${WK_PATH}/var/root"/
cp -a ../linux/authorized_keys "${WK_PATH}/.authorized_keys"

# Homebrew
echo "Installing Hombrew..."
mkdir -p "${WK_PATH}/usr/local/bin"
ln -s python3 "${WK_PATH}/usr/local/bin/python"
rsync -aS /opt/ "${WK_PATH}/opt"/
rsync -aS --exclude='.git*' /usr/local/{Cellar,Frameworks,Homebrew,bin,etc,include,lib,opt,sbin,share,var} "${WK_PATH}/usr/local"/
if [[ "${OS_VERSION:3:2}" -lt "15" ]]; then
  rsync -aS --exclude='*.git' /usr/local/libexec "${WK_PATH}/usr/local"/
fi

# Fonts
echo "Installing Fonts..."
cp -a /Library/Fonts/Inconsolata*ttf "${WK_PATH}/System/Library/Fonts"/

# pipes-sh
echo "Installing pipes.sh..."
cp -a /usr/bin/tput "${WK_PATH}/usr/bin"/

# Terminal
echo "Installing Terminal Preferences..."
rsync -aS com.apple.Terminal.plist "${WK_PATH}/var/root/Library/Preferences"/

# Timezones
echo "Installing timezones..."
if [[ "${OS_VERSION:3:2}" == "11" ]]; then
  rsync -aS /usr/share/zoneinfo/ "${WK_PATH}/usr/share/zoneinfo"/
  ln -sf "/usr/share/zoneinfo/${TIMEZONE}" "${WK_PATH}/etc/localtime"
elif [[ "${OS_VERSION:3:2}" -gt "11" ]]; then
  sudo mkdir -p "${WK_PATH}/var/db/timezone/zoneinfo"
  sudo rsync -aS /var/db/timezone/zoneinfo/ "${WK_PATH}/var/db/timezone/zoneinfo"/
  ln -sf "/var/db/timezone/zoneinfo/${TIMEZONE}" "${WK_PATH}/etc/localtime"
fi

# zsh
echo "Installing zsh..."
cp -a /bin/zsh "${WK_PATH}/bin"/
cp -a zshenv "${WK_PATH}/etc"/
rsync -aS /usr/lib/zsh "${WK_PATH}/usr/lib"/
rsync -aS /usr/share/zsh "${WK_PATH}/usr/share"/
rsync -aS /usr/local/share/zsh "${WK_PATH}/usr/local/share"/
sed -Ei '' 's!^(root.*)/bin/sh!\1/bin/zsh!' "${WK_PATH}/etc/passwd"

# Misc
cp -a ../linux/known_networks "${WK_PATH}/.known_networks"
cp -a /usr/bin/rsync "${WK_PATH}/usr/bin"/
cp -a /usr/sbin/networksetup "${WK_PATH}/usr/sbin"/
cp -a aliases "${WK_PATH}/.aliases"
cp -a bashrc "${WK_PATH}/etc/profile"
cp -a vimrc "${WK_PATH}/.vimrc"
if [[ "${OS_VERSION:3:2}" == "11" ]]; then
  cp -a /usr/bin/fgrep "${WK_PATH}/usr/bin"/
  cp -a /usr/bin/locale "${WK_PATH}/usr/bin"/
  rsync -aS /usr/share/locale/ "${WK_PATH}/usr/share/locale"/
elif [[ "${OS_VERSION:3:2}" == "13" ]]; then
  cp -a /usr/lib/libexpat* "${WK_PATH}/usr/lib"/
fi

# WizardKit
echo "Installing WizardKit env..."
touch "${WK_PATH}/.wk-live-macos"
ln -s /Volumes/RAM_Disk/Logs "${WK_PATH}/var/root/Logs"
cp -a ../linux/profile_base/airootfs/etc/skel/.tmux.conf "${WK_PATH}/etc/tmux.conf"
rsync -aS /usr/bin/{env,killall} "${WK_PATH}/usr/bin"/
rsync -aS live-macos-startup ../../scripts/ "${WK_PATH}/usr/local/bin"/
if [[ -f "../../images/macOS-${OS_VERSION:0:5}.png" ]]; then
  cp -a "../../images/macOS-${OS_VERSION:0:5}.png" "${WK_PATH}/usr/local/wallpaper.png"
else
  cp -a ../../images/macOS.png "${WK_PATH}/usr/local/wallpaper.png"
fi

# Unmount sparsebundle
hdiutil detach "${WK_IMAGE_DEV}"

# Convert to compressed read-only image
echo "Converting to read-only image..."
hdiutil convert -format UDZO -o "${OUT_NAME}.dmg" "${OUT_NAME}.sparsebundle"
