#!/bin/bash
#
. /usr/local/bin/banner
#
IFS=$(echo -en "\n\b")
#
clear
#
if ifconfig -a | grep -q 'wlan0'; then
   msg "${bama}Esta Tudo ok com ${bbra}WIFI!!"
else
   msg "${bama}Reiniciando o ${bbra}WIFI ok!!"
   /usr/sbin/usb_modeswitch -KW -v 0bda -p 1a2b
fi
#
sleep 5
#
if ! /usr/bin/systemctl status nmbd smbd | grep -Eq "failed|inactive"; then 
   msg "${bama}Esta Tudo ok com ${bbra}Samba!!"
else
   msg "${bama}Reiniciando o ${bbra}Samba ok!!"
   /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
   msg "${bama}Iniciando o ${bbra}TailScale!!"
   /usr/bin/tailscale up
   /usr/bin/systemctl restart tailscaled
else
   if /usr/bin/tailscale funnel status | grep -q '8443'; then
      msg "${bama}Esta Tudo ok com ${bbra}TailScale!!"
   else
      msg "${bama}Reiniciando o ${bbra}TailScale!!"
      /usr/bin/tailscale funnel --bg --https=8443 localhost:9443
   fi
fi
#
sleep 5
#
#/usr/bin/curl -s http://radiom7b.dyndns.org:1010 -o /tmp/log.txt
#
/usr/bin/curl -s http://localhost:1080 -o /tmp/log.txt
#
if /usr/bin/systemctl status ezstream | grep -q 'inactive' || /usr/bin/systemctl status ezstream | grep -q "Couldn't connect"; then
   msg "${bama}Iniciando o ${bbra}EzStream ok!!"
   /usr/bin/systemctl restart ezstream
else
   if ! /usr/bin/grep -q 'Unspecified description' /tmp/log.txt; then
      msg "${bama}Esta Tudo ok com ${bbra}EzStream!!"
   else
      msg "${bama}Reiniciando o ${bbra}EzStream ok!!"
      /usr/bin/systemctl restart ezstream
   fi
fi
#if ! /usr/bin/systemctl status ezstream | grep -q "Couldn't connect"; then
#   echo "Esta Tudo ok com EzStream!!"
#else
#   echo "Reiniciando o EzStream ok!!"
#   /usr/bin/systemctl restart ezstream
#fi
#
sleep 5
#
#if /usr/bin/mount | grep -q '/dev/sda1 on'; then
#   echo "Esta Tudo ok com USB!!"
#else
#   echo "Reiniciando o USB!!"
#   /usr/bin/mount -a
#fi
#
UUID="b097aaef-b3f9-4e63-a629-b2b4c819a273"
bdev="/dev/disk/by-uuid/${UUID}"
#
if [[ -e ${bdev} ]]; then
   if [[ $(lsblk -no MOUNTPOINT ${bdev}) ]]; then
      if ! [[ -e /boot/dtb/teste2.dtb ]]; then
         msg1="${bama}Arquivo ${bbra}'${ver}/boot/dtb/teste2.dtb${bbra}' ${bama}Não Existe!!"
         msg2="${bama}Copiando ${bbra}/boot/teste2.dtb para ${bbra}'${bcia}/boot/dtb/teste2.dtb${bbra}'"
         msg $msg1 $msg2
         cp /boot/teste2.dtb /boot/dtb/teste2.dtb
      else
         msg "${bama}Esta Tudo ok Arquivo ${bbra}'${bcia}/boot/dtb/teste2.dtb${bbra}' ${bama}ja Existe!!"
      fi
   else
      msg "${bbra}'${bcia}/boot/${bbra}' ${bama}Não Esta Montado!!"
   fi
else
   msg "${bbra}'${bcia}${bdev}${bbra}' ${bama}Não Existe!!"
fi
#
sleep 5
#
de=/tmp/mail
#
if ! [[ -d $de ]]; then
   msg1="${bama}O Diretorio ${bbra}'${bcia}$de${bbra}' ${bama}Nao Existe!!"
   msg2="${bama}Criando o Diretorio ${bbra}'${bcia}$de${bbra}'"
   msg $msg1 $msg2
   mkdir $de
else
   msg "${bama}Esta Tudo ok Diretorio ${bbra}'${bcia}$de${bbra}'${bama} ja Existe!!"
fi
#
