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

Web-Dashboar-name: ysf
Web-Dashboar-Port:  80
Web-Dashboar-dns:
Web-Dashboar-dir: /opt/WSYSFDash/html

EOFX
fi
sudo cat > /bin/menu-ysf <<- "EOF"
#!/bin/bash
while : ; do
choix=$(whiptail --title "Raspbian Proyect HP3ICC Menu YSFReflector" --menu "Suba o Baje con las flechas del teclado y seleccione el numero de opcion" 20 50 11 \
1 " Editar pYSFReflector Server " \
2 " Iniciar Reflector  " \
3 " Detener Reflector  " \
4 " Dashboard on  " \
5 " Dashboard off  " \
6 " Editar Puerto http & dns " \
7 " Editar HTML  " \
8 " Editar Dashboard  " \
9 " 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/pYSFReflector3/pysfreflector.ini 
;;
2)
servicios="YSFReflector.service"

# Primero, detener todos los servicios
for servicio in $servicios; do
    if systemctl is-enabled "$servicio" &>/dev/null || systemctl is-active "$servicio" &>/dev/null; then
        echo "Deteniendo $servicio"
        sudo systemctl stop "$servicio"
    fi
done

# Esperar un momento para asegurarse de que todos los servicios se detengan completamente
sleep 5

# Luego, iniciar todos los servicios
for servicio in $servicios; do
    echo "Iniciando $servicio"
    sudo systemctl start "$servicio"
    sudo systemctl enable "$servicio" &>/dev/null
done
;;
3)
sudo systemctl stop YSFReflector.service && sudo systemctl disable YSFReflector.service  ;;
4)
wdp=/opt/wdp4
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-ysf.service"

# Primero, detener todos los servicios
for servicio in $servicios; do
    if systemctl is-enabled "$servicio" &>/dev/null || systemctl is-active "$servicio" &>/dev/null; then
        echo "Deteniendo $servicio"
        sudo systemctl stop "$servicio"
    fi
done

# Esperar un momento para asegurarse de que todos los servicios se detengan completamente
sleep 5

# Luego, iniciar todos los servicios
for servicio in $servicios; do
    echo "Iniciando $servicio"
    sudo systemctl start "$servicio"
    sudo systemctl enable "$servicio" &>/dev/null
done
 ;;
5)
wdp=/opt/wdp4
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-ysf.service"
# Recorre la lista de servicios y verifica si están activos
for servicio in $servicios; do
    if systemctl is-enabled "$servicio" &>/dev/null || systemctl is-active "$servicio" &>/dev/null; then
        echo "stop $servicio"
        sudo systemctl disable "$servicio"
        sudo systemctl stop "$servicio"
    fi
done
 ;;
6)
wdp=/opt/wdp4
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-ysf.service"

# Primero, detener todos los servicios
for servicio in $servicios; do
    if systemctl is-enabled "$servicio" &>/dev/null || systemctl is-active "$servicio" &>/dev/null; then
        echo "Deteniendo $servicio"
        sudo systemctl stop "$servicio"
    fi
done

# Esperar un momento para asegurarse de que todos los servicios se detengan completamente
sleep 5

# Luego, iniciar todos los servicios
for servicio in $servicios; do
    echo "Iniciando $servicio"
    sudo systemctl start "$servicio"
    sudo systemctl enable "$servicio" &>/dev/null
done
 ;;
7)
sudo nano /opt/WSYSFDash/html/index.html ;;
8)
nano /opt/WSYSFDash/html/js/config.js ;;
9)
break;
esac
done
exit 0
EOF
#########
chmod +x /bin/menu-ysf
ln -sf /bin/menu-ysf /bin/MENU-YSF
chmod +x /bin/MENU*
