#!/bin/sh

DISABLED_MARK=/etc/vz/vzstats-disable
# vzstats disabled and no arguments provided -- exit now
test -f $DISABLED_MARK -a $# -eq 0 && exit 0

. /etc/vz/vzstats.conf
IDFILE=/etc/vz/.vzstats-uuid
VERSION=0.3.2
CURL='curl -sS --connect-timeout 3'
OPT_VIEW=

# Check that config is sufficient
set -u
echo $REP_DIR $SUBMIT_URL >/dev/null
# Check that scripts directory exist and is not empty
ls $REP_DIR/* >/dev/null || exit 1

usage() {
	cat << EOF
Usage:
	vzstats [option]

Options:
	--view		do not send a report, just see what would be send
	--disable	disable sending vzstats reports
	--enable	re-enable sending reports
	--status	output current configuration and status
	--help		see this help
EOF

	exit $1
}

while [ $# -gt 0 ]; do
	case $1 in
	--view)
		OPT_VIEW=yes
		shift
		;;
	--disable)
		if test -f $DISABLED_MARK; then
			echo "vzstats was already disabled"
			exit 0
		fi

		if touch $DISABLED_MARK; then
			echo "vzstats disabled -- no reports will be send"
			exit 0
		fi
		# Error creating file
		exit 1
		;;
	--enable)
		if ! test -f $DISABLED_MARK; then
			echo "vzstats was already enabled"
			exit 0
		fi
		if rm -f $DISABLED_MARK; then
			echo "vzstats re-enabled"
			echo "Please re-run without options to send a report"
			exit 0
		fi
		# Failed to rm mark
		exit 1
		;;
	--status)
		echo "Version:	$VERSION"
		echo -n "Status:		"
		if test -f $DISABLED_MARK; then
			echo "disabled"
		else
			echo "enabled"
		fi
		echo "Submit URL:	$SUBMIT_URL"
		echo -n "System UUID:	"
		if test -f $IDFILE; then
			cat $IDFILE
		else
			echo "(none)"
		fi
		echo "Reports dir:	$REP_DIR"
		exit 0
		;;
	-h|-?|--help)
		usage 0
		;;
	*)
		echo "ERROR: Unknown option $1" 1>&2
		usage 1
		;;
	esac
done

err() {
	echo "$*, aborting" 2>&1
	exit 1
}

check_id() {
	if ! echo $1 | grep -qiE '^\{?[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}?$'; then
		rm -f $IDFILE
		err "Invalid UUID $1"
	fi
}
test -f $DISABLED_MARK && err "vzstats disabled"

get_id() {
	local id

	if test -f $IDFILE; then
		id=$(cat $IDFILE)
	else
		# Check that IDFILE is writeable
		echo > $IDFILE || exit 1
		id=$(${CURL} -F magic_word=very-please $GET_ID_URL)
		echo $id > $IDFILE || exit 1
	fi

	check_id "$id"
	UUID=$id
}

# Autodiscover proxy
discover_proxy() {
	local proxy_line

	if [ ${http_proxy:-NOTSET} != "NOTSET" ]; then
		return
	fi

	if [ -r "/etc/wgetrc" ];then
		proxy_line=$(grep '^http_proxy' /etc/wgetrc | tr -d ' \t');
		if [ ! -z $proxy_line ]; then
			export $proxy_line
			return
		fi
	fi

	if [ -r "/etc/yum.conf" ];then
		proxy_line=$(grep '^proxy=' /etc/yum.conf);
		if [ ! -z $proxy_line ]; then
			proxy_line=http_$proxy_line
			export $proxy_line
			return
		fi
	fi
}

discover_proxy
get_id

OUTDIR=$(mktemp -d)
OUT=$(mktemp)
trap "rm -fr $OUTDIR $OUT" INT TERM EXIT
SCRIPTS=$(find $REP_DIR -maxdepth 1 -type f -user 0 -group 0 \
	-perm -u+rx -not -perm -g+w -not -perm -o+w \
	-print | sort)

if test -z "$SCRIPTS"; then
	err "No scripts found in $REP_DIR"
fi


LOG=collect.log
echo "== vzstats $VERSION ==" > $OUTDIR/$LOG
for S in $SCRIPTS; do
	NAME=$(basename $S)
	echo "== executing $S ==" >> $OUTDIR/$LOG
	$S > $OUTDIR/$NAME 2>>$OUTDIR/$LOG
done

if [ -n "$OPT_VIEW" ]; then
	# Just view, do not set report
	echo "View mode enabled -- not sending report to $SUBMIT_URL"
	echo "To review what information would be sent, see $OUTDIR"
	echo "Do not forget to remove it afterwards"
	trap "" INT TERM EXIT
	exit 0
fi
tar czf $OUT -C $OUTDIR . || exit 1
${CURL} -F uuid=$UUID -F file=@${OUT} $SUBMIT_URL
