#!/bin/bash
#
#if /usr/sbin/ifconfig -a | grep -q 'wlan0' ; then
#   echo -e "\c"
#else
#   /usr/sbin/usb_modeswitch -KQ -v 0bda -p 1a2b
#fi

if ! /usr/sbin/ifconfig -a | grep -q 'wlan0' ; then
   /usr/sbin/usb_modeswitch -KQ -v 0bda -p 1a2b
fi

#
sleep 5
#
if /usr/bin/systemctl status nmbd smbd | grep -Eq "failed|inactive"; then 
   /usr/bin/systemctl restart nmbd smbd
fi
#
sleep 5
#
#if /usr/bin/systemctl status tailscaled | grep -q 'inactive'; then 
#   /usr/bin/systemctl restart tailscaled
#else
#   if /usr/bin/tailscale funnel status | grep -q '8443'; then
#      echo -e "\c"
#   else
#      /usr/bin/tailscale funnel --bg --https=8443 localhost:9443
#   fi
#fi

if /usr/bin/systemctl status tailscaled | grep -q 'inactive'; then 
   /usr/bin/systemctl restart tailscaled
else
   if ! /usr/bin/tailscale funnel status | 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 | grep -q 'inactive' || /usr/bin/systemctl status ezstream | 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
#
#if [[ -e /boot/dtb/teste2.dtb ]]; then
#   echo -e "\c"
#else
#   cp /boot/teste2.dtb /boot/dtb/teste2.dtb
#fi
#
#if [[ -e /tmp/mail ]]; then
#   echo -e "\c"
#else
#   mkdir /tmp/mail 2>/dev/null
#fi
#

if ! [[ -e /boot/dtb/teste2.dtb ]]; then
   cp /boot/teste2.dtb /boot/dtb/teste2.dtb 2>/dev/null
fi
#
if ! [[ -e /tmp/mail ]]; then
   mkdir /tmp/mail 2>/dev/null
fi
