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

Web-Dashboar-name: adn
Web-Dashboar-Port:  80
Web-Dashboar-dns:
Web-Dashboar-dir: /var/www/fdmr2
Web-Dashboar-select: fdmr_mon2
Web-Dashboar-soft: /opt/FDMR-Monitor2
 
EOFX
fi
if ! grep -q "Web-Dashboar-name: adn" "/opt/wdp" > /dev/null 2>&1; then
   sed -i "s|Web-Dashboar-name:.*|Web-Dashboar-name: adn|g" /opt/wdp
fi
if [ -f "/etc/nginx/sites-enabled/freedmr" ]
then
  mv /etc/nginx/sites-enabled/freedmr /etc/nginx/sites-enabled/adn &&
  sudo systemctl restart nginx
fi
sudo cat > /bin/menu-adn <<- "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 0
fi

while : ; do
    choix=$(whiptail --title "ADN Server" --menu "move up or down with the keyboard arrows and select your option by pressing enter:" 23 56 13 \
        1 " Edit ADN-Server " \
        2 " Edit Monitor " \
        3 " Start-Restart SERVER  " \
        4 " Stop SERVER " \
        5 " Select Dashboard " \
        6 " Stop Dashboard " \
        7 " D-APRS " \
        8 " Menu update " 3>&1 1>&2 2>&3)
    exitstatus=$?

    if [ $exitstatus = 0 ]; then
        echo "Your chosen option:" $choix
    else
        echo "You chose cancel."
        break
    fi

    case $choix in
    1)
        sudo nano /opt/ADN-DMR-Peer-Server/config/adn.cfg &&
        if sudo systemctl status adn-server.service | grep "service; enabled;" >/dev/null 2>&1
        then 
            sudo systemctl stop adn-server.service
            sudo systemctl start adn-server.service
        fi
        if sudo systemctl status fdmr_mon.service |grep "service; enabled;" >/dev/null 2>&1
           then 
            sudo systemctl stop fdmr_mon.service
            sudo systemctl start fdmr_mon.service
        fi
        if sudo systemctl status fdmr_mon2.service |grep "service; enabled;" >/dev/null 2>&1
           then 
           sudo systemctl stop fdmr_mon2.service
           sudo systemctl start fdmr_mon2.service
        fi
        ;;
    2)
        network=$(whiptail --title "Easy ADN Server" --menu "Select a option:" 10 60 3 \
            "1" "Edit FDMR-Monitor OA4DOA" \
            "2" "Edit FDMR-Monitor CS8ABG" \
            "3" "Edit Web Port & dns" 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/FDMR-Monitor/fdmr-mon.cfg ;;
            "2")
                sudo nano /opt/FDMR-Monitor2/fdmr-mon.cfg ;;
            "3")
                wdp=/opt/wdp
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 fdmr_mon2"

# 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
    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|g" $wdp
        sed -i "s|Web-Dashboar-select:.*|Web-Dashboar-select: fdmr_mon|g" $wdp
        (crontab -l; echo "* */24 * * * rm /opt/FDMR-Monitor/data/*") | awk '!x[$0]++' | crontab -
        (crontab -l; echo "*/2 * * * * sh /opt/FDMR-Monitor/sysinfo/cpu.sh") | awk '!x[$0]++' | crontab -
        (crontab -l; echo "*/5 * * * * sh /opt/FDMR-Monitor/sysinfo/graph.sh") | awk '!x[$0]++' | crontab -
        (crontab -l | grep -v "rm /opt/FDMR-Monitor2/data/*") | crontab -
        ;;
    "2")
        sed -i "s|Web-Dashboar-dir:.*|Web-Dashboar-dir: /var/www/fdmr2|g" $wdp
        sed -i "s|Web-Dashboar-select:.*|Web-Dashboar-select: fdmr_mon2|g" $wdp
        (crontab -l; echo "* */24 * * * rm /opt/FDMR-Monitor2/data/*") | awk '!x[$0]++' | crontab -
        (crontab -l | grep -v "rm /opt/FDMR-Monitor/data/*") | crontab -
        (crontab -l | grep -v "sh /opt/FDMR-Monitor/sysinfo/cpu.sh") | crontab -
        (crontab -l | grep -v "sh /opt/FDMR-Monitor/sysinfo/graph.sh") | 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 is-enabled "$serd0" &>/dev/null || systemctl is-active "$serd0" &>/dev/null; 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
        if ! systemctl status mariadb.service | grep "Active: active" >/dev/null 2>&1
        then 
           sudo systemctl stop mariadb.service
           sudo systemctl start mariadb.service
        fi
        if ! systemctl status mariadb.service | grep "service; enabled;" >/dev/null 2>&1
        then 
            sudo systemctl enable mariadb.service
        fi
sofd0=$(grep "Web-Dashboar-soft:" $wdp | grep -oP 'Web-Dashboar-soft:\s*\K.*')
rm $sofd0/data/*
                    mysql -u root -e "DROP TABLE IF EXISTS hbmon.last_heard;"
                    mysql -u root -e "DROP TABLE IF EXISTS hbmon.lstheard_log;"
                    mysql -u root -e "DROP TABLE IF EXISTS hbmon.peer_ids;"
                    mysql -u root -e "DROP TABLE IF EXISTS hbmon.subscriber_ids;"
                    mysql -u root -e "DROP TABLE IF EXISTS hbmon.talkgroup_ids;"
                    mysql -u root -e "DROP TABLE IF EXISTS hbmon.tg_count;"
                    mysql -u root -e "DROP TABLE IF EXISTS hbmon.user_count;"                    
                    cd $sofd0
                    python3 mon_db.py --create
                    python3 mon_db.py --update
    whiptail --title "adn Dashboard" --msgbox "Configuracion finalizada" 0 50
                ;;
            *)
                echo "Invalid selection"
                ;;
        esac
        ;;
    3)
        if [ "$(ls -A "/opt/ADN-DMR-Peer-Server/data/")" ]; then
              rm -rf "/opt/ADN-DMR-Peer-Server/data/"*
        fi
        sudo systemctl stop adn-parrot.service
        sudo systemctl stop proxy.service
        sudo systemctl stop adn-server.service
        if ! systemctl status mariadb.service | grep "Active: active" >/dev/null 2>&1
        then 
           sudo systemctl stop mariadb.service
           sudo systemctl start mariadb.service
        fi
        if ! systemctl status mariadb.service | grep "service; enabled;" >/dev/null 2>&1
        then 
            sudo systemctl enable mariadb.service
        fi  
        if sudo systemctl status fdmr_mon.service |grep "service; enabled;" >/dev/null 2>&1
           then 
            sudo systemctl stop fdmr_mon.service
            sudo systemctl start fdmr_mon.service
        fi
        if sudo systemctl status fdmr_mon2.service |grep "service; enabled;" >/dev/null 2>&1
           then 
           sudo systemctl stop fdmr_mon2.service
           sudo systemctl start fdmr_mon2.service
        fi
        sudo systemctl start adn-server.service
        sudo systemctl start adn-parrot.service
        sudo systemctl start proxy.service
        sudo systemctl enable adn-parrot.service
        sudo systemctl enable proxy.service
        sudo systemctl enable adn-server.service
        ;;
    4)
        sudo systemctl stop adn-parrot.service
        sudo systemctl disable adn-parrot.service
        sudo systemctl stop proxy.service
        sudo systemctl disable proxy.service
        sudo systemctl stop adn-server.service
        sudo systemctl disable adn-server.service 

  if systemctl is-enabled mariadb.service | grep -q "disabled"; then
    echo "El servicio mariadb está deshabilitado."
else
    if systemctl is-enabled adn-server.service | grep -q "enabled"; then
        echo "El servicio adn está habilitado."
    else
        if [ -e "/etc/nginx/sites-enabled/adn" ]; then
            echo "El archivo /etc/nginx/sites-enabled/adn existe."
        else
            systemctl disable mariadb.service
            systemctl stop mariadb.service
            echo "El servicio mariadb ha sido deshabilitado."
        fi
    fi
fi
        ;;
    5)
     wdp=/opt/wdp
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 fdmr_mon2"

# 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
    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|g" $wdp
        sed -i "s|Web-Dashboar-select:.*|Web-Dashboar-select: fdmr_mon|g" $wdp
        (crontab -l; echo "* */24 * * * rm /opt/FDMR-Monitor/data/*") | awk '!x[$0]++' | crontab -
        (crontab -l; echo "*/2 * * * * sh /opt/FDMR-Monitor/sysinfo/cpu.sh") | awk '!x[$0]++' | crontab -
        (crontab -l; echo "*/5 * * * * sh /opt/FDMR-Monitor/sysinfo/graph.sh") | awk '!x[$0]++' | crontab -
        (crontab -l | grep -v "rm /opt/FDMR-Monitor2/data/*") | crontab -
        ;;
    "2")
        sed -i "s|Web-Dashboar-dir:.*|Web-Dashboar-dir: /var/www/fdmr2|g" $wdp
        sed -i "s|Web-Dashboar-select:.*|Web-Dashboar-select: fdmr_mon2|g" $wdp
        (crontab -l; echo "* */24 * * * rm /opt/FDMR-Monitor2/data/*") | awk '!x[$0]++' | crontab -
        (crontab -l | grep -v "rm /opt/FDMR-Monitor/data/*") | crontab -
        (crontab -l | grep -v "sh /opt/FDMR-Monitor/sysinfo/cpu.sh") | crontab -
        (crontab -l | grep -v "sh /opt/FDMR-Monitor/sysinfo/graph.sh") | 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 is-enabled "$serd0" &>/dev/null || systemctl is-active "$serd0" &>/dev/null; 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
        if ! systemctl status mariadb.service | grep "Active: active" >/dev/null 2>&1
        then 
           sudo systemctl stop mariadb.service
           sudo systemctl start mariadb.service
        fi
        if ! systemctl status mariadb.service | grep "service; enabled;" >/dev/null 2>&1
        then 
            sudo systemctl enable mariadb.service
        fi
sofd0=$(grep "Web-Dashboar-soft:" $wdp | grep -oP 'Web-Dashboar-soft:\s*\K.*')
rm $sofd0/data/*
                    mysql -u root -e "DROP TABLE IF EXISTS hbmon.last_heard;"
                    mysql -u root -e "DROP TABLE IF EXISTS hbmon.lstheard_log;"
                    mysql -u root -e "DROP TABLE IF EXISTS hbmon.peer_ids;"
                    mysql -u root -e "DROP TABLE IF EXISTS hbmon.subscriber_ids;"
                    mysql -u root -e "DROP TABLE IF EXISTS hbmon.talkgroup_ids;"
                    mysql -u root -e "DROP TABLE IF EXISTS hbmon.tg_count;"
                    mysql -u root -e "DROP TABLE IF EXISTS hbmon.user_count;"
                    cd $sofd0
                    python3 mon_db.py --create
                    python3 mon_db.py --update  
        ;;
    6)
    wdp=/opt/wdp
    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-Monitor2/data/*") | crontab -
        (crontab -l | grep -v "rm /opt/FDMR-Monitor/data/*") | crontab -
        (crontab -l | grep -v "sh /opt/FDMR-Monitor/sysinfo/cpu.sh") | crontab -
        (crontab -l | grep -v "sh /opt/FDMR-Monitor/sysinfo/graph.sh") | crontab -
        sudo systemctl stop fdmr_mon2.service
        sudo systemctl disable fdmr_mon2.service
        sudo systemctl stop fdmr_mon.service
        sudo systemctl disable fdmr_mon.service

        if systemctl is-enabled mariadb.service | grep -q "disabled"; then
            echo "El servicio mariadb está deshabilitado."
        else
            if systemctl is-enabled adn-server.service | grep -q "enabled"; then
                echo "El servicio adn está habilitado."
            else
                if systemctl is-enabled fdmr_mon.service | grep -q "enabled"; then
                    echo "El servicio fdmr_mon está habilitado."
                else
                    if systemctl is-enabled fdmr_mon2.service | grep -q "enabled"; then
                        echo "El servicio fdmr_mon2 está habilitado."
                    else
                        systemctl disable mariadb.service
                        systemctl stop mariadb.service
                        echo "El servicio mariadb ha sido deshabilitado."
                    fi
                fi
            fi
        fi
        ;;
    7)
        menu-igate ;;
    8)
        bash -c "$(curl -fsSL https://gitlab.com/hp3icc/fdmr/-/raw/main/update.sh)" ;;
    esac
done

exit 0


EOF
################ 
sudo chmod +x /bin/menu*
ln -sf /bin/menu-adn /bin/MENU-ADN
ln -sf /bin/menu-adn /bin/menu-fdmr
ln -sf /bin/menu-fdmr /bin/MENU-FDMR
chmod +x /bin/MENU*