#!/bin/sh

# This script is provided in hopes that it will be useful.  
#
# Its main purpose is to give an overview of the steps that are 
# required to create a 2GB mSD image for the TS-75XX/TS-4500 WITHOUT the 
# Eclipse IDE. The key here is the MBR image.  The rest is just writing the 
# proper images to the proper partitions and extracting the Debian filesystem.
# 
# One major advantage of this script is that a new image can be easily created
# from updates that have been pushed to the FTP site (such as a new kernel or 
# initrd image).
#
# Before running this script, verify the device location of your mSD card 
# after you've inserted it using 'fdisk -l'.  The other requirement is that
# you have access to the internet, including Technologic System's FTP repo.
# Once these two requirements have been satisfied, call the script as such
# (assuming the mSD card has been enumerated to the /dev/sdb device node):
#
#	chmod +x prep_noeclipse_image
# 	./prep_noeclipse_image /dev/sdb
#
# If you'd like to use a larger mSD card and take advantage of the extra 
# capacity, there's a script called "grow_sd" that will do all the dirty work
# for you.  It can be found here:
#
# 	http://oz.embeddedarm.com/~derek/Grow_SD_Card/grow_sd
#
# -DRH 08/27/10
#    - Initial Release (tested okay)

#########################################
# Final Check and Messages
#########################################
goodbye(){
	echo -n "Unmounting disks..."
	umount ${DISK}* >/dev/null 2>&1 </dev/null
	echo "done"

	echo -n "Running filesystem checks..."
	fsck -f ${DISK}1 >/dev/null 2>&1 </dev/null
	fsck -f ${DISK}3 >/dev/null 2>&1 </dev/null
	fsck -f ${DISK}4 >/dev/null 2>&1 </dev/null
	echo "done"

	echo -n "Syncing disk..."
	sync
	echo "done"
	
	echo "It is now safe to remove and use the SD card. Enjoy!" 
	exit 0
}

#########################################
# Initial checks and variable init 
#########################################
# CHECK FOR ROOT ACCESS
if [ `id -u` -ne 0 ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

# CHECK FOR COMMAND LINE ARGUMENT
if [ $# -lt 1 ]; then
   echo "Example usage: $0 /dev/sdb"
   exit 1
fi

DISK=$1
MBR="ftp://ftp.embeddedarm.com/ts-arm-sbc/ts-7500-linux/binaries/ts-images/sd-mbr-noeclipse.dd"
KERNEL="ftp://ftp.embeddedarm.com/ts-arm-sbc/ts-7500-linux/binaries/ts-images/kernel-image-latest.dd"
INITRD="ftp://ftp.embeddedarm.com/ts-arm-sbc/ts-7500-linux/binaries/ts-images/initrd-busybox-rootfs-latest.dd"
DIST="ftp://ftp.embeddedarm.com/ts-arm-sbc/ts-7500-linux/distributions/debian-lenny-arm-latest.tar.gz"
MNT="/mnt/tmp"

# CHECK IF DISK IS MOUNTED
mount | grep $DISK > /dev/null
if [ $? -eq 0 ]; then
	echo "Please unmount $DISK and re-run $0."
	exit 1
fi

# CHECK IF $MNT IS BEING USED
mount | grep "$MNT" > /dev/null
if [ $? -eq 0 ]; then
	echo "This script plans on using $MNT as mount point for SD card,"
	echo "but $MNT is a mount point for something else currently."
	echo "Please unmount it and re-run $0."
	exit 1
fi

#########################################
# MAIN portion of script
#########################################
echo -n "Writing MBR..."
curl -s $MBR > ${DISK}
if [ $? -gt 0 ]; then
	echo "Fail!  Curl failed when downloading MBR!"
	exit 1
fi
echo "done"

echo -n "Writing kernel..."
curl -s $KERNEL > ${DISK}2
if [ $? -gt 0 ]; then
	echo "Fail!  Curl failed when downloading kernel!"
	exit 1
fi
echo "done"

echo -n "Writing initrd..."
curl -s $INITRD > ${DISK}3
if [ $? -gt 0 ]; then
	echo "Fail!  Curl failed when downloading initrd!"
	exit 1
fi
echo "done"

echo -n "Creating EXT3 filesystem on ${DISK}4..."
mkfs.ext3 ${DISK}4 >/dev/null 2>&1 </dev/null
if [ $? -gt 0 ]; then
	echo "Failed to create filesystem!"
	exit 1
fi

echo "done"

echo -n "Extracting filesystem (this will take a while)..."
mkdir $MNT 2> /dev/null 
mount ${DISK}4 $MNT
if [ $? -eq 32 ]; then 
	echo "Failed to mount ${DISK}4!" 
	exit 1
fi

curl -s $DIST | tar xz -C $MNT
if [ $? -gt 0 ]; then
	echo "Fail!  Curl or tar failed when downloading filesystem!"
	exit 1
fi
echo "done"

goodbye
