#!/bin/bash
if ! grep -q "Web-Dashboar-name" "/opt/wdp5" > /dev/null 2>&1; then
cat > /opt/wdp5 <<- "EOFX"
###########################################
# Select number port, MMDVMHost Dashboard #
###########################################

Web-Dashboar-name: mmdvmhost
Web-Dashboar-Port:  80
Web-Dashboar-dns:
Web-Dashboar-dir: /opt/MMDVMHost-Websocketboard/html

EOFX
fi
cat > /bin/menu-mmdvm <<- "EOF"
#!/bin/bash
while : ; do
choix=$(whiptail --title "Raspbian Proyect HP3ICC Menu MMDVMHost" --menu "Suba o Baje con las flechas del teclado y seleccione el numero de opcion." 23 50 14 \
1 " Editar MMDVMHost " \
2 " Editar DMRGateway " \
3 " Editar YSFGateway " \
4 " Editar YSF2DMR " \
5 " MMDVMCal " \
6 " Start-Restart service MMDVM " \
7 " Stop service MMDVM " \
8 " Dashboard ON " \
9 " Dashboard Off " \
10 " Editar Puerto http & dns " \
11 " Editar HTML  " \
12 " Editar Dashboard  " \
13 " Update Dashboard TG Name  " \
14 " Menu Principal " 3>&1 1>&2 2>&3)
exitstatus=$?
#on recupere ce choix
#exitstatus=$?
if [ $exitstatus = 0 ]; then
    echo "Your chosen option:" $choix
else
    echo "You chose cancel."; break;
fi
# case : action en fonction du choix
case $choix in
1)
sudo nano /opt/MMDVMHost/MMDVM.ini
if sudo systemctl status mmdvmh.service |grep "service; enabled;" >/dev/null 2>&1
  then 
   sudo systemctl stop mmdvmh.service
   sudo systemctl start mmdvmh.service
fi ;;
2)
sudo nano /opt/DMRGateway/DMRGateway.ini
if sudo systemctl status dmrgw.service |grep "service; enabled;" >/dev/null 2>&1
  then 
   sudo systemctl stop dmrgw.service
   sudo systemctl start dmrgw.service
fi ;;
3)
sudo nano /opt/YSFGateway2/YSFGateway.ini
if sudo systemctl status ysfgw.service |grep "service; enabled;" >/dev/null 2>&1
  then 
   sudo systemctl stop ysfgw.service
   sudo systemctl start ysfgw.service
fi ;;
4)
sudo nano /opt/YSF2DMRGW/YSF2DMR.ini
if sudo systemctl status ysf2dmrgw.service |grep "service; enabled;" >/dev/null 2>&1
  then 
   sudo systemctl stop ysf2dmrgw.service
   sudo systemctl start ysf2dmrgw.service
fi ;;
5)
        network=$(whiptail --title "MMDVMCal" --menu "Select an option:" 10 60 2 \
            "1" "Edit MMDVM.ini" \
            "2" "Start MMDVMCal / Iniciar MMDVMCal" 3>&1 1>&2 2>&3)

        if [ $? != 0 ]; then
            echo "You chose cancel. Returning to the main menu."
            continue
        fi

        case "$network" in
            "1")
            sudo nano /opt/MMDVMHost/MMDVM.ini 
            if sudo systemctl status mmdvmh.service |grep "service; enabled;" >/dev/null 2>&1
             then 
              sudo systemctl stop mmdvmh.service
              sudo systemctl start mmdvmh.service
            fi  ;;
            "2")
            sudo systemctl stop mmdvmh.service
            uartp=$(awk -F= '/\[Modem\]/{f=1} f && /^[^#]*UARTPort=/{print $2; exit}' "/opt/MMDVMHost/MMDVM.ini")
            uarts=$(awk -F= '/\[Modem\]/{f=1} f && /^[^#]*UARTSpeed=/{print $2; exit}' "/opt/MMDVMHost/MMDVM.ini")
            clear
            MMDVMCal $uarts $uartp 
            read -p "Presione Enter para continuar..." 
            if sudo systemctl status mmdvmh.service |grep "service; enabled;" >/dev/null 2>&1
              then 
               sudo systemctl start mmdvmh.service
            fi  ;;
    *)
                echo "Invalid selection"
                ;;
        esac
        ;;
6)
sudo systemctl stop mmdvmh.service
if cat /opt/MMDVMHost/MMDVM.ini | grep ServiceStart=0 >/dev/null 2>&1
then 
 sudo systemctl disable mmdvmh.service
elif cat /opt/MMDVMHost/MMDVM.ini | grep ServiceStart=1 >/dev/null 2>&1
then
 python3 /opt/data-mmdvm.py &&
 sudo systemctl start mmdvmh.service
 sudo systemctl enable mmdvmh.service
fi

sudo systemctl stop ysfgw.service
if cat /opt/YSFGateway2/YSFGateway.ini | grep ServiceStart=0 >/dev/null 2>&1
then 
  sudo systemctl disable ysfgw.service
elif cat /opt/YSFGateway2/YSFGateway.ini | grep ServiceStart=1 >/dev/null 2>&1
then
 sudo systemctl start ysfgw.service
 sudo systemctl enable ysfgw.service
fi

sudo systemctl stop ysf2dmrgw.service
if cat /opt/YSF2DMRGW/YSF2DMR.ini | grep ServiceStart=0 >/dev/null 2>&1
then 
  sudo systemctl disable ysf2dmrgw.service
elif cat /opt/YSF2DMRGW/YSF2DMR.ini | grep ServiceStart=1 >/dev/null 2>&1
then
 sudo systemctl start ysf2dmrgw.service
 sudo systemctl enable ysf2dmrgw.service
fi

sudo systemctl stop dmrgw.service
if cat /opt/DMRGateway/DMRGateway.ini | grep ServiceStart=0 >/dev/null 2>&1
then 
  sudo systemctl disable dmrgw.service
elif cat /opt/DMRGateway/DMRGateway.ini | grep ServiceStart=1 >/dev/null 2>&1
then
 sudo systemctl start dmrgw.service
 sudo systemctl enable dmrgw.service
fi
  ;;
7)
servicios="mmdvmh.service dmrgw.service ysfgw.service ysf2dmrgw.service"

# Recorre la lista de servicios y deshabilita y detiene sin enviar mensaje
for servicio in $servicios; do
    sudo systemctl disable --quiet "$servicio"
    sudo systemctl stop --quiet "$servicio"
done
 ;;
8)
wdp=/opt/wdp5
wsn0=$(grep "Web-Dashboar-name:" $wdp | grep -oP 'Web-Dashboar-name:\s*\K.*')
if [ -f "/etc/nginx/sites-enabled/$wsn0" ]; then
rm /etc/nginx/sites-enabled/$wsn0
fi
if [ -f "/etc/nginx/sites-enabled/000" ]; then
    rm /etc/nginx/sites-enabled/000
fi
sudo systemctl restart nginx

wd0p=$(grep "Web-Dashboar-Port:" $wdp | grep -oP 'Web-Dashboar-Port:\s*\K.*')
if [ -z "$wd0p" ]
then wd0p=80

fi
wsn0=$(grep "Web-Dashboar-name:" $wdp | grep -oP 'Web-Dashboar-name:\s*\K.*')
w0dn0=$(grep "Web-Dashboar-dns:" $wdp | grep -oP 'Web-Dashboar-dns:\s*\K.*')
w0d0=$(grep "Web-Dashboar-dir:" $wdp | grep -oP 'Web-Dashboar-dir:\s*\K.*')
    if [ -z "$w0dn0" ]; then
        if sudo netstat -tuln | grep -q "0.0.0.0:$wd0p "; then
            whiptail --title "Check Port" --msgbox "El puerto $wd0p está ocupado. The port $wd0p is busy" 0 50
            exit 0
          else
            echo ""
        fi
    fi
 sudo ln -sf /etc/nginx/sites-available/000 /etc/nginx/sites-enabled/$wsn0
if [ ! -z "$w0dn0" ]; then
    sudo sed -i '3i\    server_name w0dn0;' /etc/nginx/sites-enabled/$wsn0
fi 
sed -i "s|wsn0|$wsn0|g" /etc/nginx/sites-enabled/$wsn0
sed -i "s|wd0p|$wd0p|g" /etc/nginx/sites-enabled/$wsn0
sed -i "s|w0dn0|$w0dn0|g" /etc/nginx/sites-enabled/$wsn0
sed -i "s|w0d0|$w0d0|g" /etc/nginx/sites-enabled/$wsn0 

if [ -f "/etc/nginx/sites-enabled/000" ]; then
    rm /etc/nginx/sites-enabled/000
fi 
sudo systemctl restart nginx
# Lista de servicios a reiniciar
servicios=("logtailer-mmdvmh.service")

# Recorre la lista de servicios y verifica si están activos
for servicio in "${servicios[@]}"; do
    if systemctl status "$servicio" | grep "service; enabled;" >/dev/null 2>&1; then
        echo "restart $servicio"
        sudo systemctl stop "$servicio"
        sudo systemctl start "$servicio"
      else
         echo "start $servicio"
        sudo systemctl stop "$servicio"
        sudo systemctl start "$servicio"
        sudo systemctl enable "$servicio"        
    fi
done 
 ;;
9)
wdp=/opt/wdp5
wsn0=$(grep "Web-Dashboar-name:" $wdp | grep -oP 'Web-Dashboar-name:\s*\K.*')
if [ -f "/etc/nginx/sites-enabled/$wsn0" ]; then
rm /etc/nginx/sites-enabled/$wsn0
fi
if [ -f "/etc/nginx/sites-enabled/000" ]; then
    rm /etc/nginx/sites-enabled/000
fi
sudo systemctl restart nginx
# Lista de servicios a reiniciar
servicios=("logtailer-mmdvmh.service")
# Recorre la lista de servicios y verifica si están activos
for servicio in "${servicios[@]}"; do
    if systemctl status "$servicio" | grep "service; enabled;" >/dev/null 2>&1; then
        echo "stop $servicio"
        sudo systemctl disable "$servicio"
        sudo systemctl stop "$servicio"
    fi
done
 ;;
10)
wdp=/opt/wdp5
if [ -f "/tmp/w0dn0.txt" ]; then
    rm /tmp/w0dn0.txt
fi
if [ -f "/tmp/wd0p.txt" ]; then
    rm /tmp/wd0p.txt
fi
wsn0=$(grep "Web-Dashboar-name:" $wdp | grep -oP 'Web-Dashboar-name:\s*\K.*')
if [ -f "/etc/nginx/sites-enabled/$wsn0" ]; then
rm /etc/nginx/sites-enabled/$wsn0
fi
if [ -f "/etc/nginx/sites-enabled/000" ]; then
    rm /etc/nginx/sites-enabled/000
fi
sudo systemctl restart nginx

    whiptail --title "Setup Webserver" --inputbox "Select HTTP port:" 10 60 2>/tmp/wd0p.txt
    exitstatus=$?
    
    if [ $exitstatus != 0 ]; then
        echo "You chose cancel. Exiting..."
        exit 1  # Salir del script con un código de error
    fi
    
    wd0p=$(cat /tmp/wd0p.txt)
    
    whiptail --title "Setup Webserver" --inputbox "DNS name:" 10 60 2>/tmp/w0dn0.txt
    exitstatus=$?
    
    if [ $exitstatus != 0 ]; then
        echo "You chose cancel. Exiting..."
        exit 1  # Salir del script con un código de error
    fi
    
    w0dn0=$(cat /tmp/w0dn0.txt)

sed -i "s|Web-Dashboar-Port:.*|Web-Dashboar-Port: $wd0p|g" $wdp
sed -i "s|Web-Dashboar-dns:.*|Web-Dashboar-dns: $w0dn0|g" $wdp
wd0p=$(grep "Web-Dashboar-Port:" $wdp | grep -oP 'Web-Dashboar-Port:\s*\K.*')
if [ -z "$wd0p" ]
then wd0p=80

fi
wsn0=$(grep "Web-Dashboar-name:" $wdp | grep -oP 'Web-Dashboar-name:\s*\K.*')
w0dn0=$(grep "Web-Dashboar-dns:" $wdp | grep -oP 'Web-Dashboar-dns:\s*\K.*')
w0d0=$(grep "Web-Dashboar-dir:" $wdp | grep -oP 'Web-Dashboar-dir:\s*\K.*')
 if [ -z "$w0dn0" ]; then
        if sudo netstat -tuln | grep -q "0.0.0.0:$wd0p "; then
            whiptail --title "Check Port" --msgbox "El puerto $wd0p está ocupado. The port $wd0p is busy" 0 50
            exit 0
          else
            echo ""
        fi
    fi
 sudo ln -sf /etc/nginx/sites-available/000 /etc/nginx/sites-enabled/$wsn0
if [ ! -z "$w0dn0" ]; then
    sudo sed -i '3i\    server_name w0dn0;' /etc/nginx/sites-enabled/$wsn0
fi 
sed -i "s|wsn0|$wsn0|g" /etc/nginx/sites-enabled/$wsn0
sed -i "s|wd0p|$wd0p|g" /etc/nginx/sites-enabled/$wsn0
sed -i "s|w0dn0|$w0dn0|g" /etc/nginx/sites-enabled/$wsn0
sed -i "s|w0d0|$w0d0|g" /etc/nginx/sites-enabled/$wsn0 

if [ -f "/etc/nginx/sites-enabled/000" ]; then
    rm /etc/nginx/sites-enabled/000
fi 
sudo systemctl restart nginx
# Lista de servicios a reiniciar
servicios=("logtailer-mmdvmh.service")

# Recorre la lista de servicios y verifica si están activos
for servicio in "${servicios[@]}"; do
    if systemctl status "$servicio" | grep "service; enabled;" >/dev/null 2>&1; then
        echo "restart $servicio"
        sudo systemctl stop "$servicio"
        sudo systemctl start "$servicio"
      else
         echo "start $servicio"
        sudo systemctl stop "$servicio"
        sudo systemctl start "$servicio"
        sudo systemctl enable "$servicio"        
    fi
done
 ;;
11)
sudo nano /opt/MMDVMHost-Websocketboard/html/index.html ;;
12)
sudo nano /opt/MMDVMHost-Websocketboard/html/js/config.js ;;
13)
bash -c "$(curl -fsSL https://gitlab.com/hp3icc/emq-TE1/-/raw/main/menu/tgname.sh)";;
14)
break;
esac
done
exit 0
EOF
################################################################################################################

chmod +x /bin/menu-mmdvm
ln -sf /bin/menu-mmdvm /bin/MENU-MMDVM
chmod +x /bin/MENU*

