#!/bin/bash

# UDP组播服务

SYSTEM_TYPE=$(cat /etc/os-release)

# 执行的 python 脚本
# 安装 python3 脚本依赖库
if [ ! -x "/usr/bin/python3" ]; then
	if [[ $SYSTEM_TYPE == *CentOS* ]]; then
		curl https://file.tyxhetc.cn/install_python3.sh | bash
	fi
fi

if [[ $SYSTEM_TYPE == *Ubuntu* ]]; then 
	apt install -y python3-pip
fi

pip3 install pyyaml &&

wget --no-check-certificate -P /opt/script https://file.tyxhetc.cn/scripts/multicast_receiver.py &&
wget --no-check-certificate -P /opt/script https://file.tyxhetc.cn/scripts/multicast_sender.py

echo """#!/bin/bash

sleep 8
nohup /usr/bin/python3 /opt/script/multicast_receiver.py &
""" > /opt/script/multicast.sh

echo """[Unit]
Description=multicast
After=network.target

[Service]
Type=forking
ExecStart=/opt/script/multicast.sh
ExecStop=/opt/script/multicast.sh
PrivateTmp=true

[Install]
WantedBy=multi-user.target
""" > /lib/systemd/system/multicast.service

chmod +x  /opt/script/multicast.sh
systemctl daemon-reload
systemctl start multicast.service
systemctl enable multicast.service

# 开启组播对应端口
if [[ $SYSTEM_TYPE == *CentOS* ]]; then
	firewall-cmd --add-port=3333/udp --permanent
	firewall-cmd --reload
elif [[ $SYSTEM_TYPE == *Ubuntu* ]]; then 
	ufw allow 3333/udp
fi
