Files
eacp_webapp/htdocs/ogn2rcd-install.sh
T
2026-06-16 11:24:50 +08:00

89 lines
2.3 KiB
Bash

#!/bin/bash
SERVICE="ogn2rcd"
SYSTEMD="/etc/systemd/system"
SERVICE_FILE="${SYSTEMD}/${SERVICE}.service"
echo ""
echo "Task: Check if destination is accessible from R-Pi"
if [ "$1" == "" ]
then
DESTIN=radarcape
else
DESTIN=$1
fi
ping -c 1 -q $DESTIN > /dev/null
if [ $? == 0 ]
then
echo "${DESTIN} is reachable from this computer"
else
echo "${DESTIN} is not reachable, please investigate."
echo "If operating with different hostname, call script with ./ogn-install.sh <hostname>"
echo "Or you may use IP address and call script with ./ogn-install.sh <ip-address>"
exit 1
fi
echo ""
echo "Task: Make sure only root can run our script"
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
echo ""
echo "Task: Check if local system provides systemd services"
if [ ! -d "$SYSTEMD" ]; then
echo "Folder ${SYSTEMD} missing, ensure you are on the right system"
exit 1
fi
# nc (netcat) would be possible if destination is addressed with IP address, but socat provides name resolution
# in this case, use nc localhost 50001 > /dev/udp/192.168.1.38/50001
# multiple targets can be served with
# socat tcp:localhost:50001 - | tee >(socat - udp-sendto:radarcape.local:50001) >(socat - udp-sendto:target2:50001)
echo ""
echo "Task: Install socat"
apt-get update
apt-get install socat
echo ""
echo "Task: Stop and disable eventually running ogn2rcd service"
if [ "$(systemctl is-active ${SERVICE})" = "active" ]
then
systemctl stop ${SERVICE}
fi
systemctl disable ${SERVICE} || true
echo ""
echo "Task: Generate systemd service which starts local script automatically after reboot"
cat > /etc/systemd/system/${SERVICE}.service <<-__EOF__
[Unit]
Description=Mirror TCP port 50001 to ${DESTIN}
After=network.target
[Service]
ExecStart=\
/usr/bin/socat -u tcp:localhost:50001 udp-sendto:${DESTIN}:50001
RestartSec=5
Restart=always
[Install]
WantedBy=multi-user.target
__EOF__
echo ""
echo "Task: Refresh systemd configuration and enter ogn2rcd.service to autostart"
systemctl daemon-reload
systemctl enable ${SERVICE}
systemctl start ${SERVICE}
echo ""
echo "Service ogn2rcd correctly installed and enabled"
echo "You now should enable external source \"Open Glider Net Local Receiver\""
echo "Monitor operation in Status -> Open Glider Net Local Receiver Connection"