#!/bin/sh
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/pkg/bin:/usr/pkg/sbin:/usr/local/bin:/usr/local/sbin:$PATH
export PATH

${DEBUG:="false"} && set -xu

# Gfarm installation directory
prefix="@prefix@"
sysconfdir="@sysconfdir@"
datarootdir="@datarootdir@"
datadir="@datadir@"
config_dir="${datadir}/gfarm/config"

PROGNAME=`basename $0`

BOURNE_SHELL="${BOURNE_SHELL-/bin/sh}"
if [ x"$1" = x--re-invoked ]; then
	# shell function works with this shell, remove --re-invoked option
	shift
elif "$BOURNE_SHELL" -c 'shellfunc() { exit 0; }; shellfunc' 2>/dev/null; then
	# shell function works
	:
else
	# Search other shell that supports shell functions
	for sh in ksh bash zsh sh ash bsh sh5; do
		set `IFS=:;
		 P="/bin:/usr/5bin:/usr/bin:/usr/local/bin:/usr/pkg/bin:$PATH";
		 echo $P`
		for dir
		do
			shell="$dir/$sh"
			if ( [ -f "$shell" ] || [ -f "$shell.exe" ] ) &&
				"$shell" -c 'shellfunc() { exit 0; };
					shellfunc' 2>/dev/null
			then
				BOURNE_SHELL="$shell" exec "$shell" "$0" \
					--re-invoked ${1+"$@"}
			fi
		done
	done
	echo "$PROGNAME: cannot find a shell which supports functions" >&2
	exit 1
fi

. $config_dir/config-gfarm.common

# iostat
. $config_dir/config-gfarm.iostat

usage()
{
	echo >&2 "usage: $PROGNAME [--help] [--prefix prefix] [-t|-T] [-f] [-S]"
	usage_iostat
	echo >&2 "	[--iostat-max-client iostat_max_client"\
			"(currentry $IOSTAT_MAX_CLIENT)]"
	exit 1
}


display_plain_params()
{
	echo "prefix [--prefix]: $CONFIG_PREFIX"
	echo "config file      : $IOSTAT_CONF"
	display_iostat_params gfsd
	if $IOSTAT_ENABLE; then
		echo "iostat max client  [--iostat-max-client]: $IOSTAT_MAX_CLIENT"
	fi
	exit 0
}

display_script_params()
{
	echo "CONFIG_PREFIX=$CONFIG_PREFIX"
	echo "IOSTAT_CONF=$IOSTAT_CONF"
	display_iostat_script_params gfsd
    if $IOSTAT_ENABLE; then
	echo "IOSTAT_MAX_CLIENT=$IOSTAT_MAX_CLIENT"
    fi
	exit 0
}

while [ $# -gt 0 ] ; do
	case $1 in

	# set parameters
	  --prefix) shift; [ $# -ge 1 ] || usage
		CONFIG_PREFIX=$1
		;;
	  --iostat*)
		key=$1
		shift; [ $# -ge 1 ] || usage
		iostat_args $key  $1
		;;

	# control options
	  --help)
		usage
		;;
	  -S)
		PRIVATE_MODE=true
		;;
	  -f)
		FORCE=true
		;;
	  -t)
		PARAMS_DISPLAY_STYLE=plain
		;;
	  -T)
		PARAMS_DISPLAY_STYLE=script
		;;
	  -*)
		usage
		;;
	esac
	shift
done

case $# in
0)	;;
*)	usage;;
esac

. $prefix/share/gfarm/config/config-gfarm.sysdep

#
# default values
#

: ${CONFIG_PREFIX:=}

# NOTE: $CONFIG_PREFIX needs to be set before calling sysdep_defaults
# sysdep_defaults must set: $RC_DIR
sysdep_defaults

: ${FORCE:=false}
: ${PRIVATE_MODE:=false}
: ${PARAMS_DISPLAY_STYLE:=none}

if [ X"$CONFIG_PREFIX" != X ]; then
	: ${GFARM_CONF_DIR:="$CONFIG_PREFIX/etc"}
else
	: ${GFARM_CONF_DIR:="$sysconfdir"}
fi
if $PRIVATE_MODE; then
	: ${IOSTAT_CONF:="$GFARM_CONF_DIR/gfsd.conf"}
else
	: ${IOSTAT_CONF:="$GFARM_CONF_DIR/gfarm2.conf"}
fi

# -t option; display parameters
[ $PARAMS_DISPLAY_STYLE != none ] && display_${PARAMS_DISPLAY_STYLE}_params

[ -f $IOSTAT_CONF ] || ABORT "$IOSTAT_CONF: no such file"

update_iostat gfsd

echo "$PROGNAME success"

exit 0
