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

Web-Dashboar-name: direwolf
Web-Dashboar-Port:  80
Web-Dashboar-dns:
Web-Dashboar-dir: /opt/direwolf_webstat

EOFX
fi
####menu-dw
cat > /bin/menu-dw <<- "EOF"
#!/bin/bash
while : ; do
choix=$(whiptail --title "Raspbian Proyect HP3ICC Menu Direwolf" --menu "Suba o Baje con las flechas del teclado y seleccione el numero de opcion" 22 56 13 \
1 " Edit Direwolf Analogo " \
2 " Edit Direwolf SDR-RTL " \
3 " Edit Frecuency SDR-RTL " \
4 " Edit Direwolf Tracker " \
5 " Edit Dashboard " \
6 " Select Web Dashboard Port & dns" \
7 " Start Direwolf SDR-RTL" \
8 " Start Direwolf Tracker" \
9 " Start Direwolf Analogo" \
10 " Start Direwolf Analogo + Dashboard" \
11 " Start Direwolf SDR-RTL + Dashboard" \
12 " Stop All Direwolf" \
13 " 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/direwolf/dw.conf;;
2)
sudo nano /opt/direwolf/sdr.conf;;
3)
sudo nano /opt/direwolf/rtl.sh ;;
4)
sudo nano /opt/direwolf/gpsd.conf;;
5)
sudo nano  /opt/direwolf_webstat/config.php;;
6)
wdp=/opt/wdp2
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
 ;;
7)
# Lista de servicios a reiniciar
servicios="gpsd.service direwolf.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
wdp=/opt/wdp2
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
if [ "$(cat /proc/cpuinfo | grep 'Zero')" != "" ]; then
 
   sudo cp /opt/direwolf/direwolf1 /usr/local/bin/direwolf
   sudo chmod +x /usr/local/bin/direwolf
else
   sudo cp /opt/direwolf/direwolf2 /usr/local/bin/direwolf
   sudo chmod +x /usr/local/bin/direwolf
fi
   sudo sed -i "s/ -l \/var\/log\/direwolf/#-l \/var\/log\/direwolf/" /opt/direwolf/rtl.sh
   sudo sed -i "s/ExecStart=.*/ExecStart=\/bin\/sh \/opt\/direwolf\/rtl.sh/" /lib/systemd/system/direwolf.service
   sudo systemctl daemon-reload
   sudo systemctl start direwolf.service
   sudo systemctl enable direwolf.service ;;
8)
# Lista de servicios a reiniciar
servicios="gpsd.service direwolf.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
wdp=/opt/wdp2
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
if systemctl status gpsd.service | grep inactive >/dev/null 2>&1
then 
   sudo systemctl stop gpsd.socket gpsd.service &&
   sudo systemctl start gpsd.socket gpsd.service
fi
if systemctl status gpsd.service | grep disable >/dev/null 2>&1
then 
   sudo systemctl enable gpsd.socket gpsd.service
fi
if [ "$(cat /proc/cpuinfo | grep 'Zero')" != "" ]; then
 
   sudo cp /opt/direwolf/direwolf1 /usr/local/bin/direwolf
   sudo chmod +x /usr/local/bin/direwolf
else
   sudo cp /opt/direwolf/direwolf2 /usr/local/bin/direwolf
   sudo chmod +x /usr/local/bin/direwolf
fi
   sudo sed -i "s/ExecStart=.*/ExecStart=\/usr\/local\/bin\/direwolf -t 0 -c \/opt\/direwolf\/gpsd.conf/" /lib/systemd/system/direwolf.service
   sudo systemctl daemon-reload
   sudo systemctl start direwolf.service
   sudo systemctl enable direwolf.service ;;
9)
# Lista de servicios a reiniciar
servicios="gpsd.service direwolf.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
wdp=/opt/wdp2
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
if [ "$(cat /proc/cpuinfo | grep 'Zero')" != "" ]; then
 
   sudo cp /opt/direwolf/direwolf1 /usr/local/bin/direwolf
   sudo chmod +x /usr/local/bin/direwolf
else
   sudo cp /opt/direwolf/direwolf2 /usr/local/bin/direwolf
   sudo chmod +x /usr/local/bin/direwolf
fi  
   sudo sed -i "s/ExecStart=.*/ExecStart=sudo direwolf -t 0 -c \/opt\/direwolf\/dw.conf/" /lib/systemd/system/direwolf.service
   sudo systemctl daemon-reload
   sudo systemctl start direwolf.service
   sudo systemctl enable direwolf.service ;;
10)
# Lista de servicios a reiniciar
servicios="gpsd.service direwolf.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
wdp=/opt/wdp2
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
  if [ "$(cat /proc/cpuinfo | grep 'Zero')" != "" ]; then
 
   sudo cp /opt/direwolf/direwolf1 /usr/local/bin/direwolf
   sudo chmod +x /usr/local/bin/direwolf
  else
   sudo cp /opt/direwolf/direwolf2 /usr/local/bin/direwolf
   sudo chmod +x /usr/local/bin/direwolf
  fi
   sudo sed -i "s/ExecStart=.*/ExecStart=\/usr\/local\/bin\/direwolf -t 0 -l \/var\/log\/direwolf -c \/opt\/direwolf\/dw.conf/" /lib/systemd/system/direwolf.service
   sudo systemctl daemon-reload
   sudo systemctl start direwolf.service
   sudo systemctl enable direwolf.service 
 ;;
11)
# Lista de servicios a reiniciar
servicios="gpsd.service direwolf.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
wdp=/opt/wdp2
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
    if [ "$(cat /proc/cpuinfo | grep 'Zero')" != "" ]; then
 
     sudo cp /opt/direwolf/direwolf1 /usr/local/bin/direwolf
     sudo chmod +x /usr/local/bin/direwolf
    else
      sudo cp /opt/direwolf/direwolf2 /usr/local/bin/direwolf
      sudo chmod +x /usr/local/bin/direwolf
    fi
   sudo sed -i "s/#-l \/var\/log\/direwolf/ -l \/var\/log\/direwolf/" /opt/direwolf/rtl.sh
   sudo sed -i "s/ExecStart=.*/ExecStart=\/bin\/sh \/opt\/direwolf\/rtl.sh/" /lib/systemd/system/direwolf.service
   sudo systemctl daemon-reload
   sudo systemctl start direwolf.service
   sudo systemctl enable direwolf.service 
  ;;
12)
wdp=/opt/wdp2
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="gpsd.service direwolf.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
 ;;
13)
break;
esac
done
exit 0
EOF
##
chmod +x /bin/menu*
ln -sf /bin/menu-dw /bin/MENU-DW
chmod +x /bin/MENU*