#!/bin/sh
#
# $Id:$
#-------------------------------------------------------------------------------
# ロケーション集計プログラム
# 一つのロケーションの次の状態を取得してXMLで返却します。
#
# 1. ストレージ利用量 / 最大利用可能量
#-------------------------------------------------------------------------------

#------------------
# set env
#------------------
if test -d "${TF_HOME}"; then
	MT_FUNCTION_HOME="${TF_HOME}/www/bin"
	MT_FUNCTION="${MT_FUNCTION_HOME}/mt-lib"
else
	if test -f "./mt-lib"; then
		MT_FUNCTION_HOME="."
	else
		MT_FUNCTION_HOME="/usr/local/teamfile/www/bin"
	fi
	MT_FUNCTION="${MT_FUNCTION_HOME}/mt-lib"
fi

if test ! -f ${MT_FUNCTION}; then
	echo "The configuration script \"${MT_FUNCTION}\" was missing. Please check it."
	exit 1
fi

. ${MT_FUNCTION}

#--------------------------------------
# Show usage
#--------------------------------------
usage() {
	echo "$0 -f locationfile -h"
	echo "    -f  : locationfile"
	echo "    -h  : Show this message"
	echo ""
}

# main

configfile=""

while getopts f:h OPT
do
	case $OPT in
		f)
			configfile=$OPTARG
			;;
		h)
			usage
			exit 0
			;;
		\?)
			usage
			exit 0
			;;
	esac
done

if [ -z $configfile ]; then
	usage
	exit 0
fi

if [ ! -e $configfile ]; then
	usage
	exit 0
fi


parse_locationconf $configfile

result=`${PSQL} -A -F" " -t -d ${ENV_TfDbDbname} -h ${ENV_TfDbHostname} -p ${ENV_TfDbHostport} -U ${ENV_TfDbUsername} -c "SELECT DKQT_MAX_ST_BI, pg_size_pretty(DKQT_MAX_ST_BI), DKQT_USED_ST_BI, pg_size_pretty(DKQT_USED_ST_BI) FROM DIVY_DISKQUOTA"` 

echo -n "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>"
echo -n "<summary>"

if [ $? -ne 0 ]; then
	echo -n "<result><failed/></result>"
	exit 1;
fi

max_st=`echo $result | cut -d ' ' -f 1`
max_st_pre=`echo $result | cut -d ' ' -f 2,3`
used_st=`echo $result | cut -d ' ' -f 4`
used_st_pre=`echo $result | cut -d ' ' -f 5,6`

echo -n "<result><success /></result>"
echo -n "<storage><max>$max_st</max><used>$used_st</used>"
echo -n "<max_pre>$max_st_pre</max_pre><used_pre>$used_st_pre</used_pre>"
echo -n "</storage>"
echo -n "</summary>"

