#!/bin/bash

echo "32move-NetworkManager start"
fifoName="32move-NetworkManager-fifo"
if [ ! -e ${fifoName} ]; then
    mkfifo -m 666 ${fifoName}
fi
exec 6<>${fifoName}
echo "20" >&6


if [ -d "/fs.old/etc/NetworkManager/system-connections" ]; then
    cp -rfp /fs.old/etc/NetworkManager/system-connections/*  /etc/NetworkManager/system-connections/
fi

echo "50" >&6

# 定义要搜索的目录路径  
directory_path="/etc/NetworkManager/system-connections/"  

# 定义输出文件的路径  
output_file="/var/lib/NetworkManager/timestamps"  

# 使用find命令遍历目录中的所有文件，并使用awk和stat提取uuid值和修改时间戳  
find "$directory_path" -type f -exec bash -c '  

    file="$1"  

    # 获取文件的UUID值（假设它存在于文件的某一行中，并且格式是"uuid=UUID"）  
    uuid=$(awk -F"uuid=" "/uuid=/{print \$2; exit}" "$file")  

    # 获取文件的最后修改时间戳（秒）  
    timestamp=$(stat -c %Y "$file")  

    # 输出UUID和修改时间戳，格式为"UUID=时间戳"  
    echo "$uuid=$timestamp"  
' _ {} \; >> "$output_file"  

file1="有线连接 1"
file2="有线连接 1.nmconnection"
file3="Wired connection 1"
file4="Wired connection 1.nmconnection"

if [ -e "$directory_path$file1" ] && [ -e "$directory_path$file2" ];then
    rm "$directory_path$file2"
    echo "file2 has delete"
else
    echo "file1 or file2 not exists"
fi

if [ -e "$directory_path$file3" ] && [ -e "$directory_path$file4" ];then
    rm "$directory_path$file4"
    echo "file4 has delete"
else
    echo "file3 or file4 not exists"
fi

systemctl restart NetworkManager
  
echo "UUID values and file modification timestamps have been appended to $output_file"
echo "NetworkManager service has been restaered"


echo "100" >&6
exec 6<&-
rm -f ${fifoName}
echo "32move-NetworkManager success"
