#! /bin/bash

##- Resizes an existing vServer
##: vserver size
##+ vserver  Name of the vServer which is to be resized
##+ size     New size or increment prefixed with +/-

[ "${__vsa_bindir}" ] || exec "$(dirname "$0")/vserver-admin" resize "$@"
[[ $# -eq 2 ]] || __vsa_usage_and_exit

__vsa_run_locked
__vsa_config "$1"

# It makes no sense resizing volumes when we are not running on volumes!
#
[ "${cfg_lvm_group}" ] || __vsa_die "Action not supported with LVM disabled"


guess_direction ()
{
	__vsa_die "Resizing by specifying the size directly is not yet supported"
}


case $2 in
	-* ) direction=shrink ;;
	+* ) direction=grow   ;;
	*  ) direction=$(guess_direction) ;;
esac

if [ "${cfg_lvm_volume_resize_hook}" ] && [[ ${direction} = shrink ]] ; then
	eval __vsa_do "${cfg_lvm_volume_resize_hook}"
fi

__vsa_do lvresize -L "$2" "${cfg_lvm_group}/${cfg_lvm_volume}"

if [ "${cfg_lvm_volume_resize_hook}" ] && [[ ${direction} = grow ]] ; then
	eval __vsa_do "${cfg_lvm_volume_resize_hook}"
fi

