#!/bin/sh -ef

PROFILE_DIR='/etc/spt/profiles/ovz'
TEMPLATE_DIR='/var/lib/vz/template/cache'
BUILD_DIR='/var/lib/vz/template/tmp'
LOG_DIR='/var/lib/vz/template/logs'

TEMPLATE_TOOL='/usr/sbin/vecache'
BUILD_USER='_vebuilder'

_()
{
	LANG="${in_language%%;*}.utf8" gettext 'alterator-ovz' "$1"
}

. /usr/share/alterator/build/backend3.sh

alt_list_profiles()
{
   	find "$PROFILE_DIR/" -mindepth 1 -maxdepth 1 -type d  ! -name '.default' -printf '("%f")\n'
}

alt_list_complevels()
{
		for i in best fast none; do echo "( \"$i\" )"; done
}

alt_list_templates()
{
	local i
	for i in $(find "$TEMPLATE_DIR/" -maxdepth 1 -type f -printf '%f\n'); do
		i="${i#altlinux-}"
		i="${i%.gz}"
		i="${i%.tar}"
		[ -z "$i" ] ||
			printf '("%s")\n' "$i"
	done
}

alt_list_logs()
{
	local i
	for i in $(find "$LOG_DIR/" -maxdepth 1 -type f -printf '%f\n'); do
		i="${i#altlinux-}"
		i="${i%.log}"
		[ -z "$i" ] ||
			printf '("%s")\n' "$i"
	done
}

alt_list()
{
	echo '('
	case "$in__objects" in
		templates)
			{ alt_list_logs; alt_list_templates; } | sort -u
			;;
		profiles)
			alt_list_profiles
			;;
		complevels)
			alt_list_complevels
			;;
	esac
	echo ')'
}

alt_read()
{
	case "$in__objects" in
		"${in__objects#*/}")
			echo '('
			if [ -f "$LOG_DIR/$in__objects" ];then
				printf 'detailedstatus "%s"' "$(simple_quote < "$LOG_DIR/$in__objects")"
				printf 'status "%s"' "$(grep "^vecache:" "$LOG_DIR/$in__objects"|tail -n 1|sed 's,^vecache:,,')"
			else
			        printf 'detailedstatus ""'
			        printf 'status ""'
			fi
			echo ')'
			;;
		/)
			echo '()'
			;;
		*)
			printf '(error "%s")' "`_ "Invalid object name"`"
			;;
	esac
}

alt_new()
{
	if [ -n "$(find "$TEMPLATE_DIR" -type f -name "altlinux-$in_profile.tar*")" ]; then
		printf '(error"%s")' "`_ "Same template already exists"`"
	elif find "$BUILD_DIR/" -type f -name pid |fgrep -qs "lockdir/pid"; then
		printf '(error"%s")' "`_ "Template build process already exists"`"
	fi

	case "$in_compression" in
	best) compression="--type tgz --gzip 9";;
	fast) compression="--type tgz --gzip 1";;
	none) compression="--type tar";;
	esac

	/usr/sbin/gen-apt-hook "$PROFILE_DIR/$in_profile" && \
		su -s /bin/sh -l "$BUILD_USER" -c \
		"$TEMPLATE_TOOL \
			--profile $in_profile \
			--build altlinux-$in_profile \
			$compression \
			> $LOG_DIR/$in_profile 2>&1" &

	for i in `seq 1 15`; do
		[ -s "$LOG_DIR/$in_profile" ] && break
		sleep 1
	done
	echo '()'
}

on_message()
{
	case "$in_action" in
		constraints)
			echo '('
			local required
			[ "$in_orig_action" = "new" ] &&
				required='#t' ||
				required='#f'
			printf ' profile (label "%s")' "`_ "Profile name"`"
			printf ' compression (label "%s")' "`_ "Compression"`"
			printf ' detailedstatus (label "%s")' "`_ "Detailed log:"`"
			printf ' status (label "%s")' "`_ "Template creation status"`"
			echo ')'
			;;
		list)
			alt_list
			;;
		write)
			echo '()'
			;;
		read)
			alt_read
			;;
		new)
			alt_new
			;;
		delete)
			for i in "${in__objects##*/}" "altlinux-${in__objects##*/}"; do
				rm -f -- "$TEMPLATE_DIR/$i.tar" "$TEMPLATE_DIR/$i.tar.gz" "$LOG_DIR/$i"
			done
			echo '()'
			;;
		*)
			echo '#f'
			;;
	esac
}

message_loop
