#!/usr/bin/bash

### Redline 'ch01/1'
### Redline 2 'rtsp/streaming?channel=01&subtype=1'
### Novicam Basic-33 '1'
### Novicam PRO-27 'Streaming/Channels/102'
### Bas-ip 'ch01'

#RTSP_PASS='admin:qwerty123'
#RTSP_URL='Streaming/Channels/102'
CAM_SRV=$(hostname --fqdn)
read -p "admin:pass [admin:qwerty123]: " RTSP_PASS
RTSP_PASS=${RTSP_PASS:-'admin:qwerty123'}
read -p "554/ [rtsp/streaming?channel=01&subtype=1] " RTSP_URL
RTSP_URL=${RTSP_URL:-'rtsp/streaming?channel=01&subtype=1'}


dom_from_dns () {
	arrDNS=(${1//./ })
#	DOM_DNS=${arrDNS[0]}.${arrDNS[1]}.${arrDNS[2]}
	DOM_DNS0=${arrDNS[0]}
	DOM_DNS01=${arrDNS[0]}.${arrDNS[1]}
	DOM_DNS012=${arrDNS[0]}.${arrDNS[1]}.${arrDNS[2]}
	DOM_DNS2=${arrDNS[2]}
}
dom_from_dns $CAM_SRV
CAM_SRV_SHORT=$DOM_DNS0
docker-compose down

mkdir -p /var/www/cam
echo '#EXTM3U' > /var/www/cam/$CAM_SRV_SHORT.m3u
cat mediamtx.yml00 > mediamtx.yml

cat /etc/hosts | egrep '^[[:blank:]]*[^[:blank:]#]' | awk '{print $2}' | awk '!/dvr/' | grep '.cam.' > tmp-cam.txt

# Init
cat cam.index.html00 > ./cam.index.html
ID_NUM=1

# Add CAM
while read IP; do
	ITSTROY=".itstroy.pro"
	dom_from_dns $IP
	if [[ "$CAM_SRV_SHORT" == "$DOM_DNS2" ]]; then
		echo mono $IP
		DOM_DNS012=$DOM_DNS01
		ITSTROY=$(echo "."$CAM_SRV_SHORT".itstroy.pro")
	fi

	echo '  {id: '$ID_NUM', name: "'${DOM_DNS012}'", rtsp: "rtsp://rtsp.'${CAM_SRV}':20407/'${DOM_DNS012}'", web: "http://'${DOM_DNS012}${ITSTROY}'/", stream: "http://'${CAM_SRV}':8889/'${DOM_DNS012}'/"},' >> ./cam.index.html
	echo '  '"${DOM_DNS012}"':' >> ./mediamtx.yml
	echo '    source: rtsp://'"${RTSP_PASS}"'@'"${DOM_DNS012}""${ITSTROY}"':554/'"${RTSP_URL}"'' >> ./mediamtx.yml
	echo '#    runOnReady: ffmpeg -i rtsp://localhost:8554/'${DOM_DNS012}' -r 1 -update 1 -y /home/snap/'${DOM_DNS012}'.jpg' >> ./mediamtx.yml
	echo 'rtsp://rtsp.'"${CAM_SRV}"':20407/'"${DOM_DNS012}" >> ./${CAM_SRV_SHORT}.m3u
	(( ID_NUM += 1 ))
done < tmp-cam.txt
echo '' >> ./mediamtx.yml
echo '' >> ./cam.index.html

# Add BAS-IP
cat /etc/hosts | egrep '^[[:blank:]]*[^[:blank:]#]' | awk '{print $2}' | grep '^p' | grep '.door.'> tmp-door.txt
echo '
  "~^(.+)door$":
    source: rtsp://rtsp:142536@$G1door.'${CAM_SRV_SHORT}'.itstroy.pro:8554/ch01
    sourceOnDemand: yes
#    runOnReady: ffmpeg -i rtsp://localhost:8554/$G1.door -r 1 -update 1 -y /home/snap/$G1.door.jpg
  "~^(.+)door(.+)$":
    source: rtsp://rtsp:142536@$G1door$G2.itstroy.pro:8554/ch01
    sourceOnDemand: yes
#    runOnReady: ffmpeg -i rtsp://localhost:8554/$G1.door$G2 -r 1 -update 1 -y /home/snap/$G1.door$G2.jpg' >> mediamtx.yml
while read IP; do
	dom_from_dns $IP
	if [[ "$CAM_SRV_SHORT" == "$DOM_DNS2" ]]; then
		echo mono $IP
		DOM_DNS012=$DOM_DNS01
		ITSTROY=$(echo "."$CAM_SRV_SHORT".itstroy.pro")
	fi

        echo '  {id: '$ID_NUM', name: "'${DOM_DNS012}'", rtsp: "rtsp://rtsp.'${CAM_SRV}':20407/'${DOM_DNS012}'", web: "http://'${DOM_DNS012}${ITSTROY}'/", stream: "http://'${CAM_SRV}':8889/'${DOM_DNS012}'/"},' >> ./cam.index.html
	echo 'rtsp://rtsp.'"${CAM_SRV}"':20407/'"${DOM_DNS012}" >> ./$CAM_SRV_SHORT.m3u

	(( ID_NUM += 1 ))
done < tmp-door.txt
sed -i '$ s/.$//' ./cam.index.html
cat cam.index.html01 >> ./cam.index.html
echo '
  all_others:' >> mediamtx.yml



systemctl restart dnsmasq

yes | cp -i ./$CAM_SRV_SHORT.m3u /var/www/cam/
yes | cp -i ./cam.index.html /var/www/cam/
mv -f ./cam /etc/nginx/sites-available/cam
ln -sf /etc/nginx/sites-available/cam /etc/nginx/sites-enabled/cam
nginx -s reload
docker load < mediamtx-xxx.tar
docker compose up -d
