#!/bin/bash
#
if ifconfig -a | grep -q 'wlan0'; then
   echo -e "\cEsta Tudo ok com WIFI!!"
else
   echo -e "\cReiniciando o 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 
   echo -e "\cEsta Tudo ok com Samba!!"
else
   echo -e "\cReiniciando o Samba ok!!"
   /usr/bin/systemctl restart nmbd smbd
fi
#
sleep 5
#
if /usr/bin/systemctl status tailscaled | grep -q 'inactive'; then 
   echo -e "\cIniciando o TailScale!!"
   /usr/bin/systemctl restart tailscaled
else
   if /usr/bin/tailscale funnel status | grep -q '8443'; then
      echo -e "\cEsta Tudo ok com TailScale!!"
   else
      echo -e "\cReiniciando o 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
   echo -e "\cIniciando o EzStream ok!!"
   /usr/bin/systemctl restart ezstream
else
   if ! /usr/bin/grep -q 'Unspecified description' /tmp/log.txt; then
      echo -e "\cEsta Tudo ok com EzStream!!"
   else
      echo -e "\cReiniciando o EzStream ok!!"
      /usr/bin/systemctl restart ezstream
   fi
fi
#if ! /usr/bin/systemctl status ezstream | grep -q "Couldn't connect"; then
#   echo -e "\cEsta Tudo ok com EzStream!!"
#else
#   echo -e "\cReiniciando o EzStream ok!!"
#   /usr/bin/systemctl restart ezstream
#fi
#
sleep 5
#
#if /usr/bin/mount | grep -q '/dev/sda1 on'; then
#   echo -e "\cEsta Tudo ok com USB!!"
#else
#   echo -e "\cReiniciando o USB!!"
#   /usr/bin/mount -a
#fi
#
ae=/boot/dtb/teste2.dtb
#
if ! [[ -e $ae ]]; then
   echo -e "\cArquivo '$ae' Não Existe!!"
   echo -e "\cCopiando /boot/teste2.dtb para $ae"
   cp /boot/teste2.dtb /boot/dtb/teste2.dtb
else
   echo -e "\cEsta Tudo ok Arquivo '$ae' ja Existe!!"
fi
#
sleep 5
#
de=/tmp/mail
#
if ! [[ -d $de ]]; then
   echo -e "\cO Diretorio '$de' Nao Existe!!"
   echo -e "\cCriando o Diretorio $de"
   mkdir $de
else
   echo -e "\cEsta Tudo ok Diretorio '$de' ja Existe!!"
fi
#
