#!/bin/bash

# nmcli connection show
# nmcli device show
#      NAME    UUID                                  TYPE      DEVICE
#      ens33   b6a6881e-97f1-42bd-8762-ce9468a7098c  ethernet  ens33
#      virbr0  e81f5651-af1b-475c-ad9b-1b49ad2b93d9  bridge    virbr0
# 通过设备获取 NAME
# echo "$(nmcli -t -f NAME,DEVICE connection show | grep "ens33" | cut -d':' -f1)"
# 通过类型获取 NAME
# echo "$(nmcli -t -f NAME,TYPE connection show | grep "ethernet" | cut -d':' -f1)"
#net_9002="有线连接 1"
#net_normal="有线连接 2"

net_9002="$(nmcli -t -f NAME,DEVICE connection show | grep "ens33" | cut -d':' -f1)"
net_normal="$(nmcli -t -f NAME,DEVICE connection show | grep "ens33" | cut -d':' -f1)"
if [[ ! -z "$net_9002" || ! -z "$net_normal" ]]; then
  sudo nmcli connection modify "${net_9002}" ipv4.method manual
  sudo nmcli connection modify "${net_9002}" ipv4.addresses 172.22.0.3/24
  sudo nmcli connection modify "${net_9002}" ipv4.gateway 172.22.0.1
  sudo nmcli connection modify "${net_normal}" ipv4.method auto
  sudo systemctl restart NetworkManager.service
else
  echo "网络设置错误！ 请手动设置，当前网络设置如下："
  connections=$(nmcli -t -f NAME connection show)
  for conn in $connections; do
    echo "Connection: $conn"
    ipv4_method=$(nmcli -g ipv4.method connection show "$conn")
    echo "  IPv4 Method: $ipv4_method"
    ipv4_addresses=$(nmcli -g ipv4.addresses connection show "$conn")
    echo "  IPv4 Addresses: ${ipv4_addresses:-None}"
    ipv4_gateway=$(nmcli -g ipv4.gateway connection show "$conn")
    echo "  IPv4 Gateway: ${ipv4_gateway:-None}"
  done
fi

sudo cp ./a.tar.gz /home/heco
cd /home/heco || {
  echo "cd /home/heco error!"
  exit 1
}
sudo chmod 777 a.tar.gz
tar -xzvf ./a.tar.gz
cd /home/heco/ys_iio_test || {
  echo "cd /home/heco/ys_iio_test error!"
  exit 1
}
./run.sh

clear
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "当前时间是: $current_time"
#ip a
#ip link show | awk '/^[0-9]+:/{iface=$2} /link/{print iface "  " $2 "   "}'
ip link show | awk '/^[0-9]+:/{iface=$2} /link/{printf "%-20s %-20s\n", iface, $2}'

echo "========================  按任意键关机!，取消请按 ctrl+c！   ========================"
read input
unique_id=$(ip link show | awk '/^[0-9]+:/{iface=$2} /link/{print iface "[" $2 "]"}' | paste -sd "_" -)
log_file="./log/${current_time}_${unique_id}.log"
nmcli device show >"$log_file"
sudo shutdown now
