#!/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/openkylin/deploy/${deploy_commit}/usr${2}"
    var_copy="/sysroot/ostree/deploy/openkylin/deploy/${deploy_commit}${2}"
    boot_path="/sysroot/ostree/deploy/openkylin/deploy/${deploy_commit}/boot/"
    mode_file="/sysroot/ostree/deploy/openkylin/deploy/${deploy_commit}.origin"
    #current_path="/sysroot/ostree/deploy/openkylin/deploy/${deploy_commit}/usr${1}"
    if [ "$1" == "etc" ]; then
        if [ "$2" == "install" ];then
            if [ -e "$3" ]; then
                echo "File exists, performing checkout..."
                diff_result=$(diff "$3" "$etc_copy")
             # 检查是否有差异
                if [ -z "$diff_result" ]; then
                    echo "没有差异"
                else
                    echo "有差异"
                    cp -rf $etc_copy/* $3
                    if [ $? -ne 0 ]; then
			            if [ -d "$3" ];then
                       	  cp -rf $etc_copy/* $3
			            else
		                  cp -rf $etc_copy $3
			            fi
                    fi
                fi
            else
                if [ -d "$etc_copy" ];then #判断是文件还是目录
                    mkdir -p $3
                    cp -rf $etc_copy/* $3
                else
                    cp -rf $etc_copy $3
                fi
            fi
        else
            if [ -e "$3" ]; then
                echo "File exists, performing checkout..."
                diff_result=$(diff "$3" "$etc_copy")
             # 检查是否有差异
                if [ -z "$diff_result" ]; then
                    echo "没有差异"
                else
                    echo "有差异"
                    ostree checkout --subpath=/usr${3} --union -C $commit $3
                     if [ $? -ne 0 ]; then
			            if [ -d "$3" ];then
                       	  cp -rf $etc_copy/* $3
			            else
		                  cp -rf $etc_copy $3
			            fi
                    fi
                fi
            else
                ostree checkout --subpath=/usr${3} --union -C $commit $3
                if [ $? -ne 0 ]; then
		            if [ -d "$3" ];then
                        mkdir -p $3
			            cp -rf $etc_copy/* $3
		            else
                   	    cp -rf $etc_copy $3
		            fi
                fi
            fi

        fi
    fi
    if [ "$1" == "var" ]; then
        if [ "$2" == "install" ];then
            if [ -e "$3" ]; then
                echo "File exists, performing checkout..."
                diff_result=$(diff "$3" "$var_copy")
             # 检查是否有差异
                if [ -z "$diff_result" ]; then
                    echo "没有差异"
                else
                    echo "有差异"
                    cp -rf $var_copy/* $3
                    if [ $? -ne 0 ]; then
			            if [ -d "$3" ];then
                       	  cp -rf $var_copy/* $3
			            else
		                  cp -rf $var_copy $3
			            fi
                    fi
                fi
            else
                if [ -d "$var_copy" ];then #判断是文件还是目录
                    mkdir -p $3
                    cp -rf $var_copy/* $3
                else
                    cp -rf $var_copy $3
                fi
            fi
        else
            if [ -e "$3" ]; then
                echo "File exists, performing checkout..."
                diff_result=$(diff "$3" "$var_copy")
             # 检查是否有差异
                if [ -z "$diff_result" ]; then
                    echo "没有差异"
                else
                    echo "有差异"
                    ostree checkout --subpath=/usr${3} --union -C $commit $3
                     if [ $? -ne 0 ]; then
			            if [ -d "$3" ];then
                       	  cp -rf $var_copy/* $3
			            else
		                  cp -rf $var_copy $3
			            fi
                    fi
                fi
            else
                ostree checkout --subpath=/usr${3} --union -C $commit $3
                if [ $? -ne 0 ]; then
		            if [ -d "$3" ];then
                        mkdir -p $3
			            cp -rf $var_copy/* $3
		            else
                   	    cp -rf $var_copy $3
		            fi
                fi
            fi

        fi
    fi
        # if [ -d "$etc_copy" ]; then
        #     if [ -e "$2" ]; then
        #         echo "File exists, performing checkout..."
        #         diff_result=$(diff "$2" "$etc_copy")
        #      # 检查是否有差异
        #         if [ -z "$diff_result" ]; then
        #             echo "没有差异"
        #         else
        #             echo "有差异"
        #             ostree checkout --subpath=/usr${2} --union -C $commit $2
        #             if [ $? -ne 0 ]; then
		# 	            if [ -d "$2" ];then
        #                	  cp -rf $etc_copy/* $2
		# 	            else
		#                   cp -rf $etc_copy $2
		# 	             fi
        #             fi
        #         fi
        #     else
        #         ostree checkout --subpath=/usr${2} --union -C $commit $2
        #         if [ $? -ne 0 ]; then
		#             if [ -d "$2" ];then
		# 	            cp -rf $etc_copy/* $2
		#             else
        #            	    cp -rf $etc_copy $2
		#             fi
        #         fi
        #     fi
        # else
        #     echo "$etc_copy不存在"
        #     if [ -e "$etc_copy" ];then
        #         cp -rf "$etc_copy" "$2"
        #     fi
        # fi
   

    # if [ "$1" == "var" ];then 
    #     if [ -d "$var_copy" ]; then
    #         if [ -e "$2" ]; then
    #             echo "File exists, performing checkout..."
    #             diff_result=$(diff "$2" "$var_copy")
    #          # 检查是否有差异
    #             if [ -z "$diff_result" ]; then
    #                 echo "没有差异"
    #             else
    #                 echo "有差异"
    #                 #ostree checkout --subpath=${2} --union -C $commit $2
    #                 if [ $? -ne 0 ]; then
    #                     echo " $var_copy"
	# 		if [ -d "$2" ];then
	# 	           cp -rf $var_copy/* $2
	# 		else
    #                        cp -rf $var_copy $2
	# 		fi
    #                 fi
    #             fi
    #         else
    #             ostree checkout --subpath=${2} --union -C $commit $2
    #             if [ $? -ne 0 ]; then
    #                 echo "$var_copy"
	# 	    if [ -d "$2" ];then
	# 		cp -rf $var_copy/* $2
	# 	    else
    #                	 cp -rf $var_copy $2
	# 	   fi
    #             fi
    #         fi
    #     else
    #         echo "$var_copy不存在"
    #         if [ -e "$var_copy" ];then
	# 	if [ -d "$2" ];then
	# 	   cp -rf $var_copy/* $2
	# 	else
    #            	    cp -rf $var_copy $2
	# 	fi
    #         fi
    #     fi
    # fi
    # if [ -f "$mode_file" ]; then
    #     echo "--Directory exists----"
    #     if [ "$(ls -A $boot_path)" ]; then
    #             echo "目录不为空"
    #         if grep -q "mode" "$mode_file"; then
    #             echo "Current mode is 运维 mode"
    #         else
    #             echo "File does not contain mode"
    #             cp -rf $boot_path* /boot/
    #             if [ $? -ne 0 ]; then
    #                 echo "boot目录拷贝成功"
    #                 rm -rf $boot_path*
    #                 if [ $? -ne 0 ]; then
    #                     echo "删除成功"
    #                 fi
    #             fi
    #         fi
    #         else
    #             echo "目录为空"
    #         fi
        
    # else
    #     echo "Directory does not exist"
    # fi

else
    echo "无法获取当前的部署commit"
    exit 1
fi
}

path_type="$1"
type="$2"
path="$3"
echo "$1"
echo "$2"
echo "$3"
current_develop $path_type $type $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
