#!/bin/bash
if [ $# -ne 1 ]; 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
    current_path="/sysroot/ostree/deploy/openkylin/deploy/${deploy_commit}/usr${1}"
    if [ -d "$current_path" ]; then
        diff_result=$(diff "$1" "$current_path")
        # 检查是否有差异
        if [ -z "$diff_result" ]; then
            echo "没有差异"
        else
            echo "有差异"
            sudo ostree checkout --subpath=/usr${1} --union -C $commit $1 
            exit 0

        fi
    else
        echo "$current_path不存在"
        if [ -f "$current_path" ];then
            mv "$current_path" "$1"
        fi
        exit 1
    fi
else
    echo "无法获取当前的部署commit"
    exit 1
fi
}

file_path="$1"

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


