#!/bin/bash

case "$(uname)" in
Darwin)
    sudo rm -rf /System/Library/Extensions/AppleGraphicsControl.kext
    ;;
Linux)
    vm="MacOSX"
    efigopmode="4"
    efigopmode=""

    read width height depth <<<$( xwininfo -root|awk '/^ *Width:/{printf "%s ",$2} /^ *Height:/{printf "%s ",$2} /^ *Depth:/{printf "%s\n",$2}' )

    case "${width}x${depth}" in
    640×480)   efigopmode="0" ;;
    800×600)   efigopmode="1" ;;
    1024×768)  efigopmode="2" ;;
    1280×1024) efigopmode="3" ;;
    1440×900)  efigopmode="4" ;;
    *)         true ;;
    esac

    VBoxManage modifyvm "${vm}" --firmware efi64

    VBoxManage setextradata "${vm}" "GUI/LastGuestSizeHint"        "${width},${height}"
    VBoxManage setextradata "${vm}" "GUI/LastNormalWindowPosition" "0,0,${width},${height}"
    VBoxManage setextradata "${vm}" "GUI/LastScaleWindowPosition"  "0,0,${width},${height}"

    if [ -z "${efigopmode}" ] ; then
        VBoxManage setextradata "${vm}" "CustomVideoMode1"             "${width}x${height}x${depth}"
        VBoxManage setextradata "${vm}" "VBoxInternal2/EfiGopMode"
        VBoxManage setextradata "${vm}" "VBoxInternal2/SmcDeviceKey"
    else
        VBoxManage setextradata "${vm}" "CustomVideoMode1"
        VBoxManage setextradata "${vm}" "VBoxInternal2/EfiGopMode"     "${efigopmode}"
        VBoxManage setextradata "${vm}" "VBoxInternal2/SmcDeviceKey"   "ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc"
    fi

    for key in "CustomVideoMode1" "GUI/LastGuestSizeHint" "GUI/LastNormalWindowPosition" "GUI/LastScaleWindowPosition" ; do
        VBoxManage setextradata global "${key}"
    done

    VBoxManage getextradata "${vm}" enumerate

    case "${efigopmode}" in
    0) wh="640×480"   ;;
    1) wh="800×600"   ;;
    2) wh="1024×768"  ;;
    3) wh="1280×1024" ;;
    4) wh="1440×900"  ;;
    *) wh="${width}x${height}" ;;
    esac

    echo "To put in /Extra/com.apple.Boot.plist  inside the guest:"
    echo "<key>Graphics Mode</key><string>${wh}x${depth}</string>"
    echo "<key>GraphicsEnabler</key><string>Yes</string>"

    ;;
*)
    echo "Where am I?"
    exit 1
    ;;
esac

exit 0



# You’ll notice that the resolution is fixed to 1024×768. You can change
# this resolution, but it has to be done in two files. First on Mac OS
# X, there’s the /Extra/com.apple.Boot.plist file. This file describes
# the resolution for the Mac to use. Before you edit this file, install
# Xcode from the Mac OS X media (insert it into the drive with the Mac
# running). Open ‘Optional Installs’ folder and double-click ‘Xcode’ and
# follow the installation instructions. Note, it takes about 2.3GB of
# space. Plist files are easier to edit when the Property List Editor is
# installed from Xcode. Otherwise, you will have to hand edit these
# files with TextEdit.
#
# Also note that the /Extra folder is at the root of the Hard Drive
# volume.  It is not inside your local user profile folder.  You will
# also note that after a default install, Mac OS X doesn’t show hard
# disk icons on the desktop.  To turn this icon on, from the top menu,
# select ‘Finder->Preferences…’.  Then, put a check next to ‘Hard Disks’
# under ‘Show these items on the desktop’.  Once you open the Hard Disk
# icon, you should see the Extra folder.  If you still don’t see the
# Extra folder, be sure that you have installed MyHack or used the
# Nawcom ModCD.  If MyHack hasn’t been installed, there won’t be an
# /Extra folder there.  The Extra folder gets installed as a result of
# installing MyHack.
#
# Also, there can only be one available resolution in Virtualbox and on
# the Mac at a given time. I wish it supported more resolutions at once,
# but it doesn’t. Note also that because VirtualBox doesn’t support Mac
# OS X fully, there are no machine additions. To enable the resolution,
# on the PC side of VirtualBox (on the VirtualBox host), you will need
# to run the following command from a command shell (cmd):
#
# VBoxManage setextradata "Mac OS X" "CustomVideoMode1" "1920x1080x32"
#
# The “Mac OS X’ label is the exact machine name in VirtualBox. The
# “1920x1080x32″ setting is customizable to your needs (and video card
# capabilities). However, both this setting and com.apple.boot.plist (on
# the Mac) must match for the screen resolution to take effect.
#
# The ‘VBoxManage’ command is located in the Program
# Files\Oracle\VirtualBox folder or wherever you installed
# VirtualBox. You can add this location to your PATH variable so you can
# use this command without typing in the full path each time.
#
# In the com.apple.Boot.plist file, you will need to add the following
# properties (if not already there):
#
# Graphics Mode - String - 1920x1080x32
# GraphicsEnabler - String - y
#
# (The resolution value should match the above VboxManage command). If
# you want to change resolutions later, you will need to edit both
# places again and reboot your Mac.
#
# If you reboot and the screen hasn’t changed or has changed to
# 1280×1024 and not the resolution you expected, double check that both
# com.apple.boot.plist and the CustomVideoMode1 setting match and are
# active. Both of these settings must match for the resolution to work.
ViewGit