#!/bin/sh

set -e
set -u

REFILE_AND_GROK_PARALLEL="1"
if [ -e "/etc/default/dsc-statistics-presenter" ]; then
 . /etc/default/dsc-statistics-presenter
fi

usage() {
cat <<EOF
$0 - pull and process dsc data
        Options:
        -h|--help - Show this message
        -R|--refile - Do the actual refile (internal use only)
EOF
}

do_refile() {
	cd $1
	$EXECDIR/dsc-xml-extractor >dsc-xml-extractor.out 2>&1
}

## Parse commandline
TEMP=$(getopt -n refile-and-grok \
        -l ,help,refile: -- \
        +hR: "$@")

if test "$?" != 0; then
        echo "Terminating..." >&2
        exit 1
fi


eval set -- ${TEMP}
REFILE=""
while test "$1" != "--"; do
        case $1 in
                -h|--help)
                        usage
                        exit 0
                ;;
                -R|--refile)
                        shift
                        REFILE="$1"
                ;;
         esac
         shift
done
shift

if [ -n "$REFILE" ]; then
  do_refile $REFILE
  exit 0
fi

me="${me:-$0}"
PROG=`basename $0`
cd /var/lib/dsc-statistics/data
exec >$PROG.stdout
#exec 2>&1
#set -x
date

EXECDIR=/usr/share/dsc-statistics-presenter
export EXECDIR SERVER NODE

NODEDIRS="$(for SERVER in * ; do
	test -L $SERVER && continue;
	test -d $SERVER || continue;
	cd $SERVER
	for NODE in * ; do
		test -L $NODE && continue;
		test -d $NODE || continue;
		echo "$SERVER/$NODE"
	done
	cd ..	# SERVER
done)"

echo me $me
echo $NODEDIRS | xargs --no-run-if-empty --max-args=1 --max-procs=$REFILE_AND_GROK_PARALLEL $me --refile

wait
date
