#!/bin/bash

# authors:
#	  marco di antonio    #
#	  fabrizio di marco   #
# email: acetoneiso_at_gmail.com
# released under GPL2 license #
# copyright 2007 	      #

if test ! -e /usr/bin/mkisofs
then
kdialog --caption "AcetoneISO" --error "The following package was not found: mkisofs"
exit
else
echo continua
fi

VAROUT=`kdialog --caption "Select Folder to transform in a bootable ISO" --getexistingdirectory $HOME/ `

if test -z $VAROUT
then
exit
else
echo continua
fi

if test ! -w "$VAROUT"
then
kdialog --caption "AcetoneISO" --error "The folder You selected is not writable\n1) If it is located on CDROM/DVD please copy it on your hardisk\n2) Check the folder permissions. Be sure your user can write on it"
exit
else
echo continua
fi

SAVE=`kdialog --caption "Select where to Save ISO and Name!" --getsavefilename $HOME/"acetoneiso_boot_cd" "*.iso |ISO Bootable Image"`

if test -z $SAVE
then
exit
else
echo continua
fi


#choose if bootable GRUB or manual generic
PLAYER=`kdialog --combobox "Select Boot File Type:" "GRUB" "manual"`

if test -z $PLAYER
then 
exit
else
echo continua
fi

if test $PLAYER = GRUB
then

#Make a directory for GRUB:

mkdir -p "$VAROUT"/boot/grub


#Copy the file stage2_eltorito:

  cp /usr/lib/grub/i386-pc/stage2_eltorito "$VAROUT"/boot/grub


#Finally, make a ISO9660 image file like this:

     konsole -e mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot \
         -boot-load-size 4 -boot-info-table -o "$SAVE" "$VAROUT"

kdialog --caption "AcetoneISO2" --msgbox "Process Completed"
exit

else
echo continua
fi

OPENFILE=`kdialog --icon /usr/share/pixmaps/aiso.png --caption "Select Boot File" --getopenfilename $HOME/`

if test -z $OPENFILE
then
exit
else
echo continua
fi

inf=`basename "$OPENFILE"`

mkdir -p "$VAROUT"/boot/acetoneiso

cp "$OPENFILE" "$VAROUT"/boot/acetoneiso

 konsole -e mkisofs -R -b boot/acetoneiso/"$inf" -no-emul-boot \
         -boot-load-size 4 -boot-info-table -o "$SAVE" "$VAROUT"

kdialog --caption "AcetoneISO2" --msgbox "Process Completed"



#If the boot image is not an image of a floppy, you need to add one of the options: -hard-disk-boot or -no-emul-boot. If the system should not boot off the emulated disk, use -no-boot.