#!/bin/sh

VERSION="0.0.5"

ISO=""
USB=""
LOFIDSK=""
DSK=""

LOFIADM=/usr/sbin/lofiadm
MOUNT=/sbin/mount
UMOUNT=/sbin/umount
UNAME=/usr/bin/uname
STAT=/usr/bin/stat
TRUNCATE=/bin/truncate
RM=/usr/bin/rm
CAT=/usr/bin/cat
MKDIR=/usr/bin/mkdir
CP=/usr/bin/cp
CPIO=/usr/bin/cpio
FIND=/usr/bin/find
SED=/usr/bin/sed
FDISK=/sbin/fdisk
FORMAT=/usr/sbin/format
NEWFS=/usr/sbin/newfs
INSTALLBOOT=/usr/sbin/installboot
INSTALLGRUB=/usr/sbin/installgrub

usage()
{

	echo "mkusb (DilOS), v,${VERSION}"
	echo "Usage:"
	echo "$0 -i source.iso -o dilos.usb"
	echo "where"
	echo "-i source.iso	source ISO file"
	echo "-o dilos.usb	output USB image"
	echo
	echo "Example:"
	echo "$0 -i dilos-core.iso -o dilos-core.usb"
	exit 65
}


cleanup()
{

	${LOFIADM} -d ${LOFIDSK}
	${RM} ${USB}
}


# Main
if [ $# -lt 1 ]; then
	usage
fi

while [ $# -ne 0 ]; do
	case $1 in
	--help|-help|--h|-h)
		usage
		;;
	-i)
		shift
		if [ $# -eq 0 ]; then
			echo "Missing argument for -i option"
			echo "It should be an ISO image of DilOS"
			usage
		fi
		if [ ! -f $1 ]; then
			echo "Source ISO file does not exist!"
			exit 1
		fi
		ISO=$1
		;;
	-o)
		shift
		if [ $# -eq 0 ]; then
			echo "Missing argument for -o option"
			echo "It should be a target USB file name"
			usage
		fi
		if [ -f $1 ]; then
			echo "The target file $1 is already exist, choose anothe file"
			exit 1
		fi
		if [ -d $1 ]; then
			echo "$1 should not be a directory, specify a new file name"
			exit 1
		fi
		USB=$1
		;;
	esac
	shift
done

ISO_SIZE=$(${STAT} -Lc%s ${ISO})
CORRECTED_SIZE=${ISO_SIZE}
i=0
while [ ${CORRECTED_SIZE} -gt 1024 ] ; do
	CORRECTED_SIZE=$((CORRECTED_SIZE>>10))
	i=$((i+1))
done
shl=$((i*10))
if [ $((CORRECTED_SIZE<<shl)) -le ${ISO_SIZE} ]; then
	CORRECTED_SIZE=$((CORRECTED_SIZE*13/9))
fi
ISO_SIZE=$(((CORRECTED_SIZE+1)<<shl))
unset CORRECTED_SIZE shl
echo "Creating target USB image"
truncate -s ${ISO_SIZE} ${USB}
if [ $? -ne 0 ]; then
	echo "Cannot create a target USB file"
	exit 1
fi

LOFIDSK=$(${LOFIADM} -la ${USB})
if [ $? -ne 0 ]; then
	echo "Cannot attach ${USB} as a block device"
	${RM} ${USB}
	exit 1
fi

PLATFORM=$(${UNAME} -p)
DEV=$(echo ${LOFIDSK}|${SED} 's,.*/,,g;s,p0$,,g')
DSK="/dev/dsk/${DEV}"
RDSK="/dev/rdsk/${DEV}"
FMT=/tmp/format.$$
cat >${FMT} <<EOF
partition
modify
1
yes
0
0
0
0
0
0
0
yes
dilos
quit
EOF
case "${PLATFORM}" in
i386)
    ${FDISK} -B ${RDSK}p0
    ;;
esac
${FORMAT} -s -f ${FMT} -d ${DEV} 2>/dev/null
${RM} -f ${FMT}
unset FMT

echo "Formating the USB image"
echo "y"|${NEWFS} -m 0% -o space ${RDSK}s0 2>/dev/null

TUSB=/tmp/usb.$$
TISO=/tmp/iso.$$

${MKDIR} -p ${TUSB} ${TISO}

echo "Mounting source ISO file"
${MOUNT} -F hsfs ${ISO} ${TISO}
if [ $? -ne 0 ]; then
	echo "Cannot mount source ISO file"
	clenup
	${RM} -rf ${TUSB} ${TISO}
	exit 1
fi

if [ "${PLATFORM}" = "sparc" ]; then
	${INSTALLBOOT} -f -F ufs /usr/platform/$(${UNAME} -i)/lib/fs/ufs/bootblk ${RDSK}s0 >/dev/null
#elif [ -d ${TISO}/boot/grub ]; then
#	echo "Installing GRUB to the USB image"
#	${INSTALLGRUB} -fm ${TISO}/boot/grub/stage1 ${TISO}/boot/grub/stage2  ${RDSK}s0 >/dev/null
else
	echo "Installing bootloader to the USB image"
	${INSTALLBOOT} -f -m /boot/pmbr /boot/gptzfsboot ${RDSK}s0 >/dev/null
fi
echo "Mounting the USB image"
${MOUNT} ${DSK}s0 ${TUSB}
if [ $? -ne 0 ]; then
	echo "Cannot mount target USB image"
	${UMOUNT} ${TISO}
	clenup
	${RM} -rf ${TUSB} ${TISO}
	exit 1
fi

echo "Copying system files..."
${RM} -rf ${TUSB}/lost+found
#${CP} -/afr ${TISO}/* ${TUSB}
#${CP} -adfr ${TISO}/* ${TUSB}
CWD=$PWD
cd ${TISO} && ${FIND} . -depth -print | ${CPIO} -pdm ${TUSB}
cd ${CWD}

echo "Unmounting source ISO file"
${UMOUNT} ${TISO}
echo "Unmounting the USB image"
${UMOUNT} ${TUSB}
${RM} -rf ${TUSB} ${TISO}
${LOFIADM} -d ${LOFIDSK}

echo "Well, done... Enjoy!"
