#!/bin/ksh
#
# AUTHOR : UNKNOWN :-)
#

#awk 'BEGIN{for(c=0;c<50;c++) printf "-"; printf "\n"}'
#set -x

# Print 20 dashes '-'
#awk 'BEGIN{while (a++<20) s=s "-"; print s}'
#x=`awk 'BEGIN {while (a++<20) s=s "-"; print s}'`
#awk 'BEGIN{for(c=0;c<50;c++) printf "-"; printf "\n"}'

##############
# INITIALIZE #
##############
start=`date`
os=`uname`
processid=$$

tmp_header="/tmp/df_${processid}.header"
tmp_dashes="/tmp/df_${processid}.dashes"
tmp_00="/tmp/df_${processid}.00"
tmp_01="/tmp/df_${processid}.01"
tmp_02="/tmp/df_${processid}.02"
tmp_03="/tmp/df_${processid}.03"
tmp_04="/tmp/df_${processid}.04"
tmp_05="/tmp/df_${processid}.05"

#################
# Parse getopts #
#################

while getopts :f:kmg size_unit
do
   #echo "size_unit=${size_unit}"
   case $size_unit in
      "k") size="KB"
         header="Filesystem KBytes Used Avail Capacity Mount"
         #echo "${header}" > ${tmp_header}
         ;;
      "m") size="MB"
         header="Filesystem MBytes Used Avail Capacity Mount"
         #echo "${header}" > ${tmp_header}
         ;;
      "g") size="GB"
         header="Filesystem GBytes Used Avail Capacity Mount"
         #echo "${header}" > ${tmp_header}
         ;;
      "f") filesystem=${OPTARG}
         ;;
      #"e") size="GB"
      #   header="Filesystem GBytes Used Avail Capacity Mount"
      #   estimate="yes"
      #   #echo "${header}" > ${tmp_header}
      #   ;;
      *) size="GB"
         header="Filesystem GBytes Used Avail Capacity Mount"
         #echo "${header}" > ${tmp_header}
         ;;
   esac
done

if [[ -z "${size}" ]] ; then
   size="GB"
   header="Filesystem GBytes Used Avail Capacity Mount"
   #echo "${header}" > ${tmp_header}
fi

#echo "!!!size=${size}"
#sleep 600000000000

#################
# format_output #
#################
format_output()
{
   #sort ${tmp_00} > ${tmp_01}

   f1_max=`awk '{ print length($1) }' ${tmp_00} | sort -nr | head -1`
   f2_max=`awk '{ print length($2) }' ${tmp_00} | sort -nr | head -1`
   f3_max=`awk '{ print length($3) }' ${tmp_00} | sort -nr | head -1`
   f4_max=`awk '{ print length($4) }' ${tmp_00} | sort -nr | head -1`
   f5_max=`awk '{ print length($5) }' ${tmp_00} | sort -nr | head -1`
   f6_max=`awk '{ print length($6) }' ${tmp_00} | sort -nr | head -1`

   num_lines=`wc -l ${tmp_00} | awk '{ print $1-1}'`
   #awk 'BEGIN{for(c=0;c<50;c++) printf "-"; printf "\n"}'
   tail -${num_lines} ${tmp_00} | sort > ${tmp_01}
   #cat ${tmp_01}

   echo ""
   #echo "/+ ------------------------------------------------------------------------ +/"
   #echo ""

   #awk 'BEGIN{for(c=0;c<50;c++) printf "-"; printf "\n"}'
   cat /dev/null > ${tmp_dashes}
   dashes=""
   f1_dash=`awk 'BEGIN { for ( c=0 ; c<'${f1_max}' ; c++ ) printf "-"; exit }'`
   f2_dash=`awk 'BEGIN { for ( c=0 ; c<'${f2_max}' ; c++ ) printf "-"; exit }'`
   f3_dash=`awk 'BEGIN { for ( c=0 ; c<'${f3_max}' ; c++ ) printf "-"; exit }'`
   f4_dash=`awk 'BEGIN { for ( c=0 ; c<'${f4_max}' ; c++ ) printf "-"; exit }'`
   f5_dash=`awk 'BEGIN { for ( c=0 ; c<'${f5_max}' ; c++ ) printf "-"; exit }'`
   f6_dash=`awk 'BEGIN { for ( c=0 ; c<'${f6_max}' ; c++ ) printf "-"; exit }'`
   echo "${f1_dash} ${f2_dash} ${f3_dash} ${f4_dash} ${f5_dash} ${f6_dash}" >> ${tmp_dashes}

   awk '{ printf "%-'${f1_max}'s  %'${f2_max}'s %'${f3_max}'s %'${f4_max}'s %'${f5_max}'s %-'${f6_max}'s \n", $1, $2, $3, $4, $5, $6 }' ${tmp_header}
   awk '{ printf "%-'${f1_max}'s  %'${f2_max}'s %'${f3_max}'s %'${f4_max}'s %'${f5_max}'s %-'${f6_max}'s \n", $1, $2, $3, $4, $5, $6 }' ${tmp_dashes}
   awk '{ printf "%-'${f1_max}'s  %'${f2_max}'s %'${f3_max}'s %'${f4_max}'s %'${f5_max}'s %-'${f6_max}'s \n", $1, $2, $3, $4, $5, $6 }' ${tmp_01}

   echo
}

##########
# run_df #
##########
run_df()
{
   #if [[ "${size_unit}" == "?" ]] ; then
   #   size="GB"
   #   header="Filesystem GBytes Used Avail Capacity Mount"
   #   echo "${header}" > ${tmp_header}
   #fi

   if [[ ! -z "${filesystem}" ]] ; then
      df -k ${filesystem}		> ${tmp_00}
   else
      df -k 				> ${tmp_00}
   fi

   df_lines=`wc -l ${tmp_00} | awk '{ print $1 }'`
   let df_lines=$df_lines-1
   tail -${df_lines} ${tmp_00} | sort 				> ${tmp_01}
}

#########
# round #
#########
round()
{
   n1=`echo ${1} | cut -f1 -d.`
   n2=`echo ${1} | cut -f2 -d. | cut -c1`

   #echo "n1 = ${n1}"
   #echo "n2 = ${n2}"

   if [[ ${n2} > 4 ]] ; then
      let n1=${n1}+1
   fi

   echo ${n1}
}

############
# round_df #
############
round_df()
{
   #aklss901(oracle)[]/nas_mnt/common/dba_utils/tmp/staging/scripts$: df -k /tmp
   #Filesystem            kbytes    used   avail capacity  Mounted on
   #swap                 8711120   58536 8652584     1%    /tmp
   #aklss901(oracle)[]/nas_mnt/common/dba_utils/tmp/staging/scripts$:
   #awk '{ print $1 " " $2/1024 " " $3/1024 " " $4/1024 " " $5 " " $6 }' ${tmp_01} > ${tmp_02}

   while read dfline
   do
      f1=`echo ${dfline} | awk '{ print $1 }'`
      f2=`echo ${dfline} | awk '{ print $2 }'`
      f3=`echo ${dfline} | awk '{ print $3 }'`
      f4=`echo ${dfline} | awk '{ print $4 }'`
      f5=`echo ${dfline} | awk '{ print $5 }'`
      f6=`echo ${dfline} | awk '{ print $6 }'`
   
      f2="`round $f2`-${2}"
      f3="`round $f3`-${2}"
      f4="`round $f4`-${2}"

      echo "${f1} ${f2} ${f3} ${f4} ${f5} ${f6}" >> ${tmp_03}
   done < ${1}
}

################
# convert_unit #
################
convert_unit()
{
   echo "${header}" > ${tmp_header}
   case ${size} in
      "KB" )
         awk '{ print $1 " " $2 " " $3 " " $4 " " $5 " " $6 }' ${tmp_01} > ${tmp_02}
         cat /dev/null > ${tmp_03}
         round_df ${tmp_02} ${size}
         cat ${tmp_header} ${tmp_03} > ${tmp_00}
         #cat ${tmp_00}
         ;;
      "MB" )
         awk '{ print $1 " " $2/1024 " " $3/1024 " " $4/1024 " " $5 " " $6 }' ${tmp_01} > ${tmp_02}
         cat /dev/null > ${tmp_03}
         round_df ${tmp_02} ${size}
         cat ${tmp_header} ${tmp_03} > ${tmp_00}
         #cat ${tmp_00}
         ;;
      "GB" )
         awk '{ print $1 " " $2/1024/1024 " " $3/1024/1024 " " $4/1024/1024 " " $5 " " $6 }' ${tmp_01} > ${tmp_02}
         cat /dev/null > ${tmp_03}
         round_df ${tmp_02} ${size}
         cat ${tmp_header} ${tmp_03} > ${tmp_00}
         #cat ${tmp_00}
         ;;
   esac
}

###########
# cleanup #
###########
cleanup()
{
   #echo
   #echo "====================================="
   #echo
   #echo "filesystem = ${filesystem}"
   #echo "oss = ${oss}"
   #echo

   #ls -l /tmp/df_*${process_id}*
   rm /tmp/df_*${process_id}*
}

########
# MAIN #
########

run_df
convert_unit
format_output

cleanup

end=`date`
print "START = ${start}"
print "END   = ${end}"

exit 0

###########
# THE END #
###########

