#! /bin/bash
#virtualbox-iphone-7.sh
#by huanix@gmail.com; http://www.huanix.com
#released 2008-11-11
#thanks to remainder http://virtualbox.org/ticket/49

#8-18 check for running as sudo user. If run correctly as "sudo ./script",these will exit cleanly.
  if [ "$UID" != "0" ]; then
    echo "This script must be run as root, try:  sudo ./virtualbox-iphone-r##.sh" 1>&2
    exit 1
    fi

  if [ "$SUDO_USER" == "" ]; then
    echo "Since you are running this script in a root shell, and not via 'sudo', this" 1>&2
    echo "script cannot verify that your user id has the proper group membership.  You" 1>&2
    echo "will need to do that on your own." 1>&2
    fi

#20- User agreement
  clear
  echo "This script will modify your linux kernel so that iTunes running in VirtualBox" 1>&2
  echo "will recognize Pod devices (iPhone, iPod, iPod touch). This is a native" 1>&2
  echo "recognition and works at the same speed as if you were actually running windows" 1>&2
  echo "by itself." 1>&2
  echo "" 1>&2
  echo "There is no charge for this script, but a monetary encouragement would keep me" 1>&2
  echo "going a while longer - even a few dollars would make my day (really!) you can" 1>&2
  echo "share by paypal at huanix@gmail.com. Thanks to tauchris@gmail.com for edits!" 1>&2
  echo ""
  echo ""
  echo "Are you reading this? You must type 'yes' to continue,  or Ctrl-C to exit." 1>&2
  read acknowledge
	if [ "$acknowledge" == "yes" ]; then
		
		dpkg -l | grep virtualbox-ose
		if [ "$?" == '0' ]; then
			clear
			echo "it looks like you're using the open source version of virtualbox. This version 
is not compatible with USB. If you choose 'y', we will remove it and proceed to 
install the non-opensource edition of Virtualbox. Is that okay? (y or ctrl-c)"
			read installvbox
				if [ "$installvbox" == 'y' ]; then
					sudo apt-get remove -y virtualbox-ose;

				else
					clear
					echo "you did not choose to remove the old version and will be returned to the shell prompt."
					exit;

				fi
		fi
				
#checking to see if virtualbox-2.0 is installed	
dpkg -l | grep virtualbox-2.0 
  if [ "$?" == '1' ]; then
	clear
	echo "it looks like virtualbox-2.0 is not installed. Can i do that now? (y or Ctrl-C)"
	read virtualbox2;
 		 if [ "$virtualbox2" == "y" ]; then
		
			#checking for the source in sources.list with a grep
			grep download.virtualbox.org/virtualbox/debian /etc/apt/sources.list 
			if [ "$?" == '1' ]; then
			sudo echo "
			#source for virtualbox (nonfree)
			deb http://download.virtualbox.org/virtualbox/debian intrepid non-free" >> /etc/apt/sources.list
			wget -q http://download.virtualbox.org/virtualbox/debian/sun_vbox.asc -O- | sudo apt-key add - 
			sudo apt-get update
			sudo apt-get install virtualbox-2.0;

		else
			sudo apt-get update
			sudo apt-get install virtualbox-2.0;

		fi
	
  else
	echo ""
	echo "You chose not to install virtualbox."
	exit 1;
 
  fi
fi
#removed a fi, not sure if it was right!

################



        grep '/proc/bus/usb' /etc/fstab > /dev/null
		if [ "$?" == '0' ]; then
			clear	
			echo ' 
It looks like your /etc/fstab already includes a mount point for usb; i wont 
mess with it!';
		else
			echo ' 
It looks like you dont have a mount point for USB in your /etc/fstab,
can I add one? (y/n)';

			read k
				if [ "$k" == "y" ]; then

					VBOXGRP=`grep -E '^vboxusers:' /etc/group`
					if [ "$VBOXGRP" == "" ]; then
					    echo "No 'vboxusers' group found.  Is VirtualBox installed??";
					    exit 1;
					fi
					if [ "$SUDO_USER" != "" ]; then
					    FOUND=0
					    for i in `echo $VBOXGRP | awk -F ':' '{print $4}' | sed -e 's/,/ /g'`; do
						if [ "$i" == "$SUDO_USER" ]; then
						    FOUND=1
						    break;
						fi
					    done
					    if [ "$FOUND" == "0" ]; then
						echo "User '$SUDO_USER' is not in group 'vboxusers'.  Can I add you to that group now? (y/n)"
						read k
						if [ "$k" == "y" ]; then
						    usermod -a -G vboxusers $SUDO_USER
						    echo "Added you to 'vboxusers'.  If there are other users that need USB in VirtualBox machines, you must add them to the vboxusers group as well.  Use this command for each user:"
						    echo " > sudo usermod -a -G vboxusers [username]"
						    echo ""
						else
						    echo "You must add yourself (and any other users that need USB in VirtualBox machines) to the vboxusers group before USB will work.  Use this command for each user:"
						    echo " > sudo usermod -a -G vboxusers [username]"
						    echo ""
						fi
					    fi
					fi

					VBOXGID=`echo $VBOXGRP | awk -F ':' '{print $3}'`

					echo "none /proc/bus/usb usbfs devgid=$VBOXGID,devmode=664 0 0" >> /etc/fstab 
					echo "if you think this caused a problem, look at the last line of the /etc/fstab file."
		
fi
fi
else 
clear
echo "You did not type 'yes' to continue this script. Please re-run the" 1>&2
echo "script when you are ready. I promise to try not to break anything." 1>&2
exit 1;
	fi


echo "
I'm going to download the kernel source and try to patch the usb kernel so it
will work for the iPhone, iPod Touch and iPods. This will take awhile!
 Are you sure you want to do it? (y/n)"
	read p
	if [ "$p" = "y" ]; then

KERNEL=`uname -r | awk -F '-' '{print $1}'`
sudo apt-get -y build-dep linux-source-$KERNEL
sudo apt-get -y install linux-source-$KERNEL build-essential
tar -jxvf /usr/src/linux-source-$KERNEL.tar.bz2
cd linux-source-$KERNEL/drivers/usb/core
perl -pi.bak -e 's/(define\s+MAX_USBFS_BUFFER_SIZE\s+)16384/${1}131072/' devio.c
make -C /lib/modules/`uname -r`/build/ M=`pwd` modules
strip --strip-debug usbcore.ko
sudo install -m644 -b usbcore.ko /lib/modules/`uname -r`/kernel/drivers/usb/core
sudo depmod -ae
sudo update-initramfs -u

echo 'Do you want to get rid of the source and installed files?'
read r
	if [ "$r" = "y" ]; then
#tentative adds:
sudo apt-get -y remove linux-source-$KERNEL build-essential
sudo apt-get -y autoremove
# PLEASE do not panic about the following line. it will remove the kernel source that was just downloaded and is no longer necessary. ALWAYS be cautious when a script tries to rm -rf!!
#doesn't remove the folder ... not sure why. tried several variants.
#sudo rm -rvf linux-source-$KERNEL/
fi


echo "

I'd like to go ahead and run /etc/init.d/vboxdrv setup. Is that okay? (y/n) "
read j
if [ "$j" = "y" ]; then
sudo /etc/init.d/vboxdrv setup
fi
echo "



Okay, after you reboot i'll be out of your life. If you appreciate my time, 
share \$2 by paypal at huanix@gmail.com

This script will be refined until an official fix is released. You should 
download and run this each time your kernel changes!

You absolutely must reboot before you try any of this. Sorry.
"

fi

