#! /bin/bash

#    qtparted - a frontend to libparted for manipulating disk partitions
#    Copyright (C) 2002-2003 Vanni Brutto
#
#    Vanni Brutto <zanac (-at-) libero dot it>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#about qparted_df
#
# this script is used to get how space is used in a device... it simple use "df" :)


function getMB() {
    MB="$(df -P $1 | tail -n 1 | awk '{print $3}' | egrep "[0-9]")"

    if [ -z "$MB" ]; then
        exit 0
    fi
    echo $MB
}

DF=$(which df)

#test if "df" is installed
if [ -z $DF ]; then
    exit 0
fi

#test if a parameter was passed from command line
if [ -z "$1" ]; then
    echo "$0: missing argument"
    echo "Usage: $0 /dev/device"
    exit 0
fi

#the device is mounted?
TESTDF="$(df -P $1 | grep $1)"

if [ -z "$TESTDF" ]; then
    #no, the device is not mounted, so we must mount it!
    MNTPOINT="/tmp/qtp/$1"
    mkdir -p $MNTPOINT
    mount $1 $MNTPOINT -o ro && getMB $1
    umount $1
    exit 0
fi

getMB $1

exit 0
