#!/bin/bash
################################################################################
#
#    HPCC SYSTEMS software Copyright (C) 2019 HPCC Systems®.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#################################################################################

declare -a arr1
declare -a arr2
declare -a arrDiscPath

if [ "${1:0:1}" == '-' ]; then
    if [ $# -lt 1 ]; then
        echo usage: $0 -d=DiskPath[,DiskPath,...]
        exit 1
    fi

    for (( i=1;$i<=$#;i=$i+1 )) do
        case ${!i:0:3} in '-d=')
                 IFS=, read -a arrDiscPath <<< "${!i:3}";;
            *)
                 echo "wrong augument:${!i}";;
        esac
    done

    echo "--Start--"
    if [ "${#arrDiscPath[@]}" ]; then
        index=0
        for i in "${arrDiscPath[@]}"; do
            ok=0
            dirp=$i
            until [ $ok = 1 ] ; do
                if [ -d $dirp ] ; then
                    ok=1
                else
                    dirp=$(dirname $dirp)
                fi
            done

            if [ "$dirp" != "/" ] ; then
                dfout=$(df $dirp | tail -n +2 | awk '{if(NF>=4) print $(NF-3) " " $(NF-2)}')
                if [ -n "$dfout" ] ; then
                    arr1[$index]=$(echo $dfout | awk '{print $1}')
                    arr2[$index]=$(echo $dfout | awk '{print $2}')
                    if [ "$dirp" != "$i" ] ; then
                        echo "$i: ${arr1[$index]} ${arr2[$index]} $dirp"
                    else
                        echo "$i: ${arr1[$index]} ${arr2[$index]}"
                    fi
                fi
            fi

            index=$((index+1))
        done
    fi

    echo "--Finish--"
    exit 0
fi
