#!/usr/bin/bash

docker-compose down
RTSP_PASS='admin:Co.4eli$'
RTSP_URL='Streaming/Channels/102'
CAM_SRV=$(hostname --fqdn)
dom_from_dns () {
	arrDNS=(${1//./ })
	DOM_DNS=${arrDNS[0]}.${arrDNS[1]}.${arrDNS[2]}
	DOM_DNS0=${arrDNS[0]}
}

echo '###############################################
# Global settings

# Settings in this section are applied anywhere.

###############################################
# Global settings -> General

logLevel: error
logDestinations: [stdout]
logFile: mediamtx.log

readTimeout: 10s
writeTimeout: 10s
writeQueueSize: 512
udpMaxPayloadSize: 1472

# Enable Prometheus-compatible metrics.
metrics: yes
metricsAddress: 0.0.0.0:9998


###############################################
# Global settings -> Authentication

authMethod: internal
authInternalUsers:
- user: any
  pass:
  ips: []
  permissions:
    # Available actions are: publish, read, playback, api, metrics, pprof.
  - action: publish
  - action: api
  - action: metrics
  - action: read
  - action: pprof
  - action: playback


###############################################
# Global settings -> Control API
api: yes


###############################################
# Global settings -> RTSP

# Allow publishing and reading streams with the RTSP protocol.
rtsp: yes
protocols: [udp, multicast, tcp]
encryption: "no"
rtspAddress: :8554
rtspsAddress: :8322
rtpAddress: :8000
rtcpAddress: :8001
multicastIPRange: 224.1.0.0/16
multicastRTPPort: 8002
multicastRTCPPort: 8003
serverKey: server.key
serverCert: server.crt
authMethods: [basic]

###############################################
# Global settings -> WebRTC server

webrtc: yes
webrtcAddress: :8889
webrtcAdditionalHosts: ["*"]
webrtcEncryption: no
webrtcServerKey: server.key
webrtcServerCert: server.crt
webrtcAllowOrigin: "*"
webrtcTrustedProxies: []
webrtcLocalUDPAddress: :8189
webrtcLocalTCPAddress: :8189
webrtcIPsFromInterfaces: yes
webrtcIPsFromInterfacesList: []
webrtcICEServers2: []

###############################################
# Path settings

pathDefaults:
  source: rtsp://*
  sourceOnDemand: yes

paths:
### Novicam Basic 33
#rtsp://admin:q4815162342@1.cam.resp204k7.itstroy.pro:554/1
### Novicam PRO 27
#rtsp://admin:Adm1234.@28.cam.resp204k7.itstroy.pro:554/Streaming/channels/102
### Bas-ip
#rtsp://rtsp:142536@p0-1.door.resp204k7.itstroy.pro:8554/ch01

' > mediamtx.yml

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

# Init
	echo '<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>cam.'"${CAM_SRV}"'</title>
</head>
<body bgcolor="121212" text="white">
    <table width="100%">' > html.txt
TD_NUM=0

# Add CAM as td
while read IP; do
	dom_from_dns $IP
	if [ $TD_NUM = 0 ]; then 
		echo '<tr>' >> html.txt
	fi

	echo '<td><a href="rtsp://rtsp.'"${CAM_SRV}"':20407/'"${DOM_DNS}"'">'"${DOM_DNS}"'</a> <a href="http://'"${DOM_DNS}"'.itstroy.pro/">web</a> <br><iframe src="http://'"${CAM_SRV}"':8889/'"${DOM_DNS}"'/" scrolling="no" frameBorder="0"></iframe></td>' >> html.txt
	echo '  '"${DOM_DNS}"':' >> mediamtx.yml
	echo '    source: rtsp://'"${RTSP_PASS}"'@'"${DOM_DNS0}"'.cam.'"${CAM_SRV}"':554/'"${RTSP_URL}"'' >> mediamtx.yml

	(( TD_NUM += 1 ))
	if [ $TD_NUM = 5 ]; then 
		echo '</tr>' >> html.txt
		TD_NUM=0
	fi
done < tmp-cam.txt

# Add BAS-IP as td
cat /etc/hosts | egrep '^[[:blank:]]*[^[:blank:]#]' | awk '{print $2}' | grep '^p' | grep '.door.'> tmp-door.txt
while read IP; do
	dom_from_dns $IP
	if [ $TD_NUM = 0 ]; then 
		echo '<tr>' >> html.txt
	fi

	echo '<td><a href="rtsp://rtsp.'"${CAM_SRV}"':20407/'"${DOM_DNS}"'">'"${DOM_DNS}"'</a> <a href="http://'"${DOM_DNS}"'.itstroy.pro/">web</a> <br><iframe src="http://'"${CAM_SRV}"':8889/'"${DOM_DNS}"'/" scrolling="no" frameBorder="0"></iframe></td>' >> html.txt
	echo '  '"${DOM_DNS}"':' >> mediamtx.yml
	echo '    source: rtsp://rtsp:142536@'"${DOM_DNS}"'.itstroy.pro:8554/ch01' >> mediamtx.yml

	(( TD_NUM += 1 ))
	if [ $TD_NUM = 5 ]; then 
		echo '</tr>' >> html.txt
		TD_NUM=0
	fi
done < tmp-door.txt

echo '<tr></tr></table></body></html>' >> html.txt
echo '
  all_others:' >> mediamtx.yml

systemctl restart dnsmasq
#yes | cp -i html.txt cam.index.html99

mkdir -p /var/www/cam
mv -f ./html.txt /var/www/cam/cam.index.html
mv -f ./cam /etc/nginx/sites-available/cam
ln -sf /etc/nginx/sites-available/cam /etc/nginx/sites-enabled/cam
nginx -s reload
#mv -f ./mediamtx.yml99 ./mediamtx.yml

#nano /var/www/cam/cam.index.html
#nano mediamtx.yml

docker-compose up -d
