#!/bin/ash
#Barry Kauler 2012
#GPL v3 (/usr/share/doc/legal)
#120202 internationalized. add udf f.s.
#120723 reduce mmcblk0p1 to mm0p1.
#130216 support f2fs.
#130331 reduce Mono size from 26 to 22, for 96 dpi.

export TEXTDOMAIN=partview
export OUTPUT_CHARSET=UTF-8

yaf-splash -bg orange -fg black -close never -fontsize large -text "$(gettext 'Please wait, probing drives...')" &
yPID=$!

GTKDIALOGEXE="gtkdialog"
[ "`which gtkdialog4`" != "" ] && GTKDIALOGEXE="gtkdialog4"

XLEFT2=84; YBOX=26; BOXWIDTH=240 #294

echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg
   id="svg1"
   xmlns="http://www.w3.org/2000/svg"
>' > /tmp/partview_display_svg

echo  "<text
     xml:space=\"preserve\"
     style=\"fill:black;stroke:none;font-family:Nimbus Mono L;font-style:normal;font-weight:bold;font-size:22;fill-opacity:1;stroke-opacity:1;stroke-width:3pt;stroke-linejoin:miter;stroke-linecap:butt;text-anchor:start;writing-mode:lr;\"
     x=\"$(($XLEFT2 + 12))\"
     y=\"18\">$(gettext 'size')
  </text>
  <text
     xml:space=\"preserve\"
     style=\"fill:black;stroke:none;font-family:Nimbus Mono L;font-style:normal;font-weight:bold;font-size:22;fill-opacity:1;stroke-opacity:1;stroke-width:3pt;stroke-linejoin:miter;stroke-linecap:butt;text-anchor:start;writing-mode:lr;\"
     x=\"$(($XLEFT2 + $BOXWIDTH + 6))\"
     y=\"18\">$(gettext 'free')
  </text>" >> /tmp/partview_display_svg

#130216 add f2fs...
PARTSLIST="`probepart -k 2> /dev/null | grep '^/dev/' | grep -E 'f2fs|udf|iso9660|ext2|ext3|ext4|reiserfs|msdos|vfat|ntfs|minix' | cut -f 1-3 -d '|' | tr '\n' ' '`"

for APART in $PARTSLIST
do
 MNTSTATUS='(currently mounted)'
 ATAG="`echo -n "$APART" | cut -f 1 -d '|' | cut -f 3 -d '/'`"
 ASIZE=`echo -n "$APART" | cut -f 3 -d '|'`
 if [ -e /proc/ide ];then #v431 old kernel without libata drivers.
  ATIPE=`echo -n "$APART" | cut -f 2 -d '|'`
  case $ATIPE in
   iso*)
    ASIZE=4300000
    [ "`dvd+rw-mediainfo /dev/${ATAG} 2>&1 | grep 'non-DVD'`" != "" ] && ASIZE=700000
   ;;
  esac
 else
  case $ATAG in
   sr*)
    ASIZE=4300000
    [ "`dvd+rw-mediainfo /dev/${ATAG} 2>&1 | grep 'non-DVD'`" != "" ] && ASIZE=700000
   ;;
  esac
 fi
 AFS="`echo -n "$APART" | cut -f 2 -d '|'`"
 AFPATTERN="^/dev/$ATAG "
 AUSED=`df -k | grep "$AFPATTERN" | tr -s " " | cut -f 3 -d " "`
 if [ ! "$AUSED" ];then
  MNTSTATUS='(not mounted)'
  mkdir -p /mnt/$ATAG
  mount -t $AFS /dev/$ATAG /mnt/$ATAG > /dev/null 2>&1
  if [ $? -eq 0 ];then
   AUSED=`df -k | grep "$AFPATTERN" | tr -s " " | cut -f 3 -d " "`
   umount /dev/$ATAG
  else
   continue
  fi
 fi
 
 #
 
 AFREE=`expr $ASIZE - $AUSED`
 nAUSED=`expr $BOXWIDTH \* $AUSED \/ $ASIZE` #normalise.
 [ $nAUSED -eq 0 ] && nAUSED=1
 #process size...
 if [ $AFREE -gt 1048576 ];then #1024*1024
  ONEFREE="`dc $AFREE 1048576 \/ p`"
  ONEFREE="`printf "%.1f" $ONEFREE`G"
 else
  if [ $AFREE -gt 99 ];then
   ONEFREE="`expr $AFREE \/ 1024`M"
  else
   ONEFREE="`dc $AFREE 1024 \/ p`"
   ONEFREE="`printf "%.1f" $ONEFREE`M"
  fi
 fi
 if [ $ASIZE -gt 1048576 ];then #1024*1024
  ONESIZE="`dc $ASIZE 1048576 \/ p`"
  ONESIZE="`printf "%.1f" $ONESIZE`G"
 else
  if [ $ASIZE -gt 99 ];then
   ONESIZE="`expr $ASIZE \/ 1024`M"
  else
   ONESIZE="`dc $ASIZE 1024 \/ p`"
   ONESIZE="`printf "%.1f" $ONESIZE`M"
  fi
 fi
 
 echo "  <rect
     style=\"fill:#00ff00;fill-opacity:1;fill-rule:evenodd;stroke-width:1.25;stroke:#0000ff;stroke-opacity:1;stroke-dasharray:none;\"
     width=\"${BOXWIDTH}\"
     height=\"31.0\"
     x=\"$XLEFT2\"
     y=\"${YBOX}\" />
  <rect
     style=\"fill:#ff0000;fill-rule:evenodd;stroke:none;stroke-width:0.820816;fill-opacity:1;\"
     width=\"${nAUSED}\"
     height=\"31.0\"
     x=\"${XLEFT2}\"
     y=\"${YBOX}\" />
" >> /tmp/partview_display_svg

 zATAG="`echo -n "$ATAG" | sed -e 's%mmcblk%mm%'`" #120723 reduce mmcblk0p1 to mm0p1

 echo  "<text
     xml:space=\"preserve\"
     style=\"fill:black;stroke:none;font-family:Nimbus Mono L;font-style:normal;font-weight:bold;font-size:22;fill-opacity:1;stroke-opacity:1;stroke-width:3pt;stroke-linejoin:miter;stroke-linecap:butt;text-anchor:start;writing-mode:lr;\"
     x=\"2\"
     y=\"$(($YBOX + 23))\">${zATAG}
  </text>
  <text
     xml:space=\"preserve\"
     style=\"fill:black;stroke:none;font-family:Nimbus Mono L;font-style:normal;font-weight:bold;font-size:22;fill-opacity:1;stroke-opacity:1;stroke-width:3pt;stroke-linejoin:miter;stroke-linecap:butt;text-anchor:start;writing-mode:lr;\"
     x=\"$(($XLEFT2 + 12))\"
     y=\"$(($YBOX + 23))\">${ONESIZE}
  </text>
  <text
     xml:space=\"preserve\"
     style=\"fill:black;stroke:none;font-family:Nimbus Mono L;font-style:normal;font-weight:bold;font-size:22;fill-opacity:1;stroke-opacity:1;stroke-width:3pt;stroke-linejoin:miter;stroke-linecap:butt;text-anchor:start;writing-mode:lr;\"
     x=\"$(($XLEFT2 + $BOXWIDTH + 6))\"
     y=\"$(($YBOX + 23))\">${ONEFREE}
  </text>" >> /tmp/partview_display_svg


 YBOX=`expr $YBOX + 40`

done

echo '</svg>' >> /tmp/partview_display_svg

kill $yPID

export MAIN_DIALOG="<window title=\"Partview\" icon-name=\"gtk-harddisk\">
 <vbox>
  <pixmap><input file>/tmp/partview_display_svg</input></pixmap>
  <hbox><button ok></button></hbox>
 </vbox>
</window>"
${GTKDIALOGEXE} --center --program=MAIN_DIALOG



###END###
