#!/bin/bash
if [ $# -ne 2 ]; then
    echo "Usage: $0 <file_path>"
    exit 1
fi


current_develop(){
ostree_status=$(ostree admin status | grep '\*')
if [ $? -eq 0 ]; then
    echo "获取到当前的部署"
else
    echo "无法获取当前的部署"
    exit 1
fi
deploy_name=$(echo "$ostree_status" | awk '{print $2}')
if [ $? -eq 0 ]; then
    echo "获取到当前的部署名称$deploy_name"
else
    echo "无法获取当前的部署名称"
    exit 1
    
fi
deploy_commit=$(echo "$ostree_status" | awk '{print $3}')
if [ $? -eq 0 ]; then
    echo "获取到当前的部署commit$deploy_commit"
    commit=$(echo "$deploy_commit" | cut -d '.' -f 1)
    echo $commit
    etc_copy="/sysroot/ostree/deploy/${deploy_name}/deploy/${deploy_commit}/usr/etc/"
    var_copy="/sysroot/ostree/deploy/${deploy_name}/deploy/${deploy_commit}${2}"
    mode_file="/sysroot/ostree/deploy/${deploy_name}/deploy/${deploy_commit}.origin"
    boot_path="/sysroot/ostree/deploy/${deploy_name}/deploy/${deploy_commit}/boot/"
    echo "$etc_copy"
    echo "$var_copy"
    echo "$boot_path"
    if [ "$1" == "etc" ];then
	if [ -d "$2" ];then
		echo "---$2---"
       	cp -rf $2/* $etc_copy
        if [ $? -ne 0 ]; then
			if [ -d "$3" ];then
                cp -rf $etc_copy/* $3
			else
		        cp -rf $etc_copy $3
			fi
        fi
	 else
		cp -rf $2 $etc_copy
	fi
    else
	if [ -d "$2" ];then
		echo "---$2---"
        	cp -rf $2/* $var_copy
	else
		cp -rf $2 $var_copy
	fi
    fi
    # if [ -f "$mode_file" ]; then
    #     echo "Directory exists"
    #     if grep -q "mode" "$mode_file"; then
    #         echo "Current mode is 运维 mode"
    #         if [ "$(ls -A $boot_path)" ]; then
    #             echo "目录不为空"
    #         else
    #             echo "目录为空"
    #             cp -rf /boot/* $boot_path
    #         fi
    #     else
    #         echo "File does not contain mode"
    #     fi
    #     else
    #     echo "Directory does not exist"
    # fi
else
    echo "无法获取当前的部署commit"
    exit 1
fi
}

etc_path="$1"
var_path="$2"
current_develop $etc_path $var_path
# if [ -e "$file_path" ]; then
#     echo "File exists, performing checkout..."
#     # 在这里执行checkout动作，例如：
#     # git checkout $file_path
#     current_develop $file_path
# else
#     echo "File does not exist."
# fi


