#!/bin/bash
#
#if ! /usr/sbin/ifconfig -a | /usr/bin/grep -q 'wlan0'; then
#     /usr/sbin/usb_modeswitch -KQ -v 0bda -p 1a2b
#     if ! /usr/bin/grep -q 'checkwifi.sh' /etc/cron.d/meu-check; then
#          /usr/bin/echo "*/1 * * * * root /usr/local/bin/checkwifi.sh" >> /etc/cron.d/meu-check
#     fi
#fi
#
#sleep 5
#
if /usr/bin/systemctl status nmbd smbd | /usr/bin/grep -Eq "failed|inactive"; then 
   /usr/bin/systemctl restart nmbd smbd
fi
#
sleep 5
#
if /usr/bin/systemctl status tailscaled | /usr/bin/grep -q 'inactive' ||  /usr/bin/systemctl status tailscaled | /usr/bin/grep -q 'missing-profile' || /usr/bin/systemctl status tailscaled | /usr/bin/grep -q 'error: Tailscale could not connect to the'; then 
   /usr/bin/tailscale up
   /usr/bin/systemctl restart tailscaled
else
   if ! /usr/bin/tailscale funnel status | /usr/bin/grep -q '8443'; then
        /usr/bin/tailscale funnel --bg --https=8443 localhost:9443
   fi
fi
#
sleep 5
#
/usr/bin/curl -s http://localhost:1080 -o /tmp/log.txt
#
if /usr/bin/systemctl status ezstream | /usr/bin/grep -q 'inactive' || /usr/bin/systemctl status ezstream | /usr/bin/grep -q "Couldn't connect"; then
   /usr/bin/systemctl restart ezstream
else
   if /usr/bin/grep -q 'Unspecified description' /tmp/log.txt; then
      /usr/bin/systemctl restart ezstream
   fi
fi
#
UUID="b097aaef-b3f9-4e63-a629-b2b4c819a273"
bdev="/dev/disk/by-uuid/${UUID}"
#
if [[ $(lsblk -no MOUNTPOINT ${bdev}) ]]; then
   if ! [[ -e /boot/dtb/teste2.dtb ]]; then
      cp /boot/teste2.dtb /boot/dtb/teste2.dtb 2>/dev/null
   fi
fi
#
if [ ! -e /tmp/mail ]; then
   mkdir /tmp/mail 2>/dev/null
fi
#
