#!/bin/bash
#Check if required commands are available, if not script exits
command -v VBoxManage >/dev/null || { echo -e "!!ERROR!!\nVirtualBox not properly installed!\nMissing 'VBoxManage' command\nNow Exitting..."; exit 1;};command -v usermod >/dev/null || { echo -e "!!ERROR!!\nMissing 'usermod' command\nNow Exitting..."; exit 1;};command -v groups >/dev/null || { echo -e "!!ERROR!!\nMissing 'groups' command\nNow Exitting..."; exit 1;}

while [ $# -gt 0 ] #Start Loop to Analyse flags and variables
do
    case "$1" in
	-c)  WALKTHROUGH="off"; COMMAND="on";; #Activates other Flags
	-f)  LOCATION="$2"; CON="on"; shift;;
	-d)  DRIVE="$2"; CON="on"; shift;;
	-s)  SOUND="off"; CON="on";; #Disables Minimal Informative Text, useful for using script with other scripts
	-*)  echo -e >&2 "\n\033[33mVirtualBox Bootable USB Mounter v1.1\nby Japzone\033[0m\n\nusage: $(basename $0) [OPTIONS...]\n\nMounts USB Drives to a VMDK for use in VirtualBox VMs\n(ex: Test Your Bootable USB Drive without Rebooting)\n\nFileTrip Page: \033[36mhttp://goo.gl/x5Flr\033[0m\nVirtualBox Forum Thread: \033[36mhttp://goo.gl/NWV59\033[0m\n\nOptions:\n	-c -- switch off walkthrough mode and enable 'Command Flags'\n	-h -- show this help info\nCommand Flags:\n	-f -- directory to save VMDK\n	      (defaults to home directory if not specified)\n	-d -- drive to mount to VMDK \n	      (partitions won't work)\n\n	-s -- silence all output and just execute\n	      (!!NOT RECOMMENDED!!)\n"
	     exit 1;; #Display Help if bad flag is used
	*)   break;; #Leave Loop once no more flags are present
    esac
    shift
done
if [ "$CON" = "on" ]; then if [ "$COMMAND" != "on" ]; then echo -e "FAIL!! - Command Flags Not Enabled"; exit 1;fi ; fi

#Checks if User has required Permissions and if not offer to apply a fix
USRMOD=`groups $USER | grep -o -e disk`
if [ "$USRMOD" != "disk" ]; then
	echo -e "------------------------------\nYou aren't part of the 'disk' group!\nThis is needed for VirtualBox to access RAW Drives!\nWould you like to add yourself to the group?(NEEDS ROOT!)\n------------------------------"
	read -p "(yes/no) - " GROUPADD
	if [ "$GROUPADD" = "yes" ]; then sudo usermod -a -G disk $USER; echo -e "------------------------------\nPlease Log-Out and then Log-In for changes to take effect!\n------------------------------"; read -p "Press any key to continue...";
	else echo -e "------------------------------\nCannot Continue..."; exit ; fi
fi

#Walkthrough Mode Provides Detailed Steps for First Time Users
if [ "$WALKTHROUGH" != "off" ]; then
	clear
	echo -e "\033[31m**************************************************\n	       --------------------\n		     WARNING\n	       --------------------\n     By using this script you risk damage to your \nsystem, possibly causing permanent data loss. You \nare sole responsible for any damage caused while \nusing this script. This script simply acts as a \nfront-end for the VBoxManage, usermod, groups, and \numount commands. Please be careful while using this.\n\n     For more advanced uses of this script please \nconsult the helpfile: '$(basename $0) -h'\n**************************************************\n\n\033[36mIf you understand the risks type '\033[33mcontinue\033[36m'\033[0m"
	read WARNING; if [ "$WARNING" != "continue" ]; then clear; echo -e "\n\nGood-Bye\n\n"; exit 1; fi
	clear
	echo -e "##############################\nChoose USB Drive to mount as VMDK:\n------------------------------"
	fdisk -l | grep --color=never -e "Disk /dev/"
	echo -e "##############################\nEnter the Device Path of the Drive to Mount:\n(NOTE -- Don't enter a partion -- ie:/dev/sdc1)"
	read -p "(ex: /dev/sdc ) - " DRIVE
	echo -e "##############################\nYou entered: $DRIVE\nIs this Correct?"
	read -p "(yes/no) - " CORRECT; if [ "$CORRECT" != "yes" ]; then echo "Try Again"; exit; fi
	echo -e "##############################\nPlease enter the save location for VMDK\n(Type nothing to default to your Home Directory)"
	read -p "(ex: /home/iamhere/Documents/ ) - " LOCATION; if [ "$LOCATION" = "" ]; then LOCATION=~; fi
	LOCATIONTEST=`echo "$LOCATION " | grep -o -e "/ "`; if [ "$LOCATIONTEST" != "/ " ]; then SLASHY="/usb"; else SLASHY="usb"; fi
	echo -e "##############################\nYou entered: $LOCATION\nIs this Correct?"
	read -p "(yes/no) - " CORRECT; if [ "$CORRECT" != "yes" ]; then echo "Try Again"; exit; fi
	echo -e "##############################\n##############################\nMount '$DRIVE' to '$LOCATION$SLASHY.vmdk'?\n(!!WARNING!! - THIS WILL UNMOUNT THE DRIVE FROM YOUR SYSTEM!!)"
	read -p "(yes/no) - " CORRECT; if [ "$CORRECT" != "yes" ]; then echo "Try Again"; exit; fi
	if [ -e "$LOCATION$SLASHY.vmdk" ]; then rm "$LOCATION$SLASHY.vmdk"; fi
	if mount | grep -s -q "$DRIVE"; then umount $DRIVE?*; fi
	VBoxManage internalcommands createrawvmdk -filename "$LOCATION$SLASHY.vmdk" -rawdisk $DRIVE
	echo -e "##############################\n##############################\nSUCCESS!\nNow just mount '$LOCATION$SLASHY.vmdk' in a Virtual Machine\n----------"
exit 1; fi

#Command Mode bypasses the Walkthrough and allows Advanced Users access to directly input the variables
if [ "$COMMAND" = "on" ]; then
	if [ "$LOCATION" = "" ]; then LOCATION=~; fi #If user does not provide a save directory, defaults to home directory
	LOCATIONTEST=`echo "$LOCATION " | grep -o -e "/ "`; if [ "$LOCATIONTEST" != "/ " ]; then SLASHY="/usb"; else SLASHY="usb"; fi #Checks whether user added an ending BackSlash to the directory, if not a fix is applied
	if [ "$SOUND" != "off" ]; then
		echo -e "##############################\nMount '$DRIVE' to '$LOCATION$SLASHY.vmdk'?\n(!!WARNING!! - THIS WILL UNMOUNT THE DRIVE FROM YOUR SYSTEM!!)\n##############################"
		read -p "(yes/no) - " CORRECT; if [ "$CORRECT" != "yes" ]; then echo "Try Again"; exit; fi
	fi
	if [ -e "$LOCATION$SLASHY.vmdk" ]; then rm "$LOCATION$SLASHY.vmdk"; fi #Detect if a VMDK already exists, if so delete it
	if mount | grep -s -q "$DRIVE"; then umount $DRIVE?*; fi #Detect if Drive is mounted, if so then Unmount Drive from System before attaching to VMDK to prevent data corruption
	VBoxManage internalcommands createrawvmdk -filename "$LOCATION$SLASHY.vmdk" -rawdisk $DRIVE #Generates VMDK file for mounting in VB
	if [ "$SOUND" != "off" ]; then
		echo -e "##############################\n##############################\nSUCCESS!\nNow just mount '$LOCATION$SLASHY.vmdk' in a Virtual Machine\n----------"
	fi
exit 1; fi










