#!/bin/bash

################################################################################
#      This file is part of makebootable_mac used for the creation
#	   of a bootable USB thumb drive that unRAID can be run from
#      Copyright (C) 2009-2012 Kyle Hiltner (stephan@openelec.tv)
#      Copyright (C) 2013 Lime Technology
#
#  This Program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#
#  This Program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
################################################################################

VERSION="v1.1"

echo "INFO: make_bootable_mac $VERSION"
echo ""

DISTRO=$(uname -s)
if [ $DISTRO != "Darwin" ]; then
	echo ""
	echo "FAIL: This script is only for Mac OS X systems, aborting!"
	echo ""
	exit 1
fi

if [ $(diskutil list | grep UNRAID | wc -l) == 1 ]; then
	echo "INFO: The following drive appears to be the unRAID USB Flash drive: " 
	diskutil list | grep UNRAID | awk '{print "/dev/"$6}' | awk '{split($0,a,"s"); print a[1]"s"a[2]}';diskutil list | grep UNRAID | awk '{print " "$3" " $4$5}'
	TARGET=$(diskutil list | grep UNRAID | awk '{print "/dev/"$6}' | awk '{split($0,a,"s"); print a[1]"s"a[2]}')
	echo ""
else
	echo "INFO: There appears to be no drive present with the label UNRAID"
	echo "Exiting since there is not a drive labeled UNRAID"
	echo ""
	exit 1
fi

mkdir -p /tmp/UNRAID/syslinux ; cp -rp /Volumes/UNRAID/syslinux/* /tmp/UNRAID/syslinux

echo -n "To continue please enter your admin "
sudo /tmp/UNRAID/syslinux/make_bootable_mac.sh $TARGET
