#!/bin/bash
# Author: Cesar FUGUET
# Date  : Novembre 2013

clusters=65536
sector_size=512
sectors_per_cluster=8
reserved_sectors=32

# FAT32 SPEC: The first two clusters are not present in the data region but
# they are used in the computation of the FAT sectors
data_region_clusters=$clusters-2

let "data_sectors = data_region_clusters * sectors_per_cluster"
let "fat_sectors  = (clusters * 4) / 512"

# The disk image contains:
#   - MBR sector : this sector is removed by this script as it will be copied
#                  after)
#   - Reserved region sectors (usually 32)
#   - FAT region sectors
#   - DATA region sectors
let "sectors      = data_sectors + fat_sectors + reserved_sectors + 1" 

platform=`uname`
echo "Detected platform: $platform"

if [[ $platform == "Darwin" ]]; then
	case $1 in
		create )
			# Create disk image
			# -fs        <fs>   : use filesystem <fs>
			# -volname   <name> : use volume name <name>
			# -srcfolder <src>  : copy every file in <src> into image
			# -format    <fmt>  : format of image. UDRW (UDIF read/write image)
			# -sectors   <sect> : size of image in 512-byte sectors
			# -ov				: overwrite existing image file
			# -fargs	 <args> : additional args to pass to newfs program 
			#			     -F : FAT type (FAT32)
			#				 -c : sector per cluster
			#				 -n : number of FAT

			if [[ -z $2 ]]; then
				echo "Create action need second argument: <srcdir_path>"
				exit 1;
			fi;

			if [[ -z $3 ]]; then
				echo "Create action need third argument: <disk_image>"
				exit 1;
			fi;

			# RAW DISK IMAGE READ-ONLY
			# The not used space is not considered (smaller disk image)
			format="UDRO"

			# RAW DISK IMAGE READ-WRITE
			# None compression is made
			# format="UDRW" 

			echo "Creating partition file $3 from $2 directory"
			hdiutil create \
				-fs MS-DOS \
				-volname virtualdisk \
				-srcfolder $2 \
				-sectors $sectors \
				-ov \
				-fsargs "-F32 -c$sectors_per_cluster -n1 -r$reserved_sectors" \
				-format $format \
				$3; 
#				-verbose \

			echo "Removing first sector of diskimage"

			if [[ -e "/tmp" ]]; then
				if [[ -e "/tmp/diskimage" ]]; then
					echo "Warning /tmp/diskimage exists already. Rename it or"
					echo "remove it before executing this script"
					exit
				fi;

				dd if=$3.dmg of=/tmp/diskimage iseek=1 oseek=0
				mv /tmp/diskimage $3.dmg
			fi;;

		attach )
			# Attach created disk image to the system and mount it
			hdiutil attach $2;;

		detach )
			# Detach attached device. Must pass as parameter the /dev/<device>
			hdiutil detach $2;;

		info )
			hdiutil imageinfo $2;;

		* )
			echo "Pass the command as the first parameter: "
			echo "  - create: create disk image"
			echo "            First parameter after create action is the path"
		    echo "            of the directory to copy in the created "
			echo "            partition"
			echo "  - attach: attach created disk image to the system and "
			echo "            mount it (arg: path to disk_image)"
			echo "  - detach: detach specified device "
			echo "            (arg: path to /dev/<device>)"
			echo "  - info  : print information about created disk image"
			echo "            (arg: path to disk_image)"
			echo ""
			echo "EXAMPLES:"
			echo " ./create_diskimage.sh create /path/to/source_dir disk_image"
			echo " ./create_diskimage.sh attach /path/to/disk_image.dmg"
			echo " ./create_diskimage.sh detach /dev/disk5"
			echo " ./create_diskimage.sh info   /path/to/disk_image.dmg"
	esac;

elif [[ $platform == "Linux" ]]; then
	case $1 in
		create )
			if [[ -z $2 ]]; then
				echo "Create action need second argument: <srcdir_path>"
				exit 1;
			fi;

			if [[ -z $3 ]]; then
				echo "Create action need third argument: <disk_image>"
				exit 1;
			fi;

			echo "Creating partition file: $3"

			let "sectors = (sectors - 1) / 2" 
		    mkfs.vfat \
				-C \
				-F32 \
				-f 1 \
				-R $reserved_sectors \
				-S $sector_size \
				-s $sectors_per_cluster \
				-v \
				$3.dmg $sectors

			if [[ -e $2 ]]; then
				echo "Copying files from $2 into partition"
				mcopy -s -i $3.dmg $2/* :://
			fi;;

		* )
			echo "Pass the command as the first parameter: "
			echo "  - create: create disk image"
			echo "            First parameter after create action is the path"
		    echo "            of the directory to copy in the created "
			echo "            partition"
			echo ""
			echo "EXAMPLES:"
			echo " ./create_diskimage.sh create /path/to/source_dir disk_image"
	esac;

else
	echo "Your platform is not supported: $platform"
fi;

