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

Web-Dashboar-name: freedmr-bidge
Web-Dashboar-Port:  80
Web-Dashboar-dns:
Web-Dashboar-dir: /var/www/fdmr-b2
Web-Dashboar-select: fdmr_mon-b2
Web-Dashboar-soft: /opt/FDMR-Monitor-B2

EOFX
fi
sudo cat > /bin/menu-fdmr-b <<- "EOF"
#!/bin/bash
if [ $EUID -ne 0 ]; then
    whiptail --title "sudo su" --msgbox "requiere ser usuario root , escriba (sudo su) antes de entrar a menu / requires root user, type (sudo su) before entering menu" 0 50
    exit 1
fi
while : ; do
choix=$(whiptail --title "Raspbian Proyect HP3ICC FreeDMR-Bridge" --menu "move up or down with the keyboard arrows and select your option by pressing enter:" 23 56 13 \
1 " Edit FreeDMR-Bridger " \
2 " Edit Rules " \
3 " Edit FDMR-Monitor " \
4 " Start Dashboard " \
5 " Start-Restart FreeDMR-Bridger  " \
6 " Stop FreeDMR-Bridge " \
7 " Stop Dashboard " \
8 " Proxy " \
9 " Menu update " 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/FreeDMR-Bridge/config/FreeDMR.cfg &&
if sudo systemctl status freedmr-bridge.service |grep "service; enabled;" >/dev/null 2>&1
   then 
   sudo systemctl stop freedmr-bridge.service
   sudo systemctl start freedmr-bridge.service
fi
if sudo systemctl status fdmr_mon-b.service |grep "service; enabled;" >/dev/null 2>&1
   then 
   sudo systemctl stop fdmr_mon-b.service
   sudo systemctl start fdmr_mon-b.service
fi
if sudo systemctl status fdmr_mon-b2.service |grep "service; enabled;" >/dev/null 2>&1
   then 
   sudo systemctl stop fdmr_mon-b2.service
   sudo systemctl start fdmr_mon-b2.service
fi ;;
2)
sudo nano /opt/FreeDMR-Bridge/config/rules.py &&
if sudo systemctl status freedmr-bridge.service |grep "service; enabled;" >/dev/null 2>&1
   then 
   sudo systemctl stop freedmr-bridge.service
   sudo systemctl start freedmr-bridge.service
fi
if sudo systemctl status fdmr_mon-b.service |grep "service; enabled;" >/dev/null 2>&1
   then 
   sudo systemctl stop fdmr_mon-b.service
   sudo systemctl start fdmr_mon-b.service
fi
if sudo systemctl status fdmr_mon-b2.service |grep "service; enabled;" >/dev/null 2>&1
   then 
   sudo systemctl stop fdmr_mon-b2.service
   sudo systemctl start fdmr_mon-b2.service
fi ;;
3)
    network=$(whiptail --title "Easy FreeDMR Bridge" --menu "Select a option:" 10 60 3 \
    "1" "Edit FDMR-Monitor OA4DOA" \
    "2" "Edit FDMR-Monitor CS8ABG" \
    "3" "Edit Web Port" 3>&1 1>&2 2>&3)
    
    if [ $? != 0 ]; then
        echo "You chose cancel. Returning to main menu."
        continue  # Regresar al inicio del bucle sin procesar el resto del caso 1
    fi

    case "$network" in
    "1")
        sudo nano /opt/FDMR-Monitor-B/fdmr-mon.cfg ;;
    "2")
        sudo nano /opt/FDMR-Monitor-B2/fdmr-mon.cfg ;;
    "3")
       wdp=/opt/wdp7
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
servicios=("fdmr_mon-b" "fdmr_mon-b2")

# 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
    network=$(whiptail --title "Setup http Port & DNS" --menu "Select Dashboard:" 10 60 2 \
    "1" "Dashboard OA4DOA" \
    "2" "Dashboard CS8ABG" 3>&1 1>&2 2>&3)
    
    if [ $? != 0 ]; then
        echo "You chose cancel. Returning to main menu."
        continue  # Regresar al inicio del bucle sin procesar el resto del caso 1
    fi

    case "$network" in
    "1")
        sed -i "s|Web-Dashboar-dir:.*|Web-Dashboar-dir: /var/www/fdmr-b|g" $wdp
        sed -i "s|Web-Dashboar-select:.*|Web-Dashboar-select: fdmr_mon-b|g" $wdp
        (crontab -l | grep -v "rm /opt/FDMR-Monitor-B2/data/*") | crontab -
        (crontab -l; echo "* */24 * * * rm /opt/FDMR-Monitor-B/data/*")|awk '!x[$0]++'|crontab -
        (crontab -l; echo "*/2 * * * * sh /opt/FDMR-Monitor-B/sysinfo/cpu.sh")|awk '!x[$0]++'|crontab -
        (crontab -l; echo "*/5 * * * * sh /opt/FDMR-Monitor-B/sysinfo/graph.sh")|awk '!x[$0]++'|crontab -
        ;;
    "2")
        sed -i "s|Web-Dashboar-dir:.*|Web-Dashboar-dir: /var/www/fdmr-b2|g" $wdp
        sed -i "s|Web-Dashboar-select:.*|Web-Dashboar-select: fdmr_mon-b2|g" $wdp
        (crontab -l | grep -v "rm /opt/FDMR-Monitor-B/data/*") | crontab -
        (crontab -l | grep -v "sh /opt/FDMR-Monitor-B/sysinfo/cpu.sh") | crontab -
        (crontab -l | grep -v "sh /opt/FDMR-Monitor-B/sysinfo/graph.sh") | crontab -
        (crontab -l; echo "* */24 * * * rm /opt/FDMR-Monitor-B2/data/*")|awk '!x[$0]++'|crontab -
        ;;
    *)
        echo "Invalid selection"
        ;;
    esac

    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
serd0=$(grep "Web-Dashboar-select:" $wdp | grep -oP 'Web-Dashboar-select:\s*\K.*')
    if systemctl status "$serd0" | grep "service; enabled;" >/dev/null 2>&1; then
        echo "restart $serd0"
        sudo systemctl stop "$serd0"
        sudo systemctl start "$serd0"
      else
         echo "start $serd0"
        sudo systemctl stop "$serd0"
        sudo systemctl start "$serd0"
        sudo systemctl enable "$serd0"        
    fi
sofd0=$(grep "Web-Dashboar-soft:" $wdp | grep -oP 'Web-Dashboar-soft:\s*\K.*')
rm $sofd0/data/*
cd $sofd0
python3 mon_db.py --create
python3 mon_db.py --update
    whiptail --title "FreeDMR Dashboard" --msgbox "Configuracion finalizada" 0 50
                ;;
    *)
        echo "Invalid selection"
        ;;
    esac
    ;;
4)
  wdp=/opt/wdp7
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
servicios=("fdmr_mon-b" "fdmr_mon-b2")

# 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
    network=$(whiptail --title "Setup http Port & DNS" --menu "Select Dashboard:" 10 60 2 \
    "1" "Dashboard OA4DOA" \
    "2" "Dashboard CS8ABG" 3>&1 1>&2 2>&3)
    
    if [ $? != 0 ]; then
        echo "You chose cancel. Returning to main menu."
        continue  # Regresar al inicio del bucle sin procesar el resto del caso 1
    fi

    case "$network" in
    "1")
        sed -i "s|Web-Dashboar-dir:.*|Web-Dashboar-dir: /var/www/fdmr-b|g" $wdp
        sed -i "s|Web-Dashboar-select:.*|Web-Dashboar-select: fdmr_mon-b|g" $wdp
        (crontab -l | grep -v "rm /opt/FDMR-Monitor-B2/data/*") | crontab -
        (crontab -l; echo "* */24 * * * rm /opt/FDMR-Monitor-B/data/*")|awk '!x[$0]++'|crontab -
        (crontab -l; echo "*/2 * * * * sh /opt/FDMR-Monitor-B/sysinfo/cpu.sh")|awk '!x[$0]++'|crontab -
        (crontab -l; echo "*/5 * * * * sh /opt/FDMR-Monitor-B/sysinfo/graph.sh")|awk '!x[$0]++'|crontab -
        ;;
    "2")
        sed -i "s|Web-Dashboar-dir:.*|Web-Dashboar-dir: /var/www/fdmr-b2|g" $wdp
        sed -i "s|Web-Dashboar-select:.*|Web-Dashboar-select: fdmr_mon-b2|g" $wdp
        (crontab -l | grep -v "rm /opt/FDMR-Monitor-B/data/*") | crontab -
        (crontab -l | grep -v "sh /opt/FDMR-Monitor-B/sysinfo/cpu.sh") | crontab -
        (crontab -l | grep -v "sh /opt/FDMR-Monitor-B/sysinfo/graph.sh") | crontab -
        (crontab -l; echo "* */24 * * * rm /opt/FDMR-Monitor-B2/data/*")|awk '!x[$0]++'|crontab -
        ;;
    *)
        echo "Invalid selection"
        ;;
    esac

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
serd0=$(grep "Web-Dashboar-select:" $wdp | grep -oP 'Web-Dashboar-select:\s*\K.*')
    if systemctl status "$serd0" | grep "service; enabled;" >/dev/null 2>&1; then
        echo "restart $serd0"
        sudo systemctl stop "$serd0"
        sudo systemctl start "$serd0"
      else
         echo "start $serd0"
        sudo systemctl stop "$serd0"
        sudo systemctl start "$serd0"
        sudo systemctl enable "$serd0"        
    fi
sofd0=$(grep "Web-Dashboar-soft:" $wdp | grep -oP 'Web-Dashboar-soft:\s*\K.*')
rm $sofd0/data/*
cd $sofd0
python3 mon_db.py --create
python3 mon_db.py --update
    ;;
5)
 sudo systemctl enable freedmr-bridge.service
 sudo systemctl stop freedmr-bridge.service
 sudo systemctl start freedmr-bridge.service
 if sudo systemctl status fdmr_mon-b.service |grep "service; enabled;" >/dev/null 2>&1
   then 
   sudo systemctl stop fdmr_mon-b.service
   sudo systemctl start fdmr_mon-b.service
fi
if sudo systemctl status fdmr_mon-b2.service |grep "service; enabled;" >/dev/null 2>&1
   then 
   sudo systemctl stop fdmr_mon-b2.service
   sudo systemctl start fdmr_mon-b2.service
fi ;;
6)
sudo systemctl stop freedmr-bridge.service
sudo systemctl disable freedmr-bridge.service ;;
7)
wdp=/opt/wdp7
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
(crontab -l | grep -v "rm /opt/FDMR-Monitor-B2/data/*") | crontab -
(crontab -l | grep -v "rm /opt/FDMR-Monitor-B/data/*") | crontab -
(crontab -l | grep -v "sh /opt/FDMR-Monitor-B/sysinfo/cpu.sh") | crontab -
(crontab -l | grep -v "sh /opt/FDMR-Monitor-B/sysinfo/graph.sh") | crontab -
sudo systemctl stop fdmr_mon-b2.service
sudo systemctl disable fdmr_mon-b2.service
sudo systemctl stop fdmr_mon-b.service
sudo systemctl disable fdmr_mon-b.service
;; 
8)
    network=$(whiptail --title "Easy FreeDMR Bridge" --menu "Select a option:" 10 60 4 \
    "1" "Wizar Config Proxy" \
    "2" "Start Proxy" \
    "3" "Stop Proxy" \
    "4" "Edit Proxy" 3>&1 1>&2 2>&3)
    
    if [ $? != 0 ]; then
        echo "You chose cancel. Returning to main menu."
        continue  # Regresar al inicio del bucle sin procesar el resto del caso 1
    fi

    case "$network" in
    "1") 
        whiptail --title "Wizar Config Proxy" --inputbox "External Port:" 10 60 2>/tmp/eport.txt
            exitstatus=$?
        if [ $exitstatus != 0 ]; then
            echo "You chose cancel. Exiting..."
            exit 1  # Salir del script con un código de error
        fi
    eport=$(cat /tmp/eport.txt)
    eport=$(echo "$eport" | tr '[:lower:]' '[:upper:]')
    whiptail --title "Wizar Config Proxy" --inputbox "Local Port:" 10 60 2>/tmp/iport.txt
            exitstatus=$?
        if [ $exitstatus != 0 ]; then
            echo "You chose cancel. Exiting..."
            exit 1  # Salir del script con un código de error
        fi
    iport=$(cat /tmp/iport.txt)
    whiptail --title "Wizar Config Proxy" --inputbox "Generator port, Sample: 100" 10 60 2>/tmp/genport.txt
            exitstatus=$?
        if [ $exitstatus != 0 ]; then
            echo "You chose cancel. Exiting..."
           exit 1  # Salir del script con un código de error
        fi
    genport=$(cat /tmp/genport.txt)
    generat=$((iport + genport - 1))
    
    sudo sed -i "s/LISTENPORT =.*/LISTENPORT = $eport/g" /opt/FreeDMR-Bridge/proxy.cfg
    sudo sed -i "s/DESTPORTSTART =.*/DESTPORTSTART = $iport/g" /opt/FreeDMR-Bridge/proxy.cfg
    sudo sed -i "s/DESTPORTEND = .*/DESTPORTEND = $generat/g" /opt/FreeDMR-Bridge/proxy.cfg

    rm -f /tmp/eport.txt /tmp/iport.txt /tmp/genport.txt

        systemctl stop proxy2.service
       if ! systemctl status proxy2.service |grep "service; enabled;" >/dev/null 2>&1
        then sudo systemctl enable proxy2.service

       fi
       systemctl start proxy2.service ;;
    "2")
        systemctl stop proxy2.service
       if ! systemctl status proxy2.service |grep "service; enabled;" >/dev/null 2>&1
        then sudo systemctl enable proxy2.service

       fi
       systemctl start proxy2.service ;;
    "3")
        systemctl disable proxy2.service && systemctl stop proxy2.service ;;
    "4")
       sudo nano /opt/FreeDMR-Bridge/proxy.cfg && 
       if systemctl status proxy2.service |grep "service; enabled;" >/dev/null 2>&1
        then 
           sudo systemctl stop proxy2.service
           sudo systemctl start proxy2.service

        fi ;;
    *)
        echo "Invalid selection"
        ;;
    esac
    ;;
9)
bash -c "$(curl -fsSL https://gitlab.com/hp3icc/fdmr/-/raw/main/update-b.sh)" ;;
esac
done
exit 0

EOF

################ 
sudo chmod +x /bin/menu*
ln -sf /bin/menu-fdmr-b /bin/MENU-FDMR-B
chmod +x /bin/MENU*