#!/usr/bin/bash
# Скрипт выполняется от пользователя arpwatch, доступа к переменным root он не имеет

BOT_API="$(echo 'NjMxNjk2MDkyMDpBQUVtVjl5YmxrdzgzZ2w2S1V5X1diYXBOWUVOWUUzRm9aRQo=' | base64 -d)"
CHAT_ID=-1001953578248
ARP_TOPIC=1167437
# Motiv1 230653
# Divo1 252799

myVar=$(</dev/stdin)
if [[ $1 == "-odi" ]]; then
	myVar=$(sed 's/From: arpwatch (Arpwatch / /g' <<< "$myVar")
	myVar=$(sed 's/)//g' <<< "$myVar")
	myVar=$(sed 's/To: root//g' <<< "$myVar")
	myVar=$(sed 's/Subject: new station (/✅ /g' <<< "$myVar")
	myVar=$(sed 's/Subject: flip flop (/🔥 /g' <<< "$myVar")
	myVar=$(sed 's/To: root//g' <<< "$myVar")
	myVar=$(sed 's/hostname: //g' <<< "$myVar")
	myVar=$(sed 's/<unknown>//g' <<< "$myVar")
	myVar=$(sed 's/ip address: //g' <<< "$myVar")
	myVar=$(sed 's/interface: //g' <<< "$myVar")
	myVar=$(sed 's/ethernet address: //g' <<< "$myVar")
	myVar=$(sed 's/ethernet vendor: /™️ /g' <<< "$myVar")
	myVar=$(sed 's/timestamp: /⌚️ /g' <<< "$myVar")
	myVar=$(sed 's/^[[:space:]]*/ /' <<< "$myVar")
	myVar=$(sed ':a;N;$!ba;s/\n /\n/g' <<< "$myVar")
	myVar=$(sed ':a;N;$!ba;s/\n\n/\n/g' <<< "$myVar")
	myVar=$(sed ':a;N;$!ba;s/\n\n/\n/g' <<< "$myVar")
else
	ARP_TOPIC="$1"
fi
curl -X POST -H "Content-Type: application/json" \
        -d "{\"chat_id\": ${CHAT_ID},
        \"text\": \"$myVar\",
        \"disable_notification\": true,
        \"reply_to_message_id\": $ARP_TOPIC}" \
        https://api.telegram.org/bot${BOT_API}/sendMessage 

#sed -i 's/ARP_TOPIC="3"/ARP_TOPIC="1167437"/g' /usr/local/bin/tlg.sh