#!/bin/bash

#############################
#
# error code:
# 1: parameter error
# 2: block device isn't exist
#
#############################
# get parameter list
. /etc/tos/scripts/scripts

usage="Usage:$(basename $0) [/dev/vg0/lv0|/dev/mapper/vg0-lv0]"
[ -z "$1" -o ! -b "$1" ] && {
    echo $usage
    exit 1
}
script_get_lock >/dev/null
[ $? -ne 0 ] && exit 1

lv_path=$(readlink -f "$1")

# volumeGroup isn't exists
vg_name=$(lvs -o vg_name $lv_path --noheadings 2>/dev/null | sed s/[[:space:]]//g)
if [ $? -ne 0 ]; then
    echo $usage
    script_put_lock >/dev/null
    exit 2
fi

TOS_UUID=$(GetDeviceUUID $lv_path)
TOS_VOLUME_ROOT=/tmp/volume
[ ! -e $TOS_VOLUME_ROOT ] && mkdir $TOS_VOLUME_ROOT

lv_name=$(lvs -o lv_name $lv_path --noheadings 2>/dev/null | sed s/[[:space:]]//g)
DEVSORT=$(GetVolumeSort $lv_path) #device sort
debugfile="$TOS_VOLUME_ROOT/${lv_name}"
rm -f $debugfile
process=0
echo "stopLVM:$lv_path:$TOS_UUID:0:$process" >$debugfile

# 10
process=10
echo "stopLVM:$lv_path:$TOS_UUID:0:$process" >$debugfile
#get mntpoint
MNTPOINT=$(getmntpoint "$lv_path")

# 20
process=20
echo "stopLVM:$lv_path:$TOS_UUID:0:$process" >$debugfile
# remove the flashcache
DEVFLAC=/dev/mapper/fc${DEVSORT}
[ -b $DEVFLAC ] && /etc/tos/scripts/flashcache remove $lv_path >/dev/null

# 30
process=30
echo "stopLVM:$lv_path:$TOS_UUID:0:$process" >$debugfile

if [ ! -z "$MNTPOINT" ]; then
    check_main_raid "$MNTPOINT"
    if [ $? -eq 0 ]; then
        main_service_stop
    else
        service $MNTPOINT stop
    fi
    service_stop

    # 40
    process=40
    echo "stopLVM:$lv_path:$TOS_UUID:0:$process" >$debugfile
    umountAll "$MNTPOINT"

    df-json | grep "${MNTPOINT}$" >/dev/null
    if [ $? -eq 0 ]; then
        process=50
        echo "stopLVM:$lv_path:$TOS_UUID:1:$process" >$debugfile
        script_put_lock >/dev/null
        exit 1
    fi

    # 60
    process=60
    echo "stopLVM:$lv_path:$TOS_UUID:0:$process" >$debugfile
    sleep 1
    #remove lvm
    lvremove -f -y $lv_path >/dev/null 2>&1
    #remove volume config
    iniparse -d -f /etc/volume/volume.conf -s "$TOS_UUID"

    /etc/tos/scripts/mntvfs >/dev/null 2>&1
    public_dir=$(readlink -f /mnt/public)
    if [ -z "$public_dir" ]; then
        df-json | awk '/\/mnt\/md[0-9]+$/ {print $8}' | sort | while read line; do
            [ -z "$line" ] && continue
            /etc/init.d/nas/mkbase $line
            public_dir=$(readlink -f /mnt/public)
            break
        done
    fi
    if [ ! -z "$public_dir" ]; then
        for svrname in $MUSTAPP; do
            svrpath=/etc/init.d/$svrname
            [ -e "$svrpath" ] && $svrpath start >/dev/null 2>&1
        done
        for svrname in $(ls /etc/sc.d/); do
            [ "$svrname" = "nginx" ] && continue
            in_Array "$svrname"
            [ $? -eq 0 ] && continue
            scriptshell=/etc/init.d/$svrname

            if [ "$svrname" = "msg" ]; then
                [ -e "/root/.msgcenter.label" ] && rm -r /root/.msgcenter.label
            fi
            [ -f $scriptshell ] && $scriptshell start >/dev/null 2>&1
        done
    fi
else
    # 60
    process=60
    echo "stopLVM:$lv_path:$TOS_UUID:0:$process" >$debugfile
    sleep 1
    #remove lvm
    lvremove -f -y $lv_path >/dev/null 2>&1
fi

script_put_lock >/dev/null
# 100
process=100
echo "stopLVM:$lv_path:$TOS_UUID:0:$process" >$debugfile

echo "VolumeGroup:$vg_name"
exit 0
