#!/bin/bash
if [ $# -ne 3 ]; 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${3}"
    var_copy="/sysroot/ostree/deploy/openkylin/deploy/${deploy_commit}${3}"
    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
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
