#!/bin/sh
#Barry Kauler 2009. A first go, not very efficient generating gif images.
#v431 clarf: bugfix, old kernel without libata drivers.

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

PARTINFO="<hbox><text><label>Free</label></text><pixmap><input file>/tmp/partview_top.gif</input></pixmap></hbox>"

if [ ! -f /tmp/partview_top.gif ];then
 ppmrough -width 100 -height 16 -var 1 -bg red -fg grey80 -left 0 | ppmlabel -background transparent -color red -size 10 -y 13 -x 0 -text "Used" -background transparent -color red -size 10 -y 13 -x 1 -text "Used" | ppmtogif > /tmp/partview_top.gif
fi

xmessage -center -bg orange -buttons "" "Please wait, processing..." &
XPID=$!

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
 #[ "$AFS" ] && echo "${ATAG} \"Filesystem: $AFS  Size: ${ASIZE}M  Free: ${AFREE}M ${MNTSTATUS}\" \\" >> /tmp/schoices.txt #v3.01 added precaution.
 AFREE=`expr $ASIZE - $AUSED`
 nAUSED=`expr 100 \* $AUSED \/ $ASIZE` #normalise.
 #process size...
 if [ $AFREE -gt 1048576 ];then #1024*1024
  ONESIZE="`dc $AFREE 1048576 \/ p`"
  ONESIZE="`printf "%.1f" $ONESIZE`G"
 else
  if [ $AFREE -gt 99 ];then
   ONESIZE="`expr $AFREE \/ 1024`M"
  else
   ONESIZE="`dc $AFREE 1024 \/ p`"
   ONESIZE="`printf "%.1f" $AFREE`M"
  fi
 fi
 if [ ! -f /tmp/partview_used_${ATAG} ];then
  echo "$ONESIZE" > /tmp/partview_used_${ATAG}_old
  echo "$ONESIZE" > /tmp/partview_used_${ATAG}
 else
  cp -f /tmp/partview_used_${ATAG} /tmp/partview_used_${ATAG}_old
  echo "$ONESIZE" > /tmp/partview_used_${ATAG}
 fi
 pvDIFFER="`diff -q /tmp/partview_used_${ATAG} /tmp/partview_used_${ATAG}_old`"
 if [ "$pvDIFFER" != "" -o ! -f /tmp/partview_${ATAG}.gif ];then #saves time.
  #generate bar graph...
  ppmrough -width 100 -height 14 -var 1 -bg red -fg grey80 -left ${nAUSED} | ppmtogif > /tmp/partview_${ATAG}.gif
 fi
 #add to gui...
 PARTINFO="${PARTINFO}
<hbox><text><label>${ATAG} ${ONESIZE}</label></text><pixmap><input file>/tmp/partview_${ATAG}.gif</input></pixmap></hbox>"
 #echo "ATAG=$ATAG ASIZE=$ASIZE AFREE=$AFREE"
done

kill $XPID

export MAIN_DIALOG="<window title=\"Partview\" icon-name=\"gtk-harddisk\">
 <vbox>
  ${PARTINFO}
 </vbox>
</window>"


gtkdialog3 --program=MAIN_DIALOG
