#!/bin/bash
logger -p local0.info "`who -m | cut -d\( -f2 |cut -d\) -f1`;excute uninstall_vol.sh starting"

umount_dir()
{
    local i
    dir_list=(`df -h | grep vg_appdata | awk '{print $6}' | sort -r`)
    len=${#dir_list[@]}
    for  ((i=0; i<len; i++))
    do
        echo ${dir_list[$i]}
        kill_process ${dir_list[$i]}
        umount ${dir_list[$i]}
		if [ $? -ne 0 ];then
		    kill_process ${dir_list[$i]}
		    fuser -m -k ${dir_list[$i]}
		    umount ${dir_list[$i]}
		fi
    done
}


delete_vg()
{
    local i
    vg_list=(`vgdisplay | grep 'VG Name' | grep appdata | awk '{print $3}'`)
    len=${#vg_list[@]}
    for  ((i=0; i<len; i++))
    do
        echo ${vg_list[$i]}
        vgremove -f ${vg_list[$i]}
    done
}


delete_vol() {
	umount_dir
	delete_vg
	sed -i '/vg_appdata/d' /etc/fstab
	mount -a
	rm -rf /srv/* > /dev/null 2>&1
}

kill_process() {
	lsof $1 | awk '{print $2}' | xargs kill -9 >/dev/null 2>&1
}

delete_vol
logger -p local0.info "`who -m | cut -d\( -f2 |cut -d\) -f1`;excute uninstall_vol.sh ended"
