
#!/bin/bash
# 获取当前脚本所在的绝对目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

stop_service_if_running() {
    local service_name="$1"

    # 检查参数是否为空
    if [ -z "$service_name" ]; then
        echo "错误：函数 stop_service_if_running 缺少服务名参数。"
        return 1
    fi

    if systemctl is-active --quiet "$service_name"; then
        echo "🟢 服务 $service_name 正在运行，正在停止..."
        sudo systemctl stop "$service_name"
        if [ $? -eq 0 ]; then
            echo "✅ 服务 $service_name 已成功停止。"
        else
            echo "❌ 停止服务 $service_name 失败。"
            return 1
        fi
    else
        echo "🟡 服务 $service_name 当前未运行。"
    fi
}
stop_service_if_running "sdk_control_panel"

#install dependences
echo "======install dependences======"
sudo apt-get update
sudo apt-get install python3-pip xfonts-75dpi vim net-tools curl sshpass python3-venv  -y
sudo python3 -m pip install watchdog inflection -i https://mirrors.aliyun.com/pypi/simple/
if [ $? -ne 0 ];then
    exit 1;
fi


cp -r $SCRIPT_DIR/sdk_control_panel /home/daystarp/
# sudo cp $SCRIPT_DIR/systemd/sdk_control_panel.service /etc/systemd/system/
TMP_SERVICE_FILE="$SCRIPT_DIR/sdk_control_panel.service"
SERVICE_FILE="/etc/systemd/system/sdk_control_panel.service"
USER_NAME="daystarp"
USER_ID=$(id -u "$USER_NAME")  # 获取指定用户的UID
# 生成服务文件
cat > "$TMP_SERVICE_FILE" << EOF
[Unit]
Description=sdk control panel Service
After=graphical-session.target

[Service]
ExecStart=/home/daystarp/sdk_control_panel/start_sdk_service.sh
User=daystarp
Group=audio
Environment="XDG_RUNTIME_DIR=/run/user/$USER_ID"
Environment="PULSE_RUNTIME_PATH=/run/user/$USER_ID/pulse/"
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=sdk_control_panel_Service


[Install]
WantedBy=multi-user.target
EOF
sudo mv $TMP_SERVICE_FILE $SERVICE_FILE
# sudo cp $SCRIPT_DIR/systemd/sdk_control_panel.service /etc/systemd/system/
pushd /home/daystarp/sdk_control_panel
# 创建python虚拟环境
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -e . -i https://mirrors.aliyun.com/pypi/simple/
python3 -m pip install dist/daystar_robot_sdk_python-2.2.4-py3-none-any.whl -i https://mirrors.aliyun.com/pypi/simple/
deactivate
popd


#set udev for uwb gps
echo "======set udev for uwb gps======"
sudo cp $SCRIPT_DIR/udev/99-gps_sensor.rules /etc/udev/rules.d/
sudo cp $SCRIPT_DIR/udev/99-uwb2_sensor.rules /etc/udev/rules.d/
if [ $? -ne 0 ];then
    exit 1;
fi

#install device drivers
echo "======install device drivers======"
# 获取当前内核版本
KERNEL_VERSION=$(uname -r)
MODULE_DIR="/lib/modules/${KERNEL_VERSION}/kernel/drivers/usb/serial"
sudo apt remove -y  brltty

sudo install -p -m 644 $SCRIPT_DIR/drivers/ch34x.ko $MODULE_DIR
sudo /sbin/depmod -a $KERNEL_VERSION
if [ $? -ne 0 ];then
    exit 1;
fi

# 设置开机自动登录，否则语音服务会启动失败
# 设置你要自动登录的用户名
USERNAME="daystarp"
# 1. 使用 sed：
#    - 取消注释并设置 AutomaticLoginEnable = true
#    - 设置 AutomaticLogin = $USERNAME
sudo sed -i 's/^\s*#\s*AutomaticLoginEnable\s*=\s*.*$/AutomaticLoginEnable=true/' /etc/gdm3/custom.conf
sudo sed -i 's/^\s*#\s*AutomaticLogin\s*=\s*.*$/AutomaticLogin='"$USERNAME"'/' /etc/gdm3/custom.conf

# 2. 如果存在未注释的 AutomaticLoginEnable 行，直接替换其值
if grep -E "^\s*AutomaticLoginEnable\s*=" /etc/gdm3/custom.conf > /dev/null; then
    sudo sed -i 's/^\s*AutomaticLoginEnable\s*=.*/AutomaticLoginEnable=true/' /etc/gdm3/custom.conf
else
    # 不存在则在 [daemon] 段后插入
    sudo sed -i '/\[daemon\]/a AutomaticLoginEnable=true' /etc/gdm3/custom.conf
fi

# 3. 如果存在未注释的 AutomaticLogin 行，更新其值
if grep -E "^\s*AutomaticLogin\s*=" /etc/gdm3/custom.conf > /dev/null; then
    sudo sed -i 's/^\s*AutomaticLogin\s*=.*/AutomaticLogin='"$USERNAME"'/' /etc/gdm3/custom.conf
else
    # 不存在则在 [daemon] 段后插入
    sudo sed -i '/\[daemon\]/a AutomaticLogin='"$USERNAME"'/' /etc/gdm3/custom.conf
fi

sudo cp $SCRIPT_DIR/monitor/sysmon_logger.sh /usr/local/bin/
sudo cp $SCRIPT_DIR/systemd/sysmon_logger.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable sysmon_logger.service
sudo systemctl start sysmon_logger.service

sudo cp $SCRIPT_DIR/udev/99-wheeltec_mic.rules /etc/udev/rules.d/
sudo cp $SCRIPT_DIR/udev/chenxing_agent.rules /etc/udev/rules.d/
sudo systemctl daemon-reload
sudo systemctl enable sdk_control_panel.service
sudo systemctl start sdk_control_panel.service
if [ $? -ne 0 ];then
    exit 1;
fi




