#!/bin/bash -x 
if [ -z "$1" -o -z "$2" ]; then
    echo "missing parameter(s)"
    echo "$0: name size"
    exit 1
fi

name="$1"
size="$2"

if [ -e /tmp/ss-mount-chroots-$name ]; then
    echo "uh oh, can't write new size"
    exit 1
fi

mount | grep -qi chroots/ss-$name
if [ $? -eq 0 ]; then
    echo "uh oh, chroots/ss-$name already mounted"
    exit 1
fi

olde_size=$( lvscan | grep "chroots/$name'" | grep -i mb | cut -d[ -f2 | cut -d. -f1 | tr -d $'\n')
if [ -z "$olde_size" ]; then
    echo "unable to determine chroots/$name size"
    exit 1
fi
new_size=$( echo "$size + $olde_size" | bc )

lvresize -v --size +$size chroots/$name
if [ $? -gt 0 ]; then
    echo "problems with lvresize"
    exit 1
fi

lvcreate -v --snapshot --size ${new_size}M --name ss-$name chroots/$name
if [ $? -gt 0 ]; then
    echo "problems with lvcreate"
    exit 1
fi

echo $size > /tmp/ss-mount-chroots-$name
chmod 600 /tmp/ss-mount-chroots-$name

resize2fs /dev/chroots/ss-$name ${new_size}M
if [ $? -gt 0 ]; then
    echo "problems with resize2fs"
    exit 1
fi

mount /dev/chroots/ss-$name /chroot/ss-$name
if [ $? -gt 0 ]; then
    echo "problems with mount"
    exit 1
fi

#xfs_growfs /chroot/ss-$name
#if [ $? -gt 0 ]; then
#    echo "problems with xfs_growfs"
#    exit 1
#fi
