#!/bin/ash
#Barry Kauler 2012
#GPL v3 (/usr/share/doc/legal)
#120202 internationalized. add udf f.s.
#131130 zigbert: gui (gtkdialog) improvements.
#150203 ASRI: add refresh button
#230928 radky: minor adjustments in script syntax and GUI presentation

export TEXTDOMAIN=partview
export OUTPUT_CHARSET=UTF-8

TMPDIR=/tmp/apps/partview
! [ -d $TMPDIR ] && mkdir -p $TMPDIR

/usr/lib/gtkdialog/box_splash -close never -fontsize large -text "$(gettext 'Please wait, probing drives...')" &
yPID=$!

#added stuff 01micko 2010-12-02
. /etc/DISTRO_SPECS
. /etc/rc.d/functions_x #fx_* functions

KERNEL="`uname -r` (`uname -m`)"
PUPVER="${DISTRO_NAME} ${DISTRO_VERSION}"

#================================================================

cd_capacity() {
	cdrom_id="$(/lib/udev/cdrom_id /dev/${1})" #ID_CDROM_* variables
	eval "$cdrom_id"
	while read i ; do
		case $i in
			"ID_CDROM_MEDIA_HDDVD"*) echo 15000000 ;;
			"ID_CDROM_MEDIA_BD"*)    echo 25000000 ;;
			"ID_CDROM_MEDIA_DVD_PLUS_RW_DL"*) echo 8500000 ;;
			"ID_CDROM_MEDIA_DVD_PLUS_R_DL"*)  echo 8500000 ;;
			"ID_CDROM_MEDIA_DVD"*) echo 4500000 ;;
			"ID_CDROM_MEDIA_CD"*)  echo 716800 ;;
		esac
	done <<EOF
$cdrom_id
EOF
}

svg_bar() {
	SVG_BAR_COLOR_USED='#006793' SVG_BAR_COLOR_TOTAL='#444444' SVG_BAR_COLOR_TEXT='#E5E5E5' SVG_BAR_HEIGHT=38

	echo '<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="'$SVG_BAR_HEIGHT'" width="'$1'">
  <defs>
	<linearGradient id="LGD_02">
	  <stop style="stop-color:'$SVG_BAR_COLOR_USED';stop-opacity:1" offset="0" />
	  <stop style="stop-color:'$SVG_BAR_COLOR_USED';stop-opacity:0" offset="1" />
	</linearGradient>
	<linearGradient id="LG_02"
	   x1="'$(($2-3))'" y1="10" x2="'$2'" y2="10"
	   xlink:href="#LGD_02"
	   gradientUnits="userSpaceOnUse" />
  </defs>
  <rect style="fill:'$SVG_BAR_COLOR_TOTAL';stroke:#111111;stroke-width:3" width="'$1'" height="'$SVG_BAR_HEIGHT'" x="0" y="0"/>
  <rect style="fill:url(#LG_02);" width="'$(($1-3))'" height="'$(($SVG_BAR_HEIGHT-3))'" x="1.5" y="1.5"/>
  <path style="fill:none;stroke:#ffffff;stroke-width:3" d="M 0,'$SVG_BAR_HEIGHT' '$1','$SVG_BAR_HEIGHT' M '$1',0 '$1','$SVG_BAR_HEIGHT'"/>
  <text
	 style="fill:'$SVG_BAR_COLOR_TEXT';font-family:sans-serif;font-size:'$(($SVG_BAR_HEIGHT/2))';text-anchor:middle"
	 x="'$(($1/2))'" y="'$(($SVG_BAR_HEIGHT*17/24))'">
	'$3'
  </text>
</svg>'
}

#================================================================

PARTSLIST="`probepart -k -extra-info`"

COUNT=0
S='<window title="Partview" icon-name="gtk-harddisk">
 <vbox space-expand="true" space-fill="true">
   '"`/usr/lib/gtkdialog/xml_info fixed "partition.svg" 70 "<b><span size='"'x-large'"'>Partview</span></b>" "$PUPVER" "Linux: $KERNEL"`"' 
 <vbox space-expand="true" space-fill="true">
   <vbox scrollable="true" border-width="10">'

while IFS="|" read -r ADEVNAME AFS ASIZE ALABEL AMNT_STATUS
do
	MNTSTATUS='(currently mounted)'
	ADEVNAME="${ADEVNAME#/dev/}" #remove leading '/dev/'
	AUSED=
	#--
	if fx_drv_is_optical $ADEVNAME ; then
		AUSED=$ASIZE
		ASIZE=$(cd_capacity $ADEVNAME)
		if [ "$AFS" = "none" ] ; then
			AUSED=0
		fi
	else
		case $AFS in none|swap) AUSED=0 ;; esac
	fi
	if [ "$ASIZE" = "0" ] ; then
		AUSED=0
	fi
	#--
	if [ "$AUSED" = "" ] ; then
		AFPATTERN="^/dev/$ADEVNAME "
		AUSED=`df -k | grep "$AFPATTERN" | head -n 1 | tr -s " " | cut -f 3 -d " "`
		if [ "$AUSED" ] && [ "$(echo -n "$AFPATTERN" | grep \/luks)" != "" ] ; then
			ADEVNAME="${ADEVNAME#mapper/}"
		fi
		if [ ! "$AUSED" ];then
			MNTSTATUS='(not mounted)'
			mkdir -p /mnt/$ADEVNAME
			mount -t $AFS /dev/$ADEVNAME /mnt/$ADEVNAME > /dev/null 2>&1 || continue
			AUSED=`df -k /mnt/$ADEVNAME | grep "$AFPATTERN" | head -n 1 | tr -s " " | cut -f 3 -d " "`
			umount /dev/$ADEVNAME
		fi
	fi
	#--
	if [ "$ASIZE" = "0" -o "$AFS" = "none" -o "$AFS" = "swap" ] ; then
		ATAG="<b>$ADEVNAME</b> [${AFS}]"
	else
		ATAG="<b>$ADEVNAME</b> [${AFS}]"
	fi
	#--
	if [ "$ALABEL" ] ; then
		ALABEL="(${ALABEL})"
	fi
	#--
	AFREE=$(($ASIZE - $AUSED))
	#process size...
	ONEFREE=$( fx_format_bytes $((1024 * $AFREE)) )
	ONESIZE=$( fx_format_bytes $((1024 * $ASIZE)) )
	W=320
	svg_bar ${W} "$(($AUSED*${W}/$ASIZE))" "${ONESIZE} / ${ONEFREE} $(gettext 'free')"  > ${TMPDIR}/partview_${COUNT}.svg
	S=$S'
 <hbox>
  <vbox homogeneous="true">
    <text space-expand="false" space-fill="false" use-markup="true"><label>"'"${ALABEL}     ${ATAG}"'"</label></text>
  </vbox>
  <vbox>
    <pixmap space-expand="false" space-fill="true"><input file>'${TMPDIR}'/partview_'${COUNT}'.svg</input></pixmap>
  </vbox>
 </hbox>'
 COUNT=$(($COUNT+1))
done <<EOF
$PARTSLIST
EOF

export PartView=$S'</vbox></vbox>
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Refresh")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon refresh`"'
      <action>partview &</action>
      <action>exit:QUIT</action>
    </button>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Ok")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <action>exit:OK</action>
    </button>
    '"`/usr/lib/gtkdialog/xml_scalegrip`"'
  </hbox>
</vbox>
</window>'

#echo "$PartView" > ${TMPDIR}/partview.xml #debug
. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
kill $yPID
gtkdialog -p PartView --geometry=700x475 --styles=/tmp/gtkrc_xml_info.css

###END###
