#!/bin/bash

cat > /usr/sbin/t8s-v50-fix-gps <<EOF
#!/bin/bash

sleep 25

PORT="/dev/ttyS4"

# Настройка последовательного порта: 9600 8N1, raw, без flow control
stty -F "\$PORT" 9600 cs8 -cstopb -parenb -ixon -ixoff -crtscts raw

# Команда 1
printf '\x24\x50\x43\x41\x53\x31\x30\x2C\x33\x2A\x31\x46\x0D\x0A' > "\$PORT"
sleep 1

# Команда 2
printf '\x24\x50\x43\x41\x53\x30\x34\x2C\x35\x2A\x31\x43\x0D\x0A' > "\$PORT"
sleep 1

# Команда 3
printf '\x24\x50\x43\x41\x53\x30\x30\x2A\x30\x31\x0D\x0A' > "\$PORT"
sleep 1
EOF

chmod +x /usr/sbin/t8s-v50-fix-gps

#cat > /etc/systemd/system/t8s-v50-fix-gps.service  <<EOF
#[Unit]
#Description=Fix GPS for MIGT8X.amd64.v2.rev50
#
#[Service]
#Type=simple
#ExecStart=/bin/bash -c /usr/sbin/t8s-v50-fix-gps
#
#[Install]
#WantedBy=multi-user.target
#EOF
#
#systemctl enable t8s-v50-fix-gps --now

cat > /usr/lib/systemd/system-sleep/t8s-v50-fix-gps <<EOF
#!/bin/bash

case "\$1" in
    post)
        /usr/sbin/t8s-v50-fix-gps
        ;;
esac
EOF

chmod +x /usr/lib/systemd/system-sleep/t8s-v50-fix-gps
