#!/bin/bash

# Joshua V. Dillon
# jvdillon (a) gmail (.) com
# http://almostsure.com/mba42/post-install-oneiric.sh
#
# Install script to get my MacBookAir4,2 (13") up and running.  Every effort
# has been made to make this work for the MacBookAir4,1 (11") as well.
# (However, since I do not own the MBA41, I can only make guesses.)
#
# A General Comment:
#   This script is meant to be a "living howto".  You will see "if false"
#   statements throughout--these are so you can see how to do something
#   without actually doing.  Everyone should read through each step of this
#   script and decide if the action makes sense.  Although...I know most won't.
#   But don't cry to me when it doesn't do what you wanted/ecxpected.
#
# Changelog:
#
# Thu Aug  4 18:20:57 EDT 2011 - initial version
# Fri Aug  5 10:28:39 EDT 2011 - cleaned up a tad
# Sun Aug  7 00:34:10 EDT 2011 - added debhelper/btusb-dkms (koadman)
# Tue Sep  6 20:45:42 EDT 2011 - added more things to support oneiric
# Sat Sep 10 10:10:45 EDT 2011 - forked from post-install.sh
# Sun Sep 11 09:39:53 EDT 2011 - improved method of display calibration
# Sun Sep 11 22:39:46 EDT 2011 - updated xorg.conf; added dispad
# Wed Sep 14 01:14:25 EDT 2011 - new patch for hid-apple
# Wed Sep 14 14:34:10 EDT 2011 - got pommed working, only to find that it doesn't
# Thu Sep 15 09:58:26 EDT 2011 - prepped all patches for 11" and eventual submission
# Mon Sep 19 00:53:49 EDT 2011 - code to build touchegg
# Wed Sep 21 16:27:53 EDT 2011 - bcm5974 build bugfix (ingineer)
# Thu Sep 22 09:42:20 EDT 2011 - commented out keyillum keybindings (ChinaSailor)
# Sat Sep 24 12:30:07 EDT 2011 - dispad dependency and autostart dir (poliva)
# Tue Sep 27 12:38:45 EDT 2011 - uncommented copy hack (kiffykroker)
# Thu Oct 13 09:38:01 PDT 2011 - disabled firmware install as Oneiric now does this (jonny)
# Sat Oct 15 11:50:47 EDT 2011 - stupid if then error mike909
# Sun Dec  4 13:30:33 PST 2011 - patches from poliva (thanks!):
#     01_accept-lowercase-choice.patch	01-Dec-2011 11:34	608	 
#     02_add-extra-power-management-features.patch	01-Dec-2011 11:46	600	 
#     03_only-rebuild-bcm5974-in-mba41.patch	01-Dec-2011 11:46	425	 
#     04_disable-mactel-ppa.patch	01-Dec-2011 11:46	766	 
#     05_use-mtrack-and-dispad-oneiric-packages.patch	01-Dec-2011 11:44	1.1K	 
#     06_fix-dispad-dependancy.patch	01-Dec-2011 11:45	363	 
#     07_new-macfanctld-version.patch	01-Dec-2011 13:29	456	 
#     08_fix-mba41-warning.patch	01-Dec-2011 11:46	1.1K	 
#     09_fix_wrong_date_format.patch	02-Dec-2011 15:43	2.8K	 
#     10_fix-post-hibernate-bug.patch	02-Dec-2011 18:18	1.2K	 
#     11_choose-between-mtrack-or-touchegg.patch	02-Dec-2011 19:43	3.3K	 
# Sat Dec 17 09:12:58 IST 2011 - joel's grub-line delimiter fix

#
# Copyright (c) 2012 Joshua V Dillon,
# http://almostsure.com/mba42/post-install-oneiric.sh
# All rights reserved. See end-of-file for license.


# --- Verify Device ---------------------------------------------

echo "Verifying system hardware."

#SysPrdNam=$(echo $(sudo dmidecode -s system-product-name))
SysPrdNam=$(cat /sys/class/dmi/id/product_name)
UBU=$(lsb_release -r|cut -f2)

if [ "MacBookAir4,1" = "${SysPrdNam}" ] || [ "MacBookAir4,2" = "${SysPrdNam}" ]; then
	echo "Good; You seem to have a 2011 MacBook Air."
else
	echo "I don't know how to configure a \"${SysPrdNam}\". This script is for the 2011 MacBook Air."
	read -n 1 -r -p 'To continue anyway, press Y. To quit, press any other key. ' choice
	echo
	case "$choice" in
		[yY]) ;;
		*) exit 1;;
	esac
fi

if [[ "11.10" = "${UBU}" ]];then
	echo "Good; you seem to be running Ubuntu Oneirc."
else
	echo "It seems you have a \"${UBU}\" and not Ubuntu Oneiric 11.10."
	read  -n 1 -r -p 'To continue anyway, press Y. To quit, press any other key. ' choice
	echo
	case "$choice" in
		[Yy]) ;;
		*) exit 2;;
	esac
fi

# we will work out of mba4-tmp for the rest of the script.  this is better than
# /tmp because we may not want to re-download things
[ -e mba4-tmp ] || mkdir -p mba4-tmp ; cd mba4-tmp



# --- Build Dependencies ----------------------------------------

# aptitude is better
[ -z "$(which aptitude)" ] && sudo apt-get install aptitude
sudo aptitude install build-essential debhelper git 



# --- Patching Drivers ------------------------------------------

echo "Patching kernel."

# --- hid-apple
if true; then
	echo "Building hid-apple (keyboard driver)."
	sudo apt-get build-dep --no-install-recommends linux-image-$(uname -r)
	apt-get source linux-image-$(uname -r)
	wget -Nq http://www.almostsure.com/mba42/hid-apple.patch
	cd linux-$(uname -r|cut -d- -f1)/drivers/hid
	patch -p2 <../../../hid-apple.patch
	make -C /lib/modules/$(uname -r)/build M=$(pwd) hid-apple.ko
	sudo make -C /lib/modules/$(uname -r)/build M=$(pwd) modules_install
	# the following command may be necessary:
	sudo cp /lib/modules/$(uname -r)/extra/hid-apple.ko \
		/lib/modules/$(uname -r)/kernel/drivers/hid/hid-apple.ko
	cd ../../../
fi

# --- bcm5974
if [[ "MacBookAir4,1" = "${SysPrdNam}" ]]; then
	# (only really needed by MBA 11" since MBA 13" patch is already in)
	echo "Building bcm5974 (trackpad driver)."
	sudo apt-get build-dep --no-install-recommends linux-image-$(uname -r)
	apt-get source linux-image-$(uname -r)
	wget -Nq http://www.almostsure.com/mba42/bcm5974.patch
	cd linux-$(uname -r|cut -d- -f1)/drivers/input/mouse
	patch -p3 <../../../../bcm5974.patch
	make -C /lib/modules/$(uname -r)/build M=$(pwd) bcm5974.ko
	sudo make -C /lib/modules/$(uname -r)/build M=$(pwd) modules_install
	# the following command may be necessary:
	sudo cp /lib/modules/$(uname -r)/extra/bcm5974.ko \
		/lib/modules/$(uname -r)/kernel/drivers/input/mouse/bcm5974.ko
	cd ../../../../
fi

# --- btusb
if true; then
	echo "Building btusb (bluetooth driver)"
	sudo apt-get build-dep --no-install-recommends linux-image-$(uname -r)
	apt-get source linux-image-$(uname -r)
	wget -Nq http://www.almostsure.com/mba42/btusb.patch
	cd linux-$(uname -r|cut -d- -f1)/drivers/bluetooth
	patch -p2 <../../../btusb.patch
	make -C /lib/modules/$(uname -r)/build M=$(pwd) btusb.ko
	sudo make -C /lib/modules/$(uname -r)/build M=$(pwd) modules_install
	# the following command may be necessary:
	sudo cp /lib/modules/$(uname -r)/extra/btusb.ko \
		/lib/modules/$(uname -r)/kernel/drivers/bluetooth/btusb.ko
	cd ../../../
fi

# --- i915
echo "Patching i915 video driver with temporary hack."
wget -Nq http://www.almostsure.com/mba42/fix-i915.sh 
bash fix-i915.sh

modinfo bcm5974   |head -1
modinfo btusb     |head -1
modinfo hid-apple |head -1
modinfo i915      |head -1
sudo depmod -a
sudo update-initramfs -u

tee <<-EOF
	I am done updating the kernel.  Do want me to continue to configure your
	system?  If you have already configured it once, don't do it again.
	To continue, press Y. To quit, press any other key.
EOF
read  -n 1 -r choice
echo
case "$choice" in
	[Yy]) ;;
	*) 
		exit 2;;
esac

tee <<-EOF
	Please select which touchpad driver do you want to use:
	1. mtrack + dispad (if unsure, select this option)
	2. synaptics + touchegg
EOF
echo
while [ "$touchpad" != "1" ] && [ "$touchpad" != "2" ]; do
	read -n 1 -p "Enter '1' or '2': " touchpad
	echo
done


# --- Mactel ----------------------------------------------------

echo "Installing packages."
sudo aptitude install gnome-color-manager icc-profiles-free lm-sensors ppa-purge xcalib

# commented out as package is not available for oneiric and we're using mtrack later
if false; then
	# Let's get what we can from Mactel and update the rest later.
	echo "Installing Mactel drivers and software."
	sudo add-apt-repository ppa:mactel-support/ppa
	sudo aptitude update -q2
	sudo aptitude install xf86-input-multitouch
fi
# bcm5974-dkms					kernel up-to-date (for 13")
# hid-apple-dkms				kernel up-to-date (for 13")
# xserver-xorg-input-mtrack		manual (not in mactel)
# btusb-dkms 					manual (need to patch)
# applesmc-dkms					kernel up-to-date
# xf86-input-multitouch			unecessary if using mtrack (instal anyway)
# hid-dkms						unecessary
# snd-hda-dkms					unecessary
# mbp-nvidia-bl-dkms			unecessary
# macfanctld					needed, but no oneiric build
# pommed						TODO

# --- mtrack
if false; then
	echo "Building xf86-input-mtrack (trackpad driver)."
	sudo aptitude install autoconf libtool libmtdev-dev xserver-xorg-dev xutils-dev
	git clone https://github.com/BlueDragonX/xf86-input-mtrack.git
	cd xf86-input-mtrack/
	# http://ubuntuforums.org/showpost.php?p=10881280&postcount=105
	# libtoolize && aclocal && autoconf && automake --add-missing --copy
	autoreconf --install --force
	# should install to: /usr/lib/xorg/modules/input/
	./configure --prefix=/usr
	dpkg-buildpackage
	#sudo aptitude install checkinstall fakeroot
	#fakeroot checkinstall -yD --pkgname xf86-input-mtrack --pkgversion 2.0.0 make install && mv *.deb ..
	cd ..
else
	echo "Downloading mtrack (xorg multitouch driver)."
	wget -Nq http://www.dev.fatedmariner.org/packages/xf86-input-mtrack/ubuntu/xserver-xorg-input-mtrack_0.2.0_oneiric_amd64.deb
fi

# --- dispad
if false; then
	echo "Building dispad (touchpad pause daemon)."
	sudo aptitude install autoconf libtool libconfuse-common libconfuse0 \
		libx11-dev libconfuse-dev libxi-dev xserver-xorg-dev 
	git clone https://github.com/BlueDragonX/dispad.git
	cd dispad
	autoreconf --install --force
	./configure #--prefix=/usr
	dpkg-buildpackage
	cd ..
else
	echo "Downloading dispad (touchpad pause daemon)."
	wget -Nq http://www.dev.fatedmariner.org/packages/dispad/ubuntu/dispad_0.1_oneiric_amd64.deb
	# fix dispad dependancy
	sudo aptitude install libconfuse0
fi

# --- touchegg
if false; then
	echo "Building touchegg (touchpad daemon)."
	# http://code.google.com/p/touchegg/wiki/CompileSourceCode
	wget http://touchegg.googlecode.com/files/touchegg-1.0.tar.gz
	tar xzvf touchegg-1.0.tar.gz
	cd touchegg-1.0
	sudo aptitude install build-essential checkinstall fakeroot libqt4-dev utouch libx11-6 libxtst-dev
	qmake
	make -j2
	fakeroot checkinstall -yD make install
	mv touchegg_1.0-1_amd64.deb ..
	cd ..
	# mkdir $HOME/.touchegg
	# cp /usr/share/touchegg/touchegg.conf $HOME/.touchegg/touchegg.conf
	# to use, run with:  "while :;do touchegg;done"
else
	echo "Installing touchegg (touchpad daemon)."
	sudo aptitude install touchegg
fi

# --- macfanctld
# It is highly recommended to use the fan controller daemon that is included in
# the mactel-support ppa called macfanctl. However, that PPA does not currently
# have a build of macfanctl for Natty. After enabling the ppa as shown above
# you can add this tool with the following command: 
if false; then
	echo "Building macfanctld (fan control daemon)."
	sudo aptitude install bzr fakeroot
	bzr branch lp:macfanctld
	cd macfanctld
#	# apparently the most recent version of macfanctld isn't in bazaar.  assuming
#	# the 0.5 changelog is accurate then this should get it up to date:
#	sed -i "s/^excluded:/exclude:/" macfanctl.conf
#	tee >debian/changelog2 <<-EOF
#	macfanctld (0.5~mactel1~all) maverick; urgency=low
#	
#	  * Corrected comment in macfanctl.conf
#	
#	 -- Mikael Strom <mikael@sesamiq.com>  Wed, 17 Nov 2010 18:31:36 +0800
#	EOF
	dpkg-buildpackage -b -rfakeroot -us -uc
	cd ..
else
	echo "Downloading macfanctld (fan control daemon)."
	wget -Nq https://launchpad.net/~mactel-support/+archive/ppa/+files/macfanctld_0.6~mactel1_amd64.deb
fi

if false; then
	#http://blog.technologeek.org/category/hacks/pommed
	sudo apt-get build-dep pommed
	#sudo aptitude install \
	#	libasound2-dev libaudiofile-dev libconfuse-dev libdbus-1-dev libpci-dev \
	#	libgtk2.0-dev libdbus-glib-1-dev libx11-dev libxext-dev libxpm-dev
	apt-get source pommed
	wget -Nq http://www.almostsure.com/mba42/pommed.patch
	cd pommed-*
	patch -p1 <../pommed.patch
	dpkg-buildpackage
#else
#	# do nothing now because pommed doesn't work out-of-the box
#	sudo aptitude install pommed 
fi

# --- Install.
echo "Installing packages: macfanctld xserver-xorg-input-mtrack dispad." #pommed."
sudo dpkg -i xserver-xorg-input-mtrack_*.deb dispad_*.deb macfanctld_*.deb #pommed_*.deb


# --- Modules & Sensors -----------------------------------------

echo "Configuring modules, dispad, temperature sensors, and macfanctld (fan control daemon)."
# https://help.ubuntu.com/community/MacBookPro8-1/Natty

mkdir -p ~/.config/autostart

# --- mtrack / xorg
if [ $touchpad == 1 ]; then
	echo "Updating video configuration."
	sudo tee -a /etc/X11/xorg.conf <<-EOF
		# mtrack
		Section "InputClass"
		    Identifier       "Multitouch Touchpad"
		    Driver           "mtrack"
		    MatchDevicePath  "/dev/input/event*"
		    MatchIsTouchpad  "on"
		    Option           "CorePointer"     "true"
		    Option           "Sensitivity"     "0.65"  #    1 : movement speed
		    Option           "ScrollDistance"  "100"   #  150 : two-finger drag dist for click
		    Option           "ClickTime"       "25"    #   50 : millisec to hold emulated click
		#   Option           "ThumbSize"       "35"    #   25 : min size of thumb
		#   Option           "PalmSize"        "55"    #   40 : min size of palm
		EndSection # "Multitouch Touchpad"
	EOF

	echo "Making dispad run at login (touchpad pause daemon)."
	tee ~/.config/autostart/dispad.desktop <<-EOF
		[Desktop Entry]
		Type=Application
		Exec=/usr/bin/dispad
		Hidden=false
		NoDisplay=false
		X-GNOME-Autostart-enabled=true
		Name[en_US]=dispad
		Name=dispad
		Comment[en_US]=Disable trackpad while typing.
		Comment=Disable trackpad while typing.
	EOF

elif [ $touchpad == 2 ]; then
	echo "Making touchegg run at login (touchpad gestures)."
	tee ~/.config/autostart/touchegg.desktop <<-EOF
		[Desktop Entry]
		Type=Application
		Exec=/usr/bin/touchegg
		Hidden=false
		NoDisplay=false
		X-GNOME-Autostart-enabled=true
		Name[en_US]=Touchegg
		Name=Touchegg
		Comment[en_US]=Load touchegg daemon.
		Comment=Load touchegg daemon.
	EOF

	if false; then
		sudo tee -a /etc/X11/xorg.conf <<-EOF
			# touchegg
			Section "InputClass"
			   Identifier       "touchpad catchall"
			   Driver           "evdev"
			   MatchIsTouchpad  "on"
			   MatchDevicePath  "/dev/input/event*"
			   #MatchUSBID      "05ac:024c"
			EndSection # "touchpad catchall"
		EOF
	fi

else
	echo "ERROR: Unrecgnized mtrack/touchegg choice."
	read -n 1 -p "Press any key to continue..."
fi

# --- xmodmap
echo "Making xmodmap run at login (custom keys)."
tee ~/.config/autostart/xmodmap.desktop <<-EOF
	[Desktop Entry]
	Type=Application
	Exec=/usr/bin/xmodmap ~/.Xmodmap
	Hidden=false
	NoDisplay=false
	X-GNOME-Autostart-enabled=true
	Name[en_US]=Xmodmap
	Name=Xmodmap
	Comment[en_US]=Load custom .Xmodmap.
	Comment=Load custom .Xmodmap.
EOF

echo "Making xmodmap run after resume (custom keys)."
wget -Nq http://almostsure.com/mba42/00_usercustom
sed -i "s/xxxxxxxx/$USER/" 00_usercustom
chmod 0755 00_usercustom
sudo mv 00_usercustom /etc/pm/sleep.d/00_usercustom

# --- FaceTime
if false; then
	# References:
	# [1] http://handyfloss.net/2008.07/making-isight-camera-work-in-ubuntu/
	# [2] http://en.gentoo-wiki.com/wiki/Apple_Macbook_Air#Webcam
	[ -e /media/Mac\ OS ] || sudo mkdir /media/Mac\ OS
	sudo mount -o ro /dev/sda2 /media/Mac\ OS
	#cp /media/Mac\ OS/System/Library/Extensions/IOUSBFamily.kext/Contents/PlugIns/AppleUSBVideoSupport.kext/Contents/MacOS/AppleUSBVideoSupport .
	sudo aptitude install isight-firmware-tools cheese
	#ift-extract --apple-driver AppleUSBVideoSupport
	sudo umount /media/Mac\ OS
	if [ -f /lib/firmware/isight.fw ]; then
		sudo modprobe uvcvideo
		if dmesg|tail -5|grep -q FaceTime; then
			echo "successfully extracted firmware"
		else
			echo "ERROR: doesn't appear that firmware was properly extracted"
			read -n 1 -p "Press any key to continue..."
		fi
	else
		echo "ERROR: unable to extract FaceTime firmware."
		read -n 1 -p "Press any key to continue..."
	fi
fi

# The program lmsensors detects the sensors, however it does not know what they
# are yet. The module coretemp will allow lm-sensor to detect the others
# sensors, the rotation speed of the fan, and the GPU temperature.
sudo tee -a /etc/modules <<-EOF
	coretemp
	hid_apple
EOF
# make function keys behave normally and fn+ required for macro
sudo tee -a /etc/modprobe.d/hid_apple.conf <<-EOF
	options hid_apple fnmode=2
EOF
sudo modprobe coretemp hid_apple

# configure macfanctld
tee <<-EOF
	Configuring macfanctld to ignore some sensors. On my system three
	sensors gave bogus readings, i.e.,
	    TH0F: +249.2 C                                    
	    TH0J: +249.0 C                                    
	    TH0O: +249.0 C
	Run 'sensors' to see current values; run 'macfanctld -f' to
	obtain the list of sensors and their associated ID.
	Applying this exclude: 13 14 15.
EOF
#cd /sys/devices/platform/applesmc.768;for l in *_label;do printf "%s\t%s\n" $l `cat $l`;done|sort -n
sudo service macfanctld stop
sudo cp /etc/macfanctl.conf /etc/macfanctl.conf.$(date +%Y-%m-%d)
sudo sed -i "s/\(^exclude:\).*\$/\\1 13 14 15/" /etc/macfanctl.conf
sudo service macfanctld start

# Fix Matlab error:
sudo ln -s /lib/x86_64-linux-gnu/libc-2.13.so /lib64/libc.so.6

# --- keyboard backlight
# no longer needed--unity does its job now
[ -d ~/bin ] || mkdir ~/bin
wget -Nq -O- http://almostsure.com/mba42/keyboard-backlight >~/bin/keyboard-backlight

if true; then
	echo "Installing MacBook automatic keyboard brightness daemon."
	sudo add-apt-repository ppa:poliva/lightum-mba
	sudo aptitude update -q2
	sudo aptitude install lightum
fi



# --- Suspend ---------------------------------------------------

echo "Fixing post-hibernate hang."
sudo tee -a /etc/pm/config.d/macbookair_fix <<-EOF
	# for more details, see: /usr/lib/pm-utils/defaults
	
	## bcm5974 doesn't suspend nicely
	#SUSPEND_MODULES="bcm5974 usbhid"

	# The following brings back eth0 after suspend when using the apple usb-ethernet adapter.
	SUSPEND_MODULES="asix usbnet"

	#HIBERNATE_RESUME_POST_VIDEO="yes"
	#ADD_PARAMETERS="--quirk-reset-brightness"
	#DROP_PARAMETERS="--quirk-none"
EOF

# no password after resume (like mac)
gsettings set org.gnome.desktop.lockdown disable-lock-screen 'true'


# --- Boot ------------------------------------------------------

echo "Setting boot parm (better power usage)."
sudo cp /etc/default/grub /etc/default/grub.$(date +%Y-%m-%d)
SWAP=$(cat /etc/fstab |grep "# swap was on" |awk '{print $5}')
sudo sed -i "s:\(GRUB_CMDLINE_LINUX_DEFAULT=\).*\$:\\1\"quiet splash i915.i915_enable_rc6=1 resume=${SWAP}\":" /etc/default/grub
sudo update-grub


echo "Adding trim to ext4 mounts (assuming you use ext4)."
# http://sites.google.com/site/lightrush/random-1/howtoconfigureext4toenabletrimforssdsonubuntu
sudo cp /etc/fstab /etc/fstab.$(date +%Y-%m-%d)
sudo sed -i '/\W\/\W/s/errors/discard,errors/g' /etc/fstab

echo "Ensuring bcm5974 loads before usbhid (editing /etc/rc.local)."
# update /etc/rc.local to ensure bcm5974 is loaded BEFORE usbhid
sudo cp /etc/rc.local /etc/rc.local.$(date +%Y-%m-%d)
sudo sed -i '$i modprobe -r usbhid\nmodprobe -a bcm5974 usbhid' /etc/rc.local

# set-up different key configurations
if true; then
	echo "Installing modified key mapping (Note: ~/.Xmodmap needs tweaking!)."
	wget -Nq http://www.almostsure.com/mba42/dotXmodmap 
	[ -e ~/.Xmodmap ] && cp ~/.Xmodmap ~/.Xmodmap.bak-$(date +%Y-%m-%d)
	mv dotXmodmap ~/.Xmodmap
	#sudo sed -i "\$i xmodmap /home/$USER/.Xmodmap" /etc/gdm/Init/Default
	xmodmap ~/.Xmodmap
fi



# --- Update Audio ----------------------------------------------

echo "Updating audio configuration (Note: this step may not be necessary)."
sudo tee -a /etc/modprobe.d/alsa-base.conf <<-EOF
	#options snd_hda_intel model=intel-mac-auto
	options snd-hda-intel model=mba32
EOF


 
# --- Extra Power Management ------------------------------------

echo "Configuring extra power management options."
wget -Nq http://www.almostsure.com/mba42/99_macbookair
#wget -Nq http://pof.eslack.org/archives/files/mba42/99_macbookair
chmod 0755 99_macbookair
sudo mv 99_macbookair /etc/pm/power.d/99_macbookair
# disable bluetooth by default
sudo sed -i '$i /usr/sbin/rfkill block bluetooth' /etc/rc.local



# --- Calibrate Display -----------------------------------------

echo "Calibrating display (based on OS X configuration)."
sudo mkdir /etc/xcalib/
[ -e /media/Mac\ OS ] || sudo mkdir /media/Mac\ OS
sudo mount -o ro /dev/sda2 /media/Mac\ OS
#sudo cp /media/Mac\ OS/Library/ColorSync/Profiles/Displays/Color\ LCD-00000610-0000-9CDF-0000-0000042737C0.icc \
#	/etc/xcalib/colorprofile.icc
#sudo umount /media/Mac\ OS
#sudo cp /etc/gdm/Init/Default /etc/gdm/Init/Default.$(date +%Y-%m-%d)
#sudo sed -i '$i/usr/bin/xcalib /etc/xcalib/colorprofile.icc' /etc/gdm/Init/Default
#xcalib /etc/xcalib/colorprofile.icc
sudo mkdir -p /usr/share/color/icc
sudo cp /media/Mac\ OS/Library/ColorSync/Profiles/Displays/Color\ LCD-00000610-0000-9C??-0000-000004273??0.icc \
	/usr/share/color/icc/Apple_MacBookAir4.icc
sudo umount /media/Mac\ OS
gcm-import /usr/share/color/icc/Apple_MacBookAir4.icc



# --- Avoid Long EFI Wait Before GRUB ---------------------------

tee <<-EOF
	If your Macbook spends 30 seconds with "white screen" before GRUB
	shows, try booting into Mac OS X, open a terminal and enter:
		sudo bless --device /dev/disk0s4 --setBoot --legacy --verbose
	where /dev/disk0s4 is your linux boot partition. If you are
	unsure which partition to use, enter:
	    diskutils list
EOF

tee <<-EOF
	If you haven't done so already, it may be wise to use the Lion Recovery
	Disk Assistant [1] to make a USB restore drive. It should be run from
	MacOS.
	[1] http://support.apple.com/kb/DL1433
EOF

exit 0

#sudo aptitude install v86d hwinfo
#echo "options uvesafb mode_option=1440x900-24 mtrr=3 scroll=ywrap" | sudo tee -a /etc/modprobe.d/uvesafb.conf
#echo "blacklist uvesafb" | sudo tee -a /etc/modprobe.d/blacklist-framebuffer
#echo "options uvesafb mode_option=1440x900-24 mtrr=3 scroll=ywrap" | sudo tee -a /etc/initramfs-tools/modules
#echo FRAMEBUFFER=y | sudo tee /etc/initramfs-tools/conf.d/splash
#sudo update-initramfs -u

#
# Copyright (c) 2012 Joshua V Dillon,
# http://almostsure.com/mba42/post-install-oneiric.sh
# All rights reserved. See end-of-file for license.
#
#  Redistribution and use in source and binary forms, with or
#  without modification, are permitted provided that the
#  following conditions are met:
#   * Redistributions of source code must retain the above
#     copyright notice, this list of conditions and the
#     following disclaimer.
#   * Redistributions in binary form must reproduce the above
#     copyright notice, this list of conditions and the
#     following disclaimer in the documentation and/or other
#     materials provided with the distribution.
#   * Neither the name of the author nor the names of its
#     contributors may be used to endorse or promote products
#     derived from this software without specific prior written
#     permission.
#  
#  THIS SOFTWARE IS PROVIDED BY JOSHUA V DILLON ''AS IS'' AND
#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
#  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JOSHUA
#  V DILLON BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
#  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


