#!/bin/sh

set_SAVEFILE (){
#user select a file, and we have to update the filename <entry>
[ -f "$CHOOSER" ] && basename "`echo "$CHOOSER"`" > $WORKDIR/SAVEFILE_NAME
}

export -f set_SAVEFILE

cd $HOME
#basename "`cat $WORKDIR/SAVEFILE`" > $WORKDIR/SAVEFILE_NAME
echo Pburn.$SAVE_EXT  > $WORKDIR/SAVEFILE_NAME
export savebox="
<window title=\"Pburn - Save\" icon-name=\"gtk-save\" resizable=\"false\">
 <vbox>
  <frame Save>
   <hbox>
    <entry>
    <variable>SAVEFILE</variable>
     <input>cat $WORKDIR/SAVEFILE_NAME</input>
    </entry>
   </hbox>
   <chooser>
    <height>400</height><width>600</width>
    <variable>CHOOSER</variable>
    <action>. $APPDIR/func -set_SAVEFILE</action>
    <action>refresh: SAVEFILE</action>
   </chooser>
  </frame>
  <hbox>
   <button>
    <input file icon=\"gtk-cancel\">
    </input><label>$LOC_CANCEL</label>
   </button>
   <button can-default=\"true\" has-default=\"true\" use-stock=\"true\">
    <input file icon=\"gtk-save\"></input>
    <label>$LOC103</label>
    <action type=\"exit\">save</action>
   </button>
  </hbox>
 </vbox>
</window>"
I=$IFS; IFS=""
for STATEMENTS in  $($GTKDIALOG --program=savebox --center); do
	eval $STATEMENTS
done
IFS=$I

if [ "$EXIT" = "save" ]; then
	CHOOSER="`dirname "$CHOOSER"`" #we want to define the dir we're standing in, not the autoselected one.
	echo "$CHOOSER"/"$SAVEFILE".$SAVE_EXT > $WORKDIR/SAVEFILE
	sed -i -e "s%\.$SAVE_EXT.$SAVE_EXT%.$SAVE_EXT%g" $WORKDIR/SAVEFILE #remove double extension if exist.
	
	#checks
	if [ "$EXPORT_ISO" = "true" ]; then
		cp -f $WORKDIR/SAVEFILE $WORKDIR/isofile
	else
		if [ -s "`cat $WORKDIR/SAVEFILE`" ]; then #already exist
			IMG=warning
			FRAME=Warning
			TXT1="<b>$LOC385</b>"
			TXT2="$LOC386"
			. "$APPDIR"/box_yesno
			if [ $EXIT = no ]; then 
				echo -e "\c" > $WORKDIR/SAVEFILE
			fi
		fi
	fi
else
	echo -e "\c" > $WORKDIR/SAVEFILE
fi

	