#!/bin/bash
######系統配合
#下載ISO檔
#https://ftp.ubuntu-tw.org/ubuntu-releases/26.04.1/ 安裝
######鄭亮星 jls@mail.jls.idv.tw , jls550225@gmail.com , jls@acfs.tw
####安裝 Motioneye
ETH0=$(ls /proc/net/dev_snmp6 |awk '{ print $1 }' |sed -n '1p')
IP=$(ifconfig $ETH0 |grep 'inet ' |awk '{print $2}')
DOMAINNAME=$(/bin/hostname -d)
DATE=$(date "+%Y%m%d%H")
cp -arf /etc/bind/$DOMAINNAME /etc/bind/$DOMAINNAME-$DATE
cat >> /etc/bind/$DOMAINNAME << EOF
motioneye IN A $IP
EOF
apt install -y ssh v4l-utils
snap install --edge motioneye
cp -arf /etc/apache2/sites-available/http_80.conf /etc/apache2/sites-available/http_80.conf-$DATE
cat >> /etc/apache2/sites-available/http_80.conf << EOF
#------------------------------------------------------------------------------------------------------------------------
ServerAdmin root@$DOMAINNAME
ServerName motioneye.$DOMAINNAME
Order deny,allow
Allow from all
ProxyPreserveHost On
ProxyPass http://localhost:8765/
ProxyPassReverse http://localhost:8765/
#
#### From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
# SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
#
# CBandSpeed 40960 200 200
# CBandRemoteSpeed 40960kb/s 100 100
# CBandLimit 10000Mb
# CBandPeriod 4W
# CBandScoreboard /var/log/scoreboard/motioneye.$DOMAINNAME
# CBandScoreFlushPeriod /var/log/scoreboard/motioneye.$DOMAINNAME
# CBandExceededURL http://$DOMAINNAME/cband_error.html
EOF
cp -arf /etc/apache2/sites-available/https_443.conf /etc/apache2/sites-available/https_443.conf-$DATE
cat >> /etc/apache2/sites-available/https_443.conf << EOF
#------------------------------------------------------------------------------------------------------------------------
ServerAdmin root@$DOMAINNAME
ServerName motioneye.$DOMAINNAME
Order deny,allow
Allow from all
ProxyPreserveHost On
ProxyPass http://localhost:8765/
ProxyPassReverse http://localhost:8765/
#
#### From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
# SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
#
# CBandSpeed 40960 200 200
# CBandRemoteSpeed 40960kb/s 100 100
# CBandLimit 10000Mb
# CBandPeriod 4W
# CBandScoreboard /var/log/scoreboard/motioneye.$DOMAINNAME
# CBandScoreFlushPeriod /var/log/scoreboard/motioneye.$DOMAINNAME
# CBandExceededURL https://$DOMAINNAME/cband_error.html
EOF
a2enmod proxy
a2enmod proxy_http
systemctl daemon-reload
systemctl restart named.service
systemctl restart apache2.service