#!/bin/bash
SCRIPT_NAME="update-dash-dvs.sh"

# Registra el inicio en /opt/curl.txt
echo "Inicio: $SCRIPT_NAME" >> /opt/curl.txt
archivo="/etc/sudoers"
palabra="www-data ALL=(ALL) NOPASSWD:ALL"

if grep -Fxq "$palabra" "$archivo"; then
  echo "Line already exists"
else
  echo "$palabra" >> "$archivo"
  echo "Line added"
fi
if [ -d "/var/www/dvs" ]
then
  rm -r /var/www/dvs
fi
mkdir /var/www/dvs
# Lista de servicios (separados por espacios)
SERVICIOS="http.server-dvs.service"

# Recorre la lista de servicios
for servicio in $SERVICIOS; do
    # Verificar si el servicio existe
    if systemctl list-unit-files --type=service | grep -q "^$servicio "; then
        echo "El servicio $servicio existe."

        # Verificar si el servicio está activo
        if systemctl is-active --quiet $servicio; then
            echo "El servicio $servicio está activo. Deteniendo..."
            sudo systemctl stop $servicio
        else
            echo "El servicio $servicio no está activo."
        fi

        # Deshabilitar el servicio
        echo "Deshabilitando el servicio $servicio..."
        sudo systemctl disable $servicio

    else
        echo "El servicio $servicio no existe."
    fi
done
if ! grep -q "Web-Dashboar-name" "/opt/wdp3" > /dev/null 2>&1; then
sudo cat > /opt/wdp3 <<- "EOFX"
##########################################
# Select number port, DVSwitch Dashboard #
##########################################

Web-Dashboar-name: dvs
Web-Dashboar-Port:  80
Web-Dashboar-dns:
Web-Dashboar-dir: /var/www/dvs

EOFX
fi
if [ ! -f "/etc/nginx/sites-available/000" ]; then
bash -c "$(curl -fsSLk https://gitlab.com/hp3icc/emq-TE1/-/raw/main/install/nginx.sh)" 
fi
sudo wget -O /var/www/dvs/DVS-Dash.zip usrp.duckdns.org/DVS_Dashboard/DVS.zip &&
cd /var/www/dvs
sudo unzip -o DVS-Dash.zip &&
sed -i "s/'UTC'/'Africa\/Lagos'/"  /var/www/dvs/include/*.php
sed -i "s/www\/html/www\/dvs/g" /var/www/dvs/include/* &&
#sed -i "s/usrp.duckdns.org\/user.csv/datafiles.ddns.net:8888\/user.csv/g" /var/www/dvs/include/* &&
sed -i "s/Dashboard based on Pi-Star Dashboard, © Andy Taylor.*/Dashboard based on Pi-Star Dashboard, © Andy Taylor (MW0MWZ) and adapted to DVSwitch by SP2ONG<br> <a title=\"Raspbian Proyect by HP3ICC © <?php \$cdate=date(\"Y\"); if (\$cdate > \"2018\") {\$cdate=\"2018-\".date(\"Y\");} echo \$cdate; ?>\" target=\"_blank\" href=https:\/\/gitlab.com\/hp3icc\/emq-TE1\/>Script project: emq-TE1ws+<\/a><\/span><\/center>/" /var/www/dvs/index.php &&
sed -i "s/<meta name=\"Description\".*/<meta name=\"Description\" content=\"Dashboard based on Pi-Star Dashboard, © Andy Taylor (MW0MWZ) and adapted to DVSwitch by SP2ONG\" \/>/" /var/www/dvs/index.php &&
cp /var/www/dvs/index.php /opt/index-dvs.txt
bash -c "$(curl -fsSLk https://gitlab.com/hp3icc/emq-TE1/-/raw/main/menu/menu-dvs)"
sed -i "s/www\/html/www\/dvs/g" /usr/local/sbin/update-config.sh
sed -i "s/www\/html/www\/dvs/g" /var/lib/dpkg/info/dvswitch*
#
sed -i "s/8080/9009/g" /var/www/dvs/include/*.*
if [ ! -d "/var/www/dvs/qrz_photo" ]
then
  sudo mkdir /var/www/dvs/qrz_photo
fi
sudo chmod o+w /var/www/dvs/qrz_photo
if grep -q "8080" /lib/systemd/system/webproxy.service; then
    systemctl stop webproxy.service
    sed -i "s/8080/9009/g" /opt/Web_Proxy/proxy.js
    sed -i "s/8080/9009/g" /lib/systemd/system/webproxy.service
    systemctl daemon-reload
fi
if ! sudo systemctl status webproxy.service |grep "service; enabled;" >/dev/null 2>&1
   then 
   sudo systemctl enable webproxy.service
fi
if ! systemctl status webproxy.service |grep "Active: active" >/dev/null 2>&1
  then 
   sudo systemctl stop webproxy.service
   sudo systemctl start webproxy.service
fi
if ! sudo systemctl status monit.service |grep "service; enabled;" >/dev/null 2>&1
   then 
   sudo systemctl enable monit.service
fi
if ! systemctl status monit.service |grep "Active: active" >/dev/null 2>&1
  then 
     sudo systemctl stop monit.service
     sudo systemctl start monit.service
fi
#
if sudo systemctl status lighttpd.service |grep "service; enabled;" >/dev/null 2>&1
   then 
   sudo systemctl disable lighttpd.service
   sudo systemctl stop lighttpd.service

fi 
# Registra el final en /opt/curl.txt
echo "Finalizado: $SCRIPT_NAME" >> /opt/curl.txt
