#!/bin/bash
# Author Leslie Satenstein 2019-01-12 
# mydd1.sh Licensed as gpl3 
# Yours to modify to your hearts content.
# Not responsbile for your goofups
# If necessary, modify ALLOWABLE_Drives to indicate /dev/sd? range. 
#  
echo -e "\n$(basename $0 ) version 0.9 12Mar2019"
MOUNT_INFO=$(mount -l | grep iso9660 )
DEVICE=$(echo $MOUNT_INFO | cut - -d' ' -f1 )
SIZE=${#DEVICE}
if [ $# = 1 ]
then
	if [[ $SIZE < "5" ]];
  	then
  	   echo "ISO $1 not mounted" 
  	   exit 1
	fi
fi
ISOFILE=$1
rc=0
if [[ $# -ne 1 ]];
then
   echo
   echo  "Use to create Bootable USB Flashdrive. Use as: $(basename $0)  path_to_iso/isofile.iso"
   echo "$(basename $0) One parameter: path_to_isofile/distribution.iso"
   echo " You must be certain that the /dev/sd? is for a flashdrive]"
   echo " If in doubt, review list with lsblk."
   echo
   exit 1
fi
if [ ! -e $ISOFILE ];then echo "$ISOFILE does not exist!" ; exit 1; fi
# -- iso file should end in dot iso --
if [[ $ISOFILE != *.iso ]];then echo "\"${ISOFILE}\" is not an *.iso file" >&2
   exit 1
fi
bs='bs=8M'
echo "sudo dd of=$DEVICE if=$1  $bs status=progress oflag=direct,sync"
yn="x"
   echo -e "\n\t\tThis ISO: $1"
   echo -e "\n\t\tTo overwrite this USB/flashdrive information?\n\t\t"
   sudo blkid $DEVICE*
   echo ""
while [[ $yn == "x" ]]
do
   echo -e "continue? y/n "
   read yn
   if [[ "x$yn" != "x" ]];
   then
     if [ "$yn" == "n" ];then exit 0; fi
     if [ "$yn" == "N" ];then exit 0; fi
     if [ "$yn" == "y" ]; then break; fi
     if [ "$yn" == "Y" ]; then break; fi
     if [ "$yn" == "YES" || "$yn" == "yes" ]; 
     then 
	  yn="Y"
	  break; 
     fi
     if [ "$yn" == "NO" || "$yn" == "no" ]; then exit 0; fi
   fi
   yn="x"
done
  
sudo dd if=$1  of=$DEVICE  $bs status=progress oflag=direct,sync
sync
echo "eject $DEVICE"



