File live-usb-gui of Package live-fat-stick

#!/bin/bash
# live-usb-gui : Simple GUI to create bootable usb stick from iso images
# Copyright (c) 2012 CyberOrg Info
# 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 of the License, 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.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Authors:      Jigish Gohil <cyberorg@opensuse.org>
# This script creates bootable openSUSE, Fedora or Ubuntu(or clones) live usb stick on fat partition
#
if [[ $(id -u) != 0 ]]; then
        echo "run this command as root"
	exit 1
fi
PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin
ionice -c3 -p$$
if [ x"$WINDOWMANAGER" = x"/usr/bin/startkde" ]; then
	which kdialog &>/dev/null && popup=kdialog
else
	which zenity &>/dev/null && popup=zenity
fi
popup=${popup:-zenity}
scriptorun=${scriptorun:-live-fat-stick}
which $popup &>/dev/null || echo "$popup not found, please install to use this tool"

export liveusbgui=/tmp/liveusbgui
touch /tmp/liveusbgui
clean_up () {
        if [[ -f $liveusbgui ]]; then
                rm $liveusbgui &>/dev/null
        fi
	if [[ -f $statusfile ]]; then
		rm $statusfile
	fi
}

fileselecttitle="Select distribution iso file"
fileselectpath="/"
fileselectfilter="*iso"
usbselecttitle="Select the target USB device"
usbselecttext="Select the correct device, usually the one with a number at the end;\n\
usually device without a number at the end is for isohybrid."
nousbmedia="No USB media found. Please insert USB stick and try again."
distributionlist="suse suse-persistent fedora mint ubuntu ubuntu-persistent ipxe isohybrid"
distributionlisttext="isohybrid should work for most distribution iso images and EFI booting,\n\
persistent preserve changes in live system after reboots."
distrotitle="Select the distribution of the iso"
scriptstitle="Select script to run"
scriptstext="Select live-fat-stick for vfat/fat32 partition,\n\
live-grub-stick for vfat/fat32/ntfs/ext3/ext4\n\
and any other partition supported by grub2"
supportedscripts="live-grub-stick live-fat-stick"
for i in $(echo "$supportedscripts"); do
        if which $i > /dev/null 2>&1; then
                scriptlist="$scriptlist $i"
        else
                scriptlist="$scriptlist"
        fi
done
usbdevicelist=$(for i in `echo $(for i in $(find /dev/disk/by-path/ |grep usb); do readlink -f $i;done)`
	do
        #	if [[ $(blkid -s TYPE -o value $i) == vfat ]];then
        		echo $i
        #        fi
done)
if [ -z "$usbdevicelist" ] ; then
	if [[ $popup == zenity ]]; then
		zenity --error --text="$nousbmedia"
	else
		kdialog --error "$nousbmedia"
	fi
	clean_up
	exit 1
fi
usbdevicelistkd=$(for device in $usbdevicelist; do
	printf "%s %s %s" ${device} ${device} off 
	printf "\n"
done)

distributionlistkd=$(for distro in $distributionlist; do
	printf "%s %s %s" $distro $distro off
	printf "\n"    
done)
scriptlistkd=$(for script in $scriptlist; do
        printf "%s %s %s" $script $script off
        printf "\n"
done)
check_variable () {
	if [ ! "$1" ]; then
		clean_up
		exit 1
	fi
}
if [[ $popup == zenity ]]; then
	sourceiso=$(zenity --file-selection --title="$fileselecttitle" --file-filter="$fileselectfilter")
	check_variable $sourceiso
	distroname=$(zenity --list --title="$distrotitle" --text "$distributionlisttext" --column="Distribution:" $distributionlist)
	check_variable $distroname
	usbdevice=$(zenity --list --title="$usbselecttitle" --text "$usbselecttext" --column="USB device" $usbdevicelist)
	check_variable $usbdevice
	scriptorun=$(zenity --list --title="Select script to run" --text "$scriptstext" --column="Scripts:" $scriptlist)
        check_variable $scriptorun
else
	sourceiso=$(kdialog --title "$fileselecttitle" --getopenfilename "$fileselectpath" "$fileselectfilter")
	check_variable $sourceiso
	distroname=$(kdialog --separate-output --title "$distrotitle" --radiolist "$distributionlisttext" $distributionlistkd)
	check_variable $distroname
	usbdevice=$(kdialog --separate-output --title "$usbselecttitle" --radiolist "$usbselecttext" $usbdevicelistkd)
	check_variable $usbdevice
        scriptorun=$(kdialog --separate-output --title "$scriptstitle" --radiolist "$scriptstext" $scriptlistkd)
        check_variable $scriptorun
fi

case $distroname in
	fedora)
	option="--fedora"
	;;
	suse)
	option="--suse"
	;;
	ubuntu)
	option="--ubuntu"
	;;
        suse-persistent)
        option="--suse-persistent"
        ;;
        ubuntu-persistent)
        option="--ubuntu-persistent"
        ;;
	mint)
	option="--ubuntu"
	;;
        isohybrid)
        option="isohybrid"
        ;;
        ipxe)
        option="--ipxe"
        ;;
esac

if [ ! "$option" ]; then
        clean_up
	exit 1
fi
questiontitle="Is the information below correct?"
questiontext="Distribution: $distroname \nISO image: $sourceiso \nUSB device: $usbdevice"
errortext="oops, something went wrong, try live-fat-stick or live-grub-stick from terminal"
successtext="Your bootable usb device is now ready"
if [[ $popup == zenity ]]; then
	if ! zenity --question --title="$questiontitle" --text "$questiontext"; then
	        clean_up
	        exit 1
	fi
else
	if ! kdialog --title "$questiontitle" --warningcontinuecancel "$questiontext"; then
		clean_up
	        exit 1
	fi	
fi
statusfile=$(mktemp)
if [[ $option == isohybrid ]]; then
	usbdev=$(echo $usbdevice | sed 's/[0-9]*//g')
	xterm -e "($scriptorun --isohybrid $sourceiso $usbdev || echo 1 > $statusfile)"
else
	xterm -e "($scriptorun $option $sourceiso $usbdevice || echo 1 > $statusfile)"
fi

if [[ $(cat $statusfile) == 1 ]]; then
	if [[ $popup == zenity ]]; then
		zenity --error --text="$errortext"
	else
		kdialog --error "$errortext"
	fi
else
	if [[ $popup == zenity ]]; then
		zenity --info --text="$successtext"	
	else
		kdialog --msgbox "$successtext"
	fi
fi
clean_up

openSUSE Build Service is sponsored by