#!/bin/sh
#(c) copyright 2008 Barry Kauler
#2008 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#v411 BK: wrapper for gparted, prevent indefinite scan at startup on some systems.
#v424 update desktop drive icons. note, this is also done in /usr/sbin/bootflash, puppyinstaller.
#v424 report that gparted crash with a non-english locale.
#111013 remove LANG=C
#130118 rodin.s: add gettext.
#130722 new method to signal to update desktop drive icons.
#150120 warning for windows vista, 7, 8.

export TEXTDOMAIN=gparted_shell
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
. gettext.sh
export LANGORG=$LANG

#v424 report that gparted crash with a non-english locale. well, maybe just do this...
#export LANG=C # off for gettext, hope it won't crash

echo -n "" > /tmp/gparted_shell_probe
PROBEDISK="`probedisk2 | grep -v '|optical|'`"
echo "$PROBEDISK" |
while read ONEPROBE
do
 ONEDRIVE="`echo -n "$ONEPROBE" | cut -f 1 -d '|' | cut -f 3 -d '/'`"
 ONECATEG="`echo -n "$ONEPROBE" | cut -f 2 -d '|'`"
 case $ONECATEG in
  drive) ONECATEG="$(gettext 'internal drive')";;
  usbdrv) ONECATEG="$(gettext 'USB drive')";;
 esac
 ONEDESCR="`echo -n "$ONEPROBE" | cut -f 3 -d '|' | tr '\"' ' '`"
 echo "<radiobutton><label>${ONEDRIVE} ${ONECATEG}. $(gettext 'Descr:') ${ONEDESCR}</label><variable>RADIO_${ONEDRIVE}</variable></radiobutton>" >> /tmp/gparted_shell_probe
done

 echo "<radiobutton><label>$(gettext 'ALL DRIVES. Scan all drives at startup')</label><variable>RADIO_all</variable></radiobutton>" >> /tmp/gparted_shell_probe
 
DRVTAGS="`cat /tmp/gparted_shell_probe`"

export MAIN_DIALOG="
<window title=\"Gparted\">
 <vbox>
  <text><label>$(gettext 'Welcome, Gparted is an application to examine and modify partitions in a disk drive. At startup, the default behaviour is for Gparted to scan for all drives in the computer, and on some computers this can take a very long time. Therefore, this dialog window enables you to make a choice of which drive you want to work on (and Gparted will only scan that drive at startup).')</label></text>
  
  <text use-markup=\"true\"><label>\"$(gettext '<b>WARNING:</b> A PC may have a single VFAT or NTFS partition, with Windows installed, and you might want to decrease the size of that partition so as to be able to create a Linux partition. For Windows XP/98/95 this is usually OK (but be careful not to make it too small). However, Windows Vista, 7 and 8 get very upset if the partition is resized by Gparted, and may not work -- in that case, there is a utility in Windows to resize partitions. Use that, then Gparted can be used to create new partitions in the free space.')\"</label></text>
  
  ${DRVTAGS}
  
  <hbox>
   <button>
    <label>$(gettext 'OKAY')</label>
    <action type=\"exit\">OKAYBUTTON</action>
   </button>
   <button>
    <label>$(gettext 'CANCEL')</label>
    <action type=\"exit\">CANCELBUTTON</action>
   </button>
  </hbox>

 </vbox>
</window>
"

RETVALS="`gtkdialog --program=MAIN_DIALOG`"

[ "`echo "$RETVALS" | grep 'OKAYBUTTON'`" = "" ] && exit

THEDRIVE="`echo "$RETVALS" | grep '"true"' | cut -f 1 -d '=' | sed -e 's/RADIO_//'`"

[ "$THEDRIVE" = "all" ] && exec gparted

#v424 record state...
tdPATTERN="/dev/${THEDRIVE}"
DRVSTATE1="`probepart | grep "$tdPATTERN"`"

gparted /dev/${THEDRIVE}

#v424 update desktop drive icons. note, this is also done in /usr/sbin/bootflash, puppyinstaller...
#/tmp/pup_event_frontend_block_request is used in /sbin/pup_event_frontend_d to refresh drv...
DRVSTATE2="`probepart | grep "$tdPATTERN"`"
if [ "$DRVSTATE1" != "$DRVSTATE2" ];then
 sync
 #130722 this file was read by the old /sbin/pup_event_frontend_d big script...
 #echo "$THEDRIVE" > /tmp/pup_event_frontend_block_request
 #now have a binary daemon, send this uevent to it, then /usr/local/pup_event/frontend_change will be called...
 echo change > /sys/block/${THEDRIVE}/uevent
fi

###END###
