#!/bin/bash
###remove any of the temporary file this script generates if it was used before.
rm /tmp/fdisk1.txt
rm /tmp/ls1.txt
rm /tmp/fdisk2.txt
rm /tmp/ls2.txt
rm /tmp/diffdev.txt
rm /tmp/diffmedia.txt
###Start up dialog that informs user that the purpose of this script is to produce 
###a bootable usb stick.
zenity --info --text="This application will assist you in the creation of a bootable install USB for Vectorlinux"

###Inform the user that the usb to be used for this task should not be in the 
###computer at this point.  Once removed click OK.
zenity --info --text="Select a usb stick at least 2 gb in size. The extra space is required for extraction from the compressed file."
zenity --info --text="Please make sure the USB stick you are using for this operation has been removed from the pc and then click OK"

###Capture the output of fdisk -l to fdisk1.txt as well as the output of 
###ls /media/ to ls1.txt
fdisk -l >> /tmp/fdisk1.txt
ls /media/ >> /tmp/ls1.txt
###Ask the user to insert the usb they are going to use for this task.
###and once the usb is inserted press ok.

zenity --info --text="Please insert the usb stick you wish to use and click OK"
zenity --question --text "Abort click NO To Proceed click YES"; answ=$?
if [ "$answ" = "1" ]; 
  then exit 
fi
###Capture the output of fdisk -l to fdisk1.txt
fdisk -l >> /tmp/fdisk2.txt
ls /media/ >> /tmp/ls2.txt
###Capture the diff we are going to work with.
diff /tmp/fdisk1.txt /tmp/fdisk2.txt >> /tmp/diffdev.txt
diff /tmp/ls1.txt /tmp/ls2.txt >> /tmp/diffmedia.txt
###Get the /dev location of the newly inserted usb.
devusb=`grep -o /dev/sd.1 /tmp/diffdev.txt`
mediausb=`grep - /tmp/diffmedia.txt`
mediausb2="/media/"${mediausb:2}
##This bit is just some output used for testing
echo $devusb
echo $mediausb2
###Warn user that anything on this drive will be removed.

zenity --question --text "Any data you have on the usb is about to be deleted. Proceed?"; answ=$?
if [ "$answ" = "1" ]; 
  then exit 
fi

(
		echo "5" ; cd $mediausb2
        echo "10" ; rm -r -f ./*
        echo "# Making the usb bootable" ; sleep 1
        echo "20" ; syslinux $devusb ###Make the usb bootable
        echo "# Downloading needed files 10 - 12 min on fast internet speeds" ; sleep 1
        echo "60" ; wget http://vectorlinux.osuosl.org/stretchedthin/content.tar
        echo "# Untarring the compressed file" ; sleep 1
        echo "80" ;  tar -xf ./content.tar
        echo "# Removeing the compressed file." ; sleep 1
        echo "90" ; rm ./content.tar
        echo "# Process Complete." ; sleep 1
        echo "100" ; sleep 1
        ) |
        zenity --progress \
          --title="ISO4USB downloader" \
          --text="Deleting USB contents..." \
          --percentage=0

        if [ "$?" = -1 ] ; then
                zenity --error \
                  --text="Update canceled."
        fi







### wget the tar file of goodies








