#!/bin/sh
# Copyright (c) 2008-2008 Synology Inc. All rights reserved.

Usage() {
	echo "Copyright (c) 2008-2008 Synology Inc. All rights reserved."
	echo ""
	echo " Usage: `basename $0` vol_id"
	echo ""
	if [ -z $1 ]; then
		echo "   vol_id: The id of targeted volume for snapshot removal."
	else	
		echo "ERROR: $1 is not a valid parameter. Please run again."
	fi
	exit 1
}

YNConfirm() {
	read YN
	if [ "x$YN" = "xYES" ]; then
		return
	elif [ "x$YN" = "xNO" ]; then
		exit 0
	fi
	echo -n "Please enter 'YES' or 'NO': "
	YNConfirm
}

SZF_SMBCONF="/usr/syno/etc/smb.conf"
SZF_TMP_SMBCONF1="/tmp/smb.conf.1"
SZF_TMP_SMBCONF2="/tmp/smb.conf.2"
share_section="^[[].*]"
# $1: the line number of share section to remove
RemoveShare() {
	# cut the file into two by the line:$1
	total=`cat $SZF_SMBCONF | wc -l`
	front=$(($1 - 1))
	rest=$(($total - $1))
	head -$front $SZF_SMBCONF > $SZF_TMP_SMBCONF1
	tail -$rest $SZF_SMBCONF > $SZF_TMP_SMBCONF2
	# get the rest share section from second file and put them into first
	next=`grep -n $share_section $SZF_TMP_SMBCONF2 | head -1 | cut -d: -f1`
	if [ "x$next" != "x" ]; then
		rest=$(($rest - $next + 1))
		tail -$rest $SZF_TMP_SMBCONF2 >> $SZF_TMP_SMBCONF1
	fi
	mv $SZF_TMP_SMBCONF1 $SZF_SMBCONF
	rm -f $SZF_TMP_SMBCONF2
}

# Check parameters
if [ -z "$1" -o "x$1" = "x-h" ]; then
	Usage
else
	Err=`echo "$1" | sed 's/[0-9]//g'`
	if [ "x$Err" != "x" ]; then
		Usage $1
	fi
fi

volid=$1
vol="/volume$volid"
vg="vg$volid"
lv="/dev/$vg/lv"

grep "$lv $vol " /proc/mounts > /dev/null
if [ $? != 0 ]; then
	echo "ERROR: $vol is not a valid lvm volume. Please run again."
	exit 1
fi

vol_snap="$vol.snap_.*"
sids=`grep $vol_snap /proc/mounts | cut -d" " -f2 | cut -d_ -f2`
if [ -z "$sids" ]; then
	echo "ERROR: There is no snapshot created for $vol."
	exit 0
fi

echo "Snapshot id list of $vol:"
snap_num=0
for s in $sids ; do
	snap_num=$(($snap_num + 1))
	echo "   $snap_num: $s"
done

if [ $snap_num -eq 1 ]; then
	snap_id=$sids
else
	while [ 1 == 1 ]; do
		echo -n "Please select ONE snapshot id from below listed choice 1~$snap_num: "
		read snap_choice
		Err=`echo "$snap_choice" | sed 's/[0-9]//g'`
		if [ "x$snap_choice" == "x" -o "x$Err" != "x" ]; then
			continue
		elif [ $snap_choice -le $snap_num -a $snap_choice -gt 0 ]; then
			break
		fi
	done
	snap_num=0
	for s in $sids ; do
		snap_num=$(($snap_num + 1))
		if [ $snap_num -eq $snap_choice ]; then
			snap_id=$s
			break
		fi
	done
fi

vol_snap="$vol.snap_${snap_id}"
snap_lv="/dev/$vg/lvs${snap_id}"

grep "$snap_lv $vol_snap " /proc/mounts > /dev/null 
if [ $? != 0 ]; then
	echo "ERROR: $vol_snap is not a valid snapshot. Please run again."
	exit 1
fi
cwd="/`pwd | cut -d/ -f2`"
if [ $cwd = $vol_snap ]; then
	echo "ERROR: Please exit $vol_snap and try again."
	exit 1
fi

echo -n "WARNING: Are you sure you want to remove $vol_snap? [YES|NO] "
YNConfirm
echo -n "Please confirm again: [YES|NO] "
YNConfirm

echo "#umount $vol_snap"
umount $vol_snap
if [ $? -ne 0 ]; then
	echo "ERROR: Failed to umount $vol_snap."
	exit 1
fi
rmdir $vol_snap

echo "#lvremove -f $snap_lv"
lvremove -f $snap_lv
if [ $? -ne 0 ]; then
	echo "ERROR: Failed to remove snapshot(${snap_id}) of $vol."
	exit 1
fi

cp $SZF_SMBCONF $SZF_SMBCONF.bak
while [ 1 == 1 ]; do
	share=`grep $vol_snap $SZF_SMBCONF | head -1 | cut -d"=" -f2 | cut -d"/" -f3`
	if [ -z "$share" ];then 
		break
	fi
	echo "#Snapshot of the shared folder $share has been removed."
	line=`grep -n $share_section $SZF_SMBCONF | grep "$share.$snap_id" | cut -d: -f1`
	RemoveShare $line
done

grep -v "$snap_lv $vol_snap" /etc/fstab > /tmp/fstab.tmp
if [ $? = 0 ]; then
	mv /tmp/fstab.tmp /etc/fstab
fi

