#!/bin/bash
#
# Author: RyeBrye (Ryan Gardner)
# http://ryebrye.com/blog/
# License: Quad-license: GPL / LGPL / BSD / Apache. Pick whichever one you want! :P
#
# Special thanks to Disconnect and bgupta for their work towards this, and Google / Brian Swetland for
# making such a cool tool as fastboot :)
#
# This script converts an input image into a rgb565 file that you will flash to your phone. You should 
# boot your phone into fastboot mode and run this. (power off phone, then hold down the back
# button and the power button to load into the bootloader. on a hacked G1, you should see the 
# skateboarding android image and the text "FASTBOOT" on the phone)
# 
# 
# Tested input files that work: PNG, or BMP (others will probably work as well)
#	
# This script is for the T-Mobile G1 that has a fastboot bootloader flashed onto it,
# or a Dev Phone. 
#
# Requires: Image Magick to be installed and in your path (so the convert command works)
# Requires: rbg2565 to be installed and in your path (built as part of the android source build)
# Requires: fastboot to be installed and in your path (build at part of the android source build)
#
# useage: ./splashimage.sh inputimage.png
# results: will flash your splash image, and reboot your phone so you can see your handiwork! :)
#
# 
#
echo "Converting input image to rgb888 raw (saving in temp directory)"

# perhaps some kind of dithering could be put here to make it not have gradient banding
# on some images? someone with more knowledge of imagemagick will have to do this :)
convert -depth 8 $1 rgb:/tmp/splash.raw

#convert image
echo "Converting input image to rgb565 (saving in temp directoy)"
rgb2565 < /tmp/splash.raw > /tmp/splash.raw565

#checking size
eval $(stat -s /tmp/splash.raw565)
if [ "$st_size" == "307200" ]; then
	echo "Flashing splash image to phone..."
	fastboot flash splash1 /tmp/splash.raw565
	echo "Rebooting phone..."
	fastboot reboot	
	echo "Cleaning up temp files..."
	rm /tmp/splash.raw
	rm /tmp/splash.raw565
else
	echo "File was not the expected size. Make sure you are using an 8-bit 320x480 PNG as the original source."
fi
