#!/bin/bash

############################################### LICENSE #########################################################
#                                                                                                               #
#  Copyright (c) 2021 KylinSoft, Inc. kyroot.sh is licensed under Mulan PSL v2. You can use this     		#
#  software according to the terms and conditions of the Mulan PSL v2. You may obtain a copy of Mulan PSL v2    #
#  at: http://license.coscl.org.cn/MulanPSL2 THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES  #
#  OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR    #
#  FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v2 for more details.                                         #
#                                                                                                               #
#  Author: Ouyangkang <ouyangkang@kylinos.cn>                                                                   #
#  Created: Jul 05 2021                                                                                         #
#                                                                                                               #
#################################################################################################################


################## BASIC INFO AND CONFIG ################
USER=`whoami`	#user name
PARTITION_LABEL_KEY="ROOTFS" #KEY OF PARTITION LABEL in two label
#CUR_FS_TYPE=""	#File System Type of current ROOT partition
#SEC_FS_TYPE=""  #File System Type of Second ROOT partition
CUR_ROOT_LABEL="" #partition Label of current ROOT partition
SEC_ROOT_LABEL="" #partition Label of second ROOT partition 
CUR_ROOT_PARTITION="" #Name of Current ROOT Partition
SEC_ROOT_PARTITION="" #Name of Current second Partition
CUR_ROOT_UUID="" #UUID of current ROOT patition
SEC_ROOT_UUID="" #UUID of second ROOT patition
SEC_ROOT_TEMP_DIR="" #temp dir of second ROOT partition
RSYNC_LOG_FILE="/var/log/kyroot.log"	# log file of rsync
SRC_DIR=""	#first sync src directory
DEST_DIR=""	#first sync dest directory
GRUB_FILE=""
if [[ -d /boot/loader/entries/ && -f /etc/machine-id ]];then	# check exist
	read machine_id < /etc/machine-id
	if [ -z "${machine_id}" ]; then
        	return
    	fi
	GRUB_FILE="`ls /boot/loader/entries/${machine_id}-*.conf 2> /dev/null`"
fi
if [[ -z ${GRUB_FILE} ]];then
	if [[ -d /sys/firmware/efi ]];then
		GRUB_FILE="/boot/efi/EFI/kylin/grub.cfg"
	else
		GRUB_FILE="/boot/grub2/grub.cfg"
	fi
fi
################## END OF BASIC INFO AND CONFIG #########

######################### COLOR CODE ####################
RED="31;1m"      # Error message
GREEN="32;1m"    # Success message
YELLOW="33;1m"   # Warning message
PURPLE="36;1m"     # Info message
LIGHT_BLUE="36m" # Debug message
######################## END OF COLOR CODE ##############

######################## COLOR ECHO #####################
colorEcho() { echo -e "\033[${1}${@:2}\033[0m";}
error() { colorEcho $RED $1; }
success() { colorEcho $GREEN $1; }
warn() { colorEcho $YELLOW $1; }
info() { colorEcho $PURPLE $1; }
####################### END OF COLOR ECHO ###############

print_status() {
        local info=$1
        local status=$2
        printf "%-60s" "$info"
        printf "%s\n" "[ "$status" ]"
}

#rsync spin wait
#$1 需要提示的内容
rsync_spin_wait()
{
	while [[ `ps -aux |grep rsync | wc -l` -ge 2 ]]
	do
	        printf "\r$1..... :\t\t\t\t\t[ \033[32;1m—\033[0m ]"
	        sleep 1
	
	        printf "\r$1..... :\t\t\t\t\t[ \033[32;1m/\033[0m ]"
	        sleep 1
	
	        printf "\r$1..... :\t\t\t\t\t[ \033[32;1m|\033[0m ]"
	        sleep 1
	
	        printf "\r$1..... :\t\t\t\t\t[ \033[32;1m\ \033[0m]"
	        sleep 1
	done
	echo 
}

#做一些基本的分区信息检查
#根据卷标检查ROOT分区的个数，存在3或者3个以上则报错
#检查2个分区的大小是否相同，不同则警告
check_root_partition()
{
	count=`blkid | grep "${PARTITION_LABEL_KEY}" | wc -l`
	if [[ ${count} -ne 2 ]];then
		error "根分区数量异常!!!"
		exit 2
	fi
	partition=`blkid | grep ${PARTITION_LABEL_KEY} | cut -d ':' -f1`
	partition_one=`echo ${partition} | cut -d ' ' -f1`		#根分区1
	partition_two=`echo ${partition} | cut -d ' ' -f2`		#根分区2
	#size_one=`df -l ${partition_one} | tail -n1 | sed -e 's/  */ /g' | cut -d ' ' -f2`	#根分区1的大小
	#size_two=`df -l ${partition_two} | tail -n1 | sed -e 's/  */ /g' | cut -d ' ' -f2`	#根分区2的大小
	#if [[ ${size_one} -ne ${size_two} ]];then
	#	warn "${partition_one} 与 ${partition_two} 分区大小不一致，可能会影响到数据同步"
	#fi
	print_status "Check root partition:" "$(success OK)"
}

#得到一些系统的基本信息
#当前使用的root分区的文件系统类型，卷标,分区名
#第二root分区的文件系统类型，卷标，分区名
get_basic_info()
{
	#当前ROOT分区的信息
	CUR_ROOT_PARTITION=`df |grep -w "/" | cut -d ' ' -f1`
	#CUR_FS_TYPE=`blkid ${CUR_ROOT_PARTITION} | sed -r 's/(.*)TYPE="(.*)"(.*)/\2/g'`
	CUR_ROOT_LABEL=`blkid ${CUR_ROOT_PARTITION} | cut -d '"' -f2`
	CUR_ROOT_UUID=`blkid ${CUR_ROOT_PARTITION} | cut -d '"' -f4`


	#第二ROOT分区的信息
	SEC_ROOT_PARTITION=`blkid | grep ${PARTITION_LABEL_KEY} | grep -v ${CUR_ROOT_LABEL} | cut -d ':' -f1`
	#SEC_FS_TYPE=`blkid ${SEC_ROOT_PARTITION} | sed -r 's/(.*)TYPE="(.*)"(.*)/\2/g'`
	SEC_ROOT_LABEL=`blkid ${SEC_ROOT_PARTITION} | cut -d '"' -f2`
	SEC_ROOT_UUID=`blkid ${SEC_ROOT_PARTITION} | cut -d '"' -f4`

	print_status "Get information of Root Partition:" "$(success OK)"
}

#检查用户权限
check_permission()
{
	if [[ ${USER} != "root" ]];then			#非root用户需要检测是否具有sudo权限
		sudo -l &> /dev/null
		if [[ $? -ne 0 ]];then
			print_status "Permission denied:" "$(error FAIL)"
			exit 3
		fi		
		print_status "Permission denied: Please run sudo" "$(error FAIL)"
		exit 4
	fi
	print_status "Check user: " "$(success OK)"
}

#挂载另一个根分区磁盘
another_mount()
{
	while [[ `mount | grep "${SEC_ROOT_UUID}" | wc -l` -gt 0 ]]
	do
		sudo umount ${SEC_ROOT_PARTITION}			#需要先卸载掉第二个分区
		if [[ $? -ne 0 ]];then
			print_status "umount another ROOT: Please run after umount ${SEC_ROOT_PARTITION}" "$(error FAIL)"
	                exit 5
		fi
	done
	SEC_ROOT_TEMP_DIR=$(mktemp -d)
	sudo mount ${SEC_ROOT_PARTITION} ${SEC_ROOT_TEMP_DIR} 
	if [[ $? -ne 0 ]];then
		print_status "mount another ROOT:" "$(error FAIL)"
		exit 6
	fi
}

#卸载另一个根分区磁盘
another_umount()
{
	sudo umount ${SEC_ROOT_TEMP_DIR}  &> /dev/null
	if [[ $? -ne 0 ]];then
		warn "umount fail!"
	else
		rm -rf ${SEC_ROOT_TEMP_DIR}
        fi
}


#修改kyroot的glag
modify_flag()
{
	if [[ ! -f "/boot/efi/EFI/kylin/grub.kyroot" ]];then
		echo "0" > /boot/efi/EFI/kylin/grub.kyroot 2> /dev/null
		chmod 600 /boot/efi/EFI/kylin/grub.kyroot 2> /dev/null
	fi
	if [[ `cat /boot/efi/EFI/kylin/grub.kyroot` -eq 0 ]];then
		echo "1" > /boot/efi/EFI/kylin/grub.kyroot 2> /dev/null
	else
		echo "0" > /boot/efi/EFI/kylin/grub.kyroot 2> /dev/null
	fi

}

#修改grub中的UUID
modify_grub()
{
	for grub_file in ${GRUB_FILE}
	do
		# 备份grub.cfg
		if [[ ! -f "${grub_file}.bak" ]];then
                        print_status "back up "${grub_file}" as "${grub_file}.bak"" "$(success OK)"
                        cp ${grub_file} ${grub_file}.bak
                fi

		if [[ ! -f "${grub_file}" ]];then
			print_status "modify grub:"${grub_file}" does not exist" "$(error FAIL)"
			exit 7
		fi
		current_grub_uuid=`cat "${grub_file}" |grep '^\s\+linux'| sed  -r 's/(.*)root=UUID=([a-zA-Z0-9\-]+|[a-zA-Z0-9\-\/]+)(.*)/\2/g' | head -n1`		#得到grub中的UUID
		#linux   /vmlinuz-4.19.90-43.0.v2207.ky10.aarch64 root=UUID=[ae4a2feb-b5d8-4c0e-bb04-f43e87a52719] ro video=VGA-1:640x480-32@60me rhgb quiet console=tty0 crashkernel=1024M,high smmu.bypassdev=0x1000:0x17 smmu.bypassdev=0x1000:0x15 video=efifb:off
		current_grub_partition_name=`cat "${grub_file}" |grep '^\s\+linux'|sed -r 's/(.*)root=([0-9a-zA-Z\/\-]+)(.*)/\2/g' | head -n1`		#得到分区名字
		#linux   /vmlinuz-4.19.90-43.0.v2207.ky10.x86_64 root=[/dev/mapper/klas-root] ro resume=/dev/mapper/klas-swap rd.lvm.lv=klas/root rd.lvm.lv=klas/swap rhgb quiet crashkernel=1024M,high
	
		if [[ "${CUR_ROOT_UUID}" == "${current_grub_uuid}" || "${current_grub_partition_name}" == "${CUR_ROOT_PARTITION}" ]];then	#grub中的UUID是当前使用的ROOT分区的UUID,或者分区名相同
			sed -r -i "s/(^\s+linux.*)(root=)([0-9a-zA-Z=\/\-]+)(.*)/\1\2UUID=${SEC_ROOT_UUID}\4/g" "${grub_file}"	&> /dev/null		#统一使用UUID
			if lvdisplay ${SEC_ROOT_PARTITION} 2>/dev/null 1>&2 ;then # check if lv partition
				lvm=$(lvdisplay ${SEC_ROOT_PARTITION} |grep "LV Path"|tr -s ' '|cut -d ' ' -f4|cut -c 6-)
	                        sed -i -r "s&(.*)(rd.lvm.lv=)([0-9a-zA-Z\/-]+)(.*)(rd.lvm.lv=[0-9a-zA-Z\/-]+)(.*)&\1\2${lvm}\4\5\6&g" "${grub_file}"
	                fi
	        else
	                sed -r -i "s/(^\s+linux.*)(root=)([0-9a-zA-Z=\/\-]+)(.*)/\1\2UUID=${CUR_ROOT_UUID}\4/g" "${grub_file}"  &> /dev/null            #统一使用UUID
			if lvdisplay ${CUR_ROOT_PARTITION} 2>/dev/null 1>&2 ;then # check if lv partition
				lvm=$(lvdisplay ${CUR_ROOT_PARTITION} |grep "LV Path"|tr -s ' '|cut -d ' ' -f4|cut -c 6-)
	                        sed -i -r "s&(.*)(rd.lvm.lv=)([0-9a-zA-Z\/-]+)(.*)(rd.lvm.lv=[0-9a-zA-Z\/-]+)(.*)&\1\2${lvm}\4\5\6&g" "${grub_file}"
	                fi
		fi
	done 
	print_status "modify grub:" "$(success OK)"
}

modify_fstab()
{
	another_mount           #挂载第二ROOT分区

	if [[ ! -f "${SEC_ROOT_TEMP_DIR}/etc/fstab" ]];then
                print_status "modify fstab:" "$(error ${SEC_ROOT_TEMP_DIR}/etc/fstab not exist)"
                exit 8
        fi
	current_grub_uuid=`cat "${GRUB_FILE}" | grep '^\s\+linux' | sed -r 's/(.*)root=UUID=([a-zA-Z0-9\-]+)(.*)/\2/g' | head -n1`            #得到grub中的UUID
	current_grub_partition_name=`cat "${GRUB_FILE}" | grep '^\s\+linux' | sed -r 's/(.*)root=([0-9a-zA-Z\/\-]+)(.*)/\2/g' | head -n1`     #得到分区名字
	if [[ "${CUR_ROOT_UUID}" == "${current_grub_uuid}" || "${current_grub_partition_name}" == "${CUR_ROOT_PARTITION}" ]];then    #fstab中的UUID是当前使用的ROOT分区的UUID
		sed -r -i "s/${CUR_ROOT_UUID}/${SEC_ROOT_UUID}/g" ${SEC_ROOT_TEMP_DIR}/etc/fstab &> /dev/null
        else
		sed -r -i "s/${SEC_ROOT_UUID}/${CUR_ROOT_UUID}/g" ${SEC_ROOT_TEMP_DIR}/etc/fstab &> /dev/null
        fi

        another_umount          #卸载第二ROOT分区

	print_status "Modify fstab:" "$(success OK)"
}

check_fstab()
{
	if [[ ! -f  "$1/etc/fstab" ]];then
                error "$1 ROOT partition's /etc/fstab does not exist."
                exit 9
        fi
}

show_next_root()
{
	check_permission	#检查权限

	#确保文件存在
        if [[ ! -f "/boot/efi/EFI/kylin/grub.kyroot" ]];then
                echo "0" > /boot/efi/EFI/kylin/grub.kyroot 2> /dev/null
                chmod 600 /boot/efi/EFI/kylin/grub.kyroot 2> /dev/null
        fi

	print_status "Inquire Current ROOT Patition:   " "$(success OK)"

	if [[ `cat /boot/efi/EFI/kylin/grub.kyroot` -eq 1 ]];then
		info "下次重启后将会切换 ROOT 分区"
	else
		info "下次重启不会切换 ROOT 分区"
        fi
}

change_root()
{
	#修改grub,fstab
	check_permission	#检查权限
	modify_flag		#修改grub.kyroot的flag
	print_status "Change ROOT Partition:" "$(success OK)"
}


store_root()
{
	check_permission		#检查权限
	check_root_partition 		#检查root分区信息
	get_basic_info			#得到一些ROOT分区的基本信息
	check_fstab 			#检查fstab是否存在
	another_mount 			#挂载第二Root分区

	if [[ -f ${RSYNC_LOG_FILE} ]];then
		if [[ `du ${RSYNC_LOG_FILE} | sed -r 's/\t\t*/ /g' | cut -d ' ' -f1` -gt 10240 ]];then          #大于10M则删除
                	rm -rf ${RSYNC_LOG_FILE} &> /dev/null
	        fi
	fi

	sudo rsync -avzAXHq --log-file=${RSYNC_LOG_FILE} --ignore-missing-args --delete --exclude "${RSYNC_LOG_FILE}" --exclude "${SEC_ROOT_TEMP_DIR}" --exclude "/backup" --exclude "/boot" \
	       	--exclude "/dev" --exclude "/ghost" --exclude "/mnt" --exclude "/proc"  --exclude "/run" --exclude "/sys" --exclude "/media/*" --exclude "/tmp/*" --exclude "/data" \
	       	--exclude "/var/lib/udisks2/*" / ${SEC_ROOT_TEMP_DIR}/ &>/dev/null & #开始同步
	
       	[[ ! -d ${SEC_ROOT_TEMP_DIR}/dev ]] && mkdir -p ${SEC_ROOT_TEMP_DIR}/dev &> /dev/null	#必须有dev目录，否则可能起不来
       	[[ ! -d ${SEC_ROOT_TEMP_DIR}/run ]] && mkdir -p ${SEC_ROOT_TEMP_DIR}/run &> /dev/null	#必须有dev目录，否则可能起不来
       	[[ ! -d ${SEC_ROOT_TEMP_DIR}/sys ]] && mkdir -p ${SEC_ROOT_TEMP_DIR}/sys &> /dev/null	#必须有dev目录，否则可能起不来
       	[[ ! -d ${SEC_ROOT_TEMP_DIR}/proc ]] && mkdir -p ${SEC_ROOT_TEMP_DIR}/proc &> /dev/null	#必须有dev目录，否则可能起不来
	rsync_spin_wait "Data Sync"
	
	sudo rsync -avzAXHq --log-file=${RSYNC_LOG_FILE} --ignore-missing-args --delete --exclude "${RSYNC_LOG_FILE}" --exclude "${SEC_ROOT_TEMP_DIR}" --exclude "/backup" --exclude "/boot" \
	       	--exclude "/dev" --exclude "/ghost" --exclude "/mnt" --exclude "/proc"  --exclude "/run" --exclude "/sys" --exclude "/media/*" --exclude "/tmp/*" --exclude "/data" \
	       	--exclude "/var/lib/udisks2/*" / ${SEC_ROOT_TEMP_DIR}/ &>/dev/null  #获取同步返回值

	if [[ $? -ne 0 && $? -ne 24 && $? -ne 23 ]];then
		print_status "Data Sync" "$(error FAIL)" 
		exit 14
	else
		print_status "Data Sync:" "$(success OK)"
	fi
	another_umount			#卸载副Root分区
}

restore_root()
{
	check_permission				#检查权限
	check_root_partition            		#检查root分区信息
        get_basic_info                 	 		#得到一些ROOT分区的基本信息
	check_fstab 	${SEC_ROOT_TEMP_DIR}		#检查fstab是否存在
	another_mount 					#挂载第二Root分区

	if [[ -f ${RSYNC_LOG_FILE} ]];then
		if [[ `du ${RSYNC_LOG_FILE} | sed -r 's/\t\t*/ /g' | cut -d ' ' -f1` -gt 10240 ]];then          #大于10M则删除
                	rm -rf ${RSYNC_LOG_FILE} &> /dev/null
	        fi
	fi

        sudo rsync -avzAXH --log-file=${RSYNC_LOG_FILE} --ignore-missing-args --delete --exclude "${RSYNC_LOG_FILE}" --exclude "${SEC_ROOT_TEMP_DIR}" --exclude "/backup" --exclude "/boot" \
	       	--exclude "/dev" --exclude "/ghost" --exclude "/mnt" --exclude "/proc"  --exclude "/run" --exclude "/sys"  --exclude "${RSYNC_LOG_FILE}"--exclude "/media/*" --exclude "/tmp/*" \
	       	--exclude "/data" --exclude "/var/lib/udisks2/*" ${SEC_ROOT_TEMP_DIR}/ / &> /dev/null & #开始同步

       	[[ ! -d ${SEC_ROOT_TEMP_DIR}/dev ]] && mkdir -p ${SEC_ROOT_TEMP_DIR}/dev &> /dev/null	#必须有dev目录，否则可能起不来
       	[[ ! -d ${SEC_ROOT_TEMP_DIR}/run ]] && mkdir -p ${SEC_ROOT_TEMP_DIR}/run &> /dev/null	#必须有dev目录，否则可能起不来
       	[[ ! -d ${SEC_ROOT_TEMP_DIR}/sys ]] && mkdir -p ${SEC_ROOT_TEMP_DIR}/sys &> /dev/null	#必须有dev目录，否则可能起不来
       	[[ ! -d ${SEC_ROOT_TEMP_DIR}/proc ]] && mkdir -p ${SEC_ROOT_TEMP_DIR}/proc &> /dev/null	#必须有dev目录，否则可能起不来
	rsync_spin_wait "Data Sync"
	
        sudo rsync -avzAXH --log-file=${RSYNC_LOG_FILE} --ignore-missing-args --delete --exclude "${RSYNC_LOG_FILE}" --exclude "${SEC_ROOT_TEMP_DIR}" --exclude "/backup" --exclude "/boot" \
	       	--exclude "/dev" --exclude "/ghost" --exclude "/mnt" --exclude "/proc"  --exclude "/run" --exclude "/sys"  --exclude "${RSYNC_LOG_FILE}"--exclude "/media/*" --exclude "/tmp/*" \
	       	--exclude "/data" --exclude "/var/lib/udisks2/*" ${SEC_ROOT_TEMP_DIR}/ / &> /dev/null #获取同步返回值


	if [[ $? -ne 0 && $? -ne 24 && $? -ne 23 ]];then
		print_status "Data Sync" "$(error FAIL)" 
		exit 14
	else
		print_status "Data Sync:" "$(success OK)"
	fi
	another_umount			#卸载副Root分区
}

#首次存储root数据
first_store_root()
{
	if [[ -z ${SRC_DIR} || -z ${DEST_DIR} || ! -d ${SRC_DIR} || ! -d ${DEST_DIR} ]];then
		error "Please check arguments!"
		exit 11
	fi

	if [[ -f ${RSYNC_LOG_FILE} ]];then
		if [[ `du ${RSYNC_LOG_FILE} | sed -r 's/\t\t*/ /g' | cut -d ' ' -f1` -gt 10240 ]];then          #大于10M则删除
                	rm -rf ${RSYNC_LOG_FILE} &> /dev/null
	        fi
	fi

       	[[ ! -d ${DEST_DIR}/dev ]] && mkdir -p ${DEST_DIR}/dev &> /dev/null	#必须有dev目录，否则可能起不来
       	[[ ! -d ${DEST_DIR}/run ]] && mkdir -p ${DEST_DIR}/run &> /dev/null	#必须有dev目录，否则可能起不来
       	[[ ! -d ${DEST_DIR}/sys ]] && mkdir -p ${DEST_DIR}/sys &> /dev/null	#必须有dev目录，否则可能起不来
       	[[ ! -d ${DEST_DIR}/proc ]] && mkdir -p ${DEST_DIR}/proc &> /dev/null	#必须有dev目录，否则可能起不来
	
        rsync -avzAXHq --log-file=${RSYNC_LOG_FILE} --ignore-missing-args --delete --exclude "${RSYNC_LOG_FILE}" --exclude "/`basename ${DEST_DIR}`" --exclude "/backup" --exclude "/boot" \
	       	--exclude "/dev" --exclude "/ghost" --exclude "/mnt" --exclude "/proc"  --exclude "/run" --exclude "/sys" --exclude "/media/*" --exclude "/tmp/*" --exclude "/data" --exclude  \
		"/var/lib/udisks2/*" ${SRC_DIR}/ ${DEST_DIR}/ &>/dev/null #获取同步返回值

	if [[ $? -ne 0 && $? -ne 24 && $? -ne 23 ]];then
		print_status "Data Sync" "$(error FAIL)" 
		exit 14
	else
		print_status "Data Sync:" "$(success OK)"
	fi
}

#注意先调用modify_fstab
#然后再调用modify_grub
real_change()
{
	#手动Stop或者restart服务，不用执行脚本
	if [[ `ps -aux | grep "systemctl" | grep -E "restart|stop" | grep "kyroot" | wc -l` -gt 0 ]];then
                exit 0
        fi

	#确保文件存在
	if [[ ! -f "/boot/efi/EFI/kylin/grub.kyroot" ]];then
                echo "0" > /boot/efi/EFI/kylin/grub.kyroot 2> /dev/null
                chmod 600 /boot/efi/EFI/kylin/grub.kyroot 2> /dev/null
        fi
	#判断是否需要改grub
        if [[ `cat /boot/efi/EFI/kylin/grub.kyroot` -eq 1 ]];then
		check_permission                                #检查权限
	        check_root_partition                            #检查root分区信息
        	get_basic_info                                  #得到一些ROOT分区的基本信息
		modify_fstab	#修改fstab
		modify_grub	#修改grub
		modify_flag	#grub.kyroot清0
		print_status "real change root patition:" "$(success OK)"
        fi
}


show_help()
{
	echo "ROOT分区同步工具,用法："
	echo
	echo "必选参数对长短选项同时适用。"
	echo "		-h | --help 显示帮助"
	echo "		-l | --list 查看当前所选择的启动ROOT分区"
	echo "		-c | --change-root 设置系统启动ROOT分区"
	echo "		-s | --store-root 将此时使用的root分区数据同步到备份ROOT分区"
	echo "		-r | --restore-root 将备份ROOT分区数据同步到当前ROOT分区"
	echo "		--src 源目录   设置数据同步源目录,仅限开发者使用"
	echo "		--dest 目标目录 设置数据同步目标目录，仅限开发者使用"
}

##################### EXEC CONTENT #################################
ARG=`getopt  -o h,c,s,r,l --long help,change-root,store-root,restore-root,list,src:,dest:,real-change -- "$@"`
eval set -- "${ARG}"

if [[ $# -eq 1 ]];then
	show_help
fi

while true ; do
        case "$1" in
		-h|--help)
			show_help
			shift
			;;
		-l|--list)
			show_next_root
			shift
			;;
                -c|--change-root)
			change_root 
			shift 
			;;
		-s|--store-root)
			store_root
			shift 
			;;
		--src)  SRC_DIR=$2
			if [[ -n ${DEST_DIR} ]];then
				first_store_root
				exit 0
			fi
			shift 2
			;;
		--dest) DEST_DIR=$2
			if [[ -n ${SRC_DIR} ]];then
				first_store_root
				exit 0
                        fi
			shift 2
			;;
		-r|--restore-root)
			restore_root
			shift
			;;
		--real-change)
			real_change
			shift
			;;
                --) shift ; break ;;
                *) error "$1:Argument error,Please check arguments!" ; show_help; exit 1 ;;
        esac
done
