#!/usr/bin/env bash
# $MirOS: ports/infrastructure/scripts/darwindiskimage,v 1.3 2007/07/02 13:18:21 tg Exp $
# $NetBSD: darwindiskimage,v 1.2 2006/08/30 04:36:10 schmonz Exp $
#-
# Copyright (c) 2006
#	Thorsten Glaser <tg@mirbsd.de>
# Derived from NetBSD(R) pkgsrc(R)
#
# Provided that these terms and disclaimer and all copyright notices
# are retained or reproduced in an accompanying document, permission
# is granted to deal in this work without restriction, including un-
# limited rights to use, publicly perform, distribute, sell, modify,
# merge, give away, or sublicence.
#
# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
# the utmost extent permitted by applicable law, neither express nor
# implied; without malicious intent or gross negligence. In no event
# may a licensor, author or contributor be held liable for indirect,
# direct, other damage, loss, or other issues arising in any way out
# of dealing in the work, even if advised of the possibility of such
# damage or existence of a defect, except proven that it results out
# of said person's immediate fault when using the work as intended.
#-
# Use this to create a disk image, for use with a MirPorts Framework
# installation on a default case-insensitive Mac OSX installation.
#
# $ /usr/bin/cvs -d anoncvs@anoncvs.mirbsd.org:/cvs co ports/infrastructure
# $ cp ports/infrastructure/scripts/darwindiskimage ~
# $ bash ~/darwindiskimage create ~/Documents/MirPorts 512
#				# Mebibytes - season to taste
# $ bash ~/darwindiskimage mount ~/Documents/MirPorts
# $ sudo chown $(id -u):$(id -g) /Volumes/MirPorts
# $ cd /Volumes/MirPorts
# $ /usr/bin/cvs -d anoncvs@anoncvs.mirbsd.org:/cvs co ports
# $ bash ./Setup.sh -uel /Volumes/MirPorts/mpkg
# $ . /Volumes/MirPorts/mpkg/db/SetEnv.sh
# $ cd devel/cvs
# $ mmake install clean
# $ cd ../..
# $ /Volumes/MirPorts/mpkg/bin/cvs -Rq up -PAd
#
# To use:
# $ bash ~/darwindiskimage mount ~/Documents/MirPorts
# $ . /Volumes/MirPorts/mpkg/db/SetEnv.sh
# $ cd /Volumes/MirPorts/ports/foo/bar; mmake install clean
#
# Have fun...


_getdevice_and_halfway_mount() {
	hdid -nomount "$1" | _getdevicebasename | tail -1
}

_getdevicebasename() {
	awk '{print $1}' | sed -e 's|^/dev/||'
}

_normalise_filename() {
	echo "$1" | sed -e 's|\.dmg$||' -e 's|$|.dmg|'
}

dmg_create() {
	local fstype fs osmajor file mountedname megabytes device
	[ $# -eq 2 ] || die 1 "Usage: $0 create <file> <megabytes>"

	# Use case-sensitive HFS+ where available (Darwin >= 7)
	fstype='Apple_UFS'
	fs='UFS'
	osmajor=`uname -r | awk 'BEGIN {FS="."} {print $1}'`
	if [ ${osmajor} -ge 7 ]; then
		fstype='Apple_HFSX'
		fs='HFSX'
	fi

	file="`_normalise_filename \"$1\"`"
	mountedname="`basename \"${file}\" .dmg`"
	megabytes=$2

	# create
	hdiutil create -quiet "${file}" -megabytes ${megabytes} \
		-partitionType ${fstype} -layout SPUD -fs ${fs}

	# rename
	device=`_getdevice_and_halfway_mount "${file}"`
	hdiutil mount "${file}"
	disktool -n "${device}" "${mountedname}"
	hdiutil eject -quiet "${device}"
}

dmg_mount() {
	local file device exitcode
	[ $# -eq 1 ] || die 1 "Usage: $0 mount <file>"

	file="`_normalise_filename \"$1\"`"

	hdiutil mount ${file}
}


dmg_umount() {
	local mountpoint device
	[ $# -eq 1 ] || die 1 "Usage: $0 umount <mount-point>"

	mountpoint="$1"
	device=`mount | grep "${mountpoint} (local" | _getdevicebasename`

	[ "${device}" ] || die 1 "error: no device mounted at ${mountpoint}"

	hdiutil eject -quiet "${device}"
}

die() {
	local exitcode
	exitcode=$1; shift
	warn "$@"
	exit ${exitcode}
}

warn() {
	echo >&2 "$@"
}

try() {
	exitcode=$1; shift
	action=$1; shift
	error=`"${action}" "$@" 2>&1` || die ${exitcode} "${error}"
}

main() {
	[ $# -eq 0 ] && die 1 "Usage: $0 <create|mount|umount>"
	ACTION="$1"; shift
	case ${ACTION} in
	create|mount|umount)
		try 1 "dmg_${ACTION}" "$@"
		return 0
		;;
	*)
		die 1 "Usage: $0 <create|mount|umount>"
		;;
	esac
}

PATH=${PATH}:/sbin:/usr/sbin
main "$@"
exit $?
