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

Web-Dashboar-name: XLX
Web-Dashboar-Port: 80
Web-Dashboar-dns:
Web-Dashboar-dir: /opt/xlxd/dashboard
XLX-DASH:
XLX-XRFNUM: 
XLX-CONTRIE: 
XLX-DESCRIPTION: 
XLX-NMODU: 
XLX-YSFMODU:
XLX-YSFPOR: 
XLX-AMBIP: 
XLX-AMBPOR: 
XLX-EMAIL: 
XLX-CALLSIGN: 
 


EOFX
fi
cat > /bin/menu-xlx <<- "EOF"
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
  whiptail --title "root" --msgbox "Cambie a usuario root / Change to user root" 0 50
  exit 0
fi
while : ; do
choix=$(whiptail --title "Script Proyect HP3ICC Menu Easy-XLX" --menu "Suba o Baje con las flechas del teclado y seleccione el numero de opcion" 15 50 6 \
1 " Easy Config XLX  " \
2 " Start & Restart XLX Reflector " \
3 " Stop XLX Reflector " \
4 " Select or Change Dashboard " \
5 " Active XLXD Reflector Public " \
6 " Menu Principal " 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)
wd0p=""
w0dn0=""
DASH=""
XRFNUM=""
CONTRIE=""
DESCRIPTION=""
NMODU=""
YSFMODU=""
YSFPOR=""
AMBIP=""
AMBPOR=""
EMAIL="" 
CALLSIGN=""
CALLHOME=""
wdp=/opt/wdp10
if [ ! -d "/opt/xlx-bk" ]; then
 mkdir /opt/xlx-bk
fi
source_folder="/xlxd"
backup_folder="/opt/xlx-bk"

# Lista de archivos para realizar backup
files_to_backup=(
    "xlxd.terminal"
    "xlxd.interlink"
    "xlxd.whitelist"
    "xlxd.blacklist"
)

# Verifica si la carpeta /xlxd/ no está vacía
if [ "$(ls -A $source_folder)" ]; then
    # La carpeta no está vacía, realiza la copia de archivos
    for file in "${files_to_backup[@]}"; do
        if [ -e "$source_folder/$file" ]; then
            cp "$source_folder/$file" "$backup_folder/$file"
            echo "Copia de $file realizada con éxito."
        else
            echo "El archivo $file no existe en la carpeta $source_folder."
        fi
    done
else
    # La carpeta está vacía
    echo "La carpeta $source_folder está vacía. No se realizará la copia."
fi
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 "Easy Setup XLXD" --inputbox "XLX uses 3 digit numbers :" 10 60 2>/tmp/XRFNUM.txt
    if [ $? -ne 0 ]; then
        echo "Has elegido cancelar. Saliendo..."
        break
    fi
    
    XRFNUM=XLX$(cat /tmp/XRFNUM.txt)
    XRFNUM=$(echo "$XRFNUM" | tr '[:lower:]' '[:upper:]')

    network=$(whiptail --title "Easy setup XLXD" --menu "Select Dashboard:" 10 60 2 \
    "1" "XLX Dashboard 1 Clasic" \
    "2" "XLX Dashboard 2 Dark" 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: /opt/xlxd/dashboard|g" $wdp
        ;;
    "2")
        sed -i "s|Web-Dashboar-dir:.*|Web-Dashboar-dir: /opt/xlxd/dashboard2|g" $wdp
        ;;
    *)
        echo "Invalid selection"
        ;;
    esac

    whiptail --title "Easy setup XLXD" --inputbox "Select HTTP port:" 10 60 2>/tmp/wd0p.txt
    if [ $? -ne 0 ]; then
        echo "Has elegido cancelar. Saliendo..."
        break
    fi
    
    wd0p=$(cat /tmp/wd0p.txt)

    whiptail --title "Easy Setup XLXD" --inputbox "DNS Dashboard :" 10 60 2>/tmp/XLXDOMAIN.txt
    if [ $? -ne 0 ]; then
        echo "Has elegido cancelar. Saliendo..."
        break
    fi
    
    XLXDOMAIN=$(cat /tmp/XLXDOMAIN.txt)

    DASH=$XLXDOMAIN
    if [[ ! "$XLXDOMAIN" =~ [a-zA-Z] ]]; then
    DASH=$(curl -s https://ifconfig.me)
    fi

    w0dn0=$XLXDOMAIN
    if [[ ! "$XLXDOMAIN" =~ [a-zA-Z] ]]; then
    w0dn0=""
    fi
    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
    whiptail --title "Easy setup XLXD" --inputbox "What contrie your server? :" 10 60 2>/tmp/CONTRIE.txt
    if [ $? -ne 0 ]; then
        echo "Has elegido cancelar. Saliendo..."
        break
    fi
    
    CONTRIE=$(cat /tmp/CONTRIE.txt)

    whiptail --title "Easy setup XLXD" --inputbox "description your server? :" 10 60 2>/tmp/DESCRIPTION.txt
    if [ $? -ne 0 ]; then
        echo "Has elegido cancelar. Saliendo..."
        break
    fi
    
    DESCRIPTION=$(cat /tmp/DESCRIPTION.txt)

    whiptail --title "Easy setup XLXD" --inputbox "Enabled modules. sample 10 :" 10 60 2>/tmp/NMODU.txt
    if [ $? -ne 0 ]; then
        echo "Has elegido cancelar. Saliendo..."
        break
    fi
    
    NMODU=$(cat /tmp/NMODU.txt)

    whiptail --title "Easy setup XLXD" --inputbox "Defauld YSF module. sample B :" 10 60 2>/tmp/YSFMODU.txt
    if [ $? -ne 0 ]; then
        echo "Has elegido cancelar. Saliendo..."
        break
    fi
    
    YSFMODU=$(cat /tmp/YSFMODU.txt)
    YSFMODU=$(echo "$YSFMODU" | tr '[:lower:]' '[:upper:]')

    whiptail --title "Easy setup XLXD" --inputbox "ysf port sample defauld 42000 :" 10 60 2>/tmp/YSFPOR.txt
    if [ $? -ne 0 ]; then
        echo "Has elegido cancelar. Saliendo..."
        break
    fi
    
    YSFPOR=$(cat /tmp/YSFPOR.txt)

    whiptail --title "Easy setup XLXD" --inputbox "ambe server addres sample defauld 127.0.0.1 :" 10 60 2>/tmp/AMBIP.txt
    if [ $? -ne 0 ]; then
        echo "Has elegido cancelar. Saliendo..."
        break
    fi
    
    AMBIP=$(cat /tmp/AMBIP.txt)

    whiptail --title "Easy setup XLXD" --inputbox "ambe server port sample defauld 10100 :" 10 60 2>/tmp/AMBPOR.txt
    if [ $? -ne 0 ]; then
        echo "Has elegido cancelar. Saliendo..."
        break
    fi
    
    AMBPOR=$(cat /tmp/AMBPOR.txt)

    whiptail --title "Easy setup XLXD" --inputbox "What E-Mail address can your users send questions to?  :" 10 60 2>/tmp/EMAIL.txt
    if [ $? -ne 0 ]; then
        echo "Has elegido cancelar. Saliendo..."
        break
    fi
    
    EMAIL=$(cat /tmp/EMAIL.txt)

    whiptail --title "Easy setup XLXD" --inputbox "What is the admins callsign?   :" 10 60 2>/tmp/CALLSIGN.txt
    if [ $? -ne 0 ]; then
        echo "Has elegido cancelar. Saliendo..."
        break
    fi
    CALLHOME=true

# Capturar la selección del menú
selection=$(whiptail --title "Easy setup XLXD" --menu "ACTIVE XLXD REFLECTOR PUBLIC:" 10 60 2 \
    "1" "No" \
    "2" "Yes" 3>&1 1>&2 2>&3)

# Verificar si el usuario canceló el menú
if [ $? -ne 0 ]; then
    echo "You chose cancel. Returning to the main menu."
    continue  # Regresa al inicio del bucle si estás dentro de uno
fi

# Procesar la selección del usuario
case "$selection" in
    "1")
       CALLHOME=false
        ;;
    "2")
        CALLHOME=true
        ;;
    *)
        echo "Invalid selection. Returning to the main menu."
        ;;
esac

    find /var/log/ -type f -name "xlx*" -exec rm -f {} +
    CALLSIGN=$(cat /tmp/CALLSIGN.txt)
    CALLSIGN=$(echo "$CALLSIGN" | tr '[:lower:]' '[:upper:]')
##################################################
if [ -z "$CONTRIE" ]
then CONTRIE=USA

fi
if [ -z "$DESCRIPTION" ]
then DESCRIPTION=XLXD_Reflector_Test

fi
if [ -z "$NMODU" ]
then NMODU=10

fi
if [ -z "$YSFMODU" ]
then YSFMODU=B

fi
if [ -z "$YSFPOR" ]
then YSFPOR=42000

fi
if [ -z "$AMBIP" ]
then AMBIP=127.0.0.1

fi
if [ -z "$AMBPOR" ]
then AMBPOR=10100

fi
if [ -z "$EMAIL" ] 
then EMAIL=xlx@xlxd.net  

fi
if [ -z "$CALLSIGN" ]
then CALLSIGN=LX1IQ

fi
#########################
cp /opt/xlx-bk/main.h /opt/xlxd/src/main.h 
cp /opt/xlx-bk/config.inc1.php /opt/xlxd/dashboard/pgs/config.inc.php
cp /opt/xlx-bk/config.inc2.php /opt/xlxd/dashboard2/pgs/config.inc.php
#wpd
sed -i "s|Web-Dashboar-Port:.*|Web-Dashboar-Port: $wd0p|g" $wdp
sed -i "s|Web-Dashboar-dns:.*|Web-Dashboar-dns: $w0dn0|g" $wdp
sed -i "s|XLX-DASH:.*|XLX-DASH: $DASH|g" $wdp
sed -i "s|XLX-XRFNUM:.*|XLX-XRFNUM: $XRFNUM|g" $wdp
sed -i "s|XLX-XRFNUM:.*|XLX-XRFNUM: $XRFNUM|g" $wdp
sed -i "s|XLX-CONTRIE:.*|XLX-CONTRIE: $CONTRIE|g" $wdp
sed -i "s|XLX-DESCRIPTION:.*|XLX-DESCRIPTION: $DESCRIPTION|g" $wdp
sed -i "s|XLX-NMODU:.*|XLX-NMODU: $NMODU|g" $wdp
sed -i "s|XLX-YSFMODU:.*|XLX-YSFMODU: $YSFMODU|g" $wdp
sed -i "s|XLX-YSFPOR:.*|XLX-YSFPOR: $YSFPOR|g" $wdp
sed -i "s|XLX-AMBIP:.*|XLX-AMBIP: $AMBIP|g" $wdp
sed -i "s|XLX-AMBPOR:.*|XLX-AMBPOR: $AMBPOR|g" $wdp
sed -i "s|XLX-EMAIL:.*|XLX-EMAIL: $EMAIL|g" $wdp
sed -i "s|XLX-CALLSIGN:.*|XLX-CALLSIGN: $CALLSIGN|g" $wdp 
#dash1
sed -i "s|<title><?php echo.*|<title><?php echo \$Reflector->GetReflectorName(); ?> Reflector Dashboard / $DESCRIPTION</title>|" /opt/xlxd/dashboard/index.php
sed -i "s|NumberOfModules.*|NumberOfModules']                      = $NMODU;|g" /opt/xlxd/dashboard/pgs/config.inc.php
sed -i "s|'Country.*|'Country']                              = \"$CONTRIE\";|g" /opt/xlxd/dashboard/pgs/config.inc.php
sed -i "s|'Comment.*|'Comment']                              = \"$DESCRIPTION\";|g" /opt/xlxd/dashboard/pgs/config.inc.php
sed -i "s|'CustomTXT.*|'CustomTXT']                            = '$DESCRIPTION';|g" /opt/xlxd/dashboard/pgs/config.inc.php
sed -i "s|'ContactEmail.*|'ContactEmail']                         = '$EMAIL';|g" /opt/xlxd/dashboard/pgs/config.inc.php
sed -i "s|'MetaAuthor.*|'MetaAuthor']                           = '$CALLSIGN';|g" /opt/xlxd/dashboard/pgs/config.inc.php
sed -i "s|CallingHome\['MyDashBoardURL.*|CallingHome\['MyDashBoardURL'\]\                       = 'http:\/\/$DASH';|" /opt/xlxd/dashboard/pgs/config.inc.php
sed -i "s|'Active.*|'Active']                               = $CALLHOME;|g" /opt/xlxd/dashboard/pgs/config.inc.php
#dash2
sed -i "s|<title><?php echo.*|<title><?php echo \$Reflector->GetReflectorName(); ?> Reflector Dashboard / $DESCRIPTION</title>|" /opt/xlxd/dashboard2/index.php
sed -i "s|NumberOfModules.*|NumberOfModules']                      = $NMODU;|g" /opt/xlxd/dashboard2/pgs/config.inc.php
sed -i "s|'Country.*|'Country']                              = \"$CONTRIE\";|g" /opt/xlxd/dashboard2/pgs/config.inc.php
sed -i "s|'Comment.*|'Comment']                              = \"$DESCRIPTION\";|g" /opt/xlxd/dashboard2/pgs/config.inc.php
sed -i "s|'CustomTXT.*|'CustomTXT']                            = '$DESCRIPTION';|g" /opt/xlxd/dashboard2/pgs/config.inc.php
sed -i "s|'ContactEmail.*|'ContactEmail']                         = '$EMAIL';|g" /opt/xlxd/dashboard2/pgs/config.inc.php
sed -i "s|'MetaAuthor.*|'MetaAuthor']                           = '$CALLSIGN';|g" /opt/xlxd/dashboard2/pgs/config.inc.php
sed -i "s|CallingHome\['MyDashBoardURL.*|CallingHome\['MyDashBoardURL'\]\                       = 'http:\/\/$DASH';|" /opt/xlxd/dashboard2/pgs/config.inc.php
sed -i "s|'Active.*|'Active']                               = $CALLHOME;|g" /opt/xlxd/dashboard2/pgs/config.inc.php
#
########
sed -i "s|define YSF_PORT.*|define YSF_PORT                        $YSFPOR|g"  /opt/xlxd/src/main.h
sed -i "s|MODULE .*|MODULE             '$YSFMODU'|g"  /opt/xlxd/src/main.h
sed -i "s|define NB_OF_MODULES.*|define NB_OF_MODULES                   $NMODU|g"  /opt/xlxd/src/main.h
sed -i "s|define NB_MODULES_MAX.*|define NB_MODULES_MAX                   $NMODU|g"  /opt/xlxd/src/main.h
sed -i "s|TRANSCODER_PORT.*|TRANSCODER_PORT                 $AMBPOR|g"  /opt/xlxd/src/main.h
sed -i "s|YSF_DEFAULT_NODE_TX_FREQ.*|YSF_DEFAULT_NODE_TX_FREQ        434000000|g"  /opt/xlxd/src/main.h
sed -i "s|YSF_DEFAULT_NODE_RX_FREQ.*|YSF_DEFAULT_NODE_RX_FREQ        434000000|g"  /opt/xlxd/src/main.h
sed -i "s|define YSF_AUTOLINK_ENABLE.*|define YSF_AUTOLINK_ENABLE             1|g"  /opt/xlxd/src/main.h
#xlxd-start
###########
#cd /opt/xlxd/
#git checkout IMRS
cd /opt/xlxd/src/
make clean
make
make install
###########
chmod -R +x /opt/xlxd/
chmod -R +x /xlxd/
chmod -R 777 /var/log/
chmod -R 777 /opt/xlxd/
chmod -R 777 /xlxd/
chown -R www-data:www-data /opt/xlxd/dashboard/
chown -R www-data:www-data /opt/xlxd/dashboard2/
chown -R www-data:www-data /xlxd/
sudo chmod -R +x /opt/xlxd/*
sudo chmod -R 777 /opt/xlxd/*
wget http://xlxapi.rlx.lu/api/exportdmr.php -O /xlxd/dmrid.dat
#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.*')
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

export LOCAL_IP=$(hostname -I | awk '{print $1}') && exec "$@"
cp /opt/xlxd/scripts/xlxd /etc/init.d/xlxd
sed -i "s|ARGUMENTS=.*|ARGUMENTS=\"$XRFNUM $LOCAL_IP $AMBIP\"|g" /etc/init.d/xlxd
sudo systemctl daemon-reload &&
sudo chmod +x /etc/init.d/xlxd
sudo update-rc.d xlxd defaults &&
sudo chmod -R +x /opt/xlxd/*
sudo chmod -R 777 /var/log/*
sudo chmod -R 777 /opt/xlxd/* 
############
source_folder="/opt/xlx-bk"
backup_folder="/xlxd"

# Verifica si la carpeta /xlxd/ existe
if [ -d "$source_folder" ]; then
    # Verifica si la carpeta /xlxd/ no está vacía
    if [ "$(ls -A $source_folder)" ]; then
        # La carpeta no está vacía, realiza la copia de archivos
        files_to_backup=(
            "xlxd.terminal"
            "xlxd.interlink"
            "xlxd.whitelist"
            "xlxd.blacklist"
        )

        for file in "${files_to_backup[@]}"; do
            if [ -e "$source_folder/$file" ]; then
                cp "$source_folder/$file" "$backup_folder/$file"
                echo "Copia de $file realizada con éxito."
            else
                echo "El archivo $file no existe en la carpeta $source_folder."
            fi
        done
    else
        # La carpeta está vacía
        echo "La carpeta $source_folder está vacía. No se realizará la copia."
    fi
else
    # La carpeta no existe
    echo "La carpeta $source_folder no existe. No se realizará la copia."
fi
    # Lista de servicios a reiniciar
    servicios=("xlxd" "rebooter-xlxd.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
   whiptail --title "Easy-XLX" --msgbox "Easy Config XLX done:\n\n\
XLXD Reflector = $XRFNUM\n\
YSF Port       = $YSFPOR\n\
Enable Module  = $NMODU\n\
HOME Module    = $YSFMODU\n\
AMBE Server    = $AMBIP\n\
AMBE Port      = $AMBPOR\n\
XLXD Public    = $CALLHOME" 0 50
 ;;
2)
find /var/log/ -type f -name "xlx*" -exec rm -f {} +
wdp=/opt/wdp10
xok0=$(grep "XLX-XRFNUM:" $wdp | grep -oP 'XLX-XRFNUM:\s*\K.*')
if [ -z "$xok0" ]; then
  whiptail --title "Easy-XLX" --msgbox "Primero seleciona opcion Easy config xlx / First select option Easy config xlx" 0 50
  exit 0
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
if [ -f "/etc/init.d/xlxd" ]; then
  systemctl stop xlxd
fi
if [ -f "/etc/init.d/xlxd" ]; then
  rm /etc/init.d/xlxd
fi
sudo update-rc.d -f xlxd remove
#
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.*')
 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
XRFNUM=$(grep "XLX-XRFNUM:" $wdp | grep -oP 'XLX-XRFNUM:\s*\K.*')
AMBIP=$(grep "XLX-AMBIP:" $wdp | grep -oP 'XLX-AMBIP:\s*\K.*')
export LOCAL_IP=$(hostname -I | awk '{print $1}') && exec "$@"
cp /opt/xlxd/scripts/xlxd /etc/init.d/xlxd
sed -i "s|ARGUMENTS=.*|ARGUMENTS=\"$XRFNUM $LOCAL_IP $AMBIP\"|g" /etc/init.d/xlxd
sudo systemctl daemon-reload &&
sudo chmod +x /etc/init.d/xlxd
sudo update-rc.d xlxd defaults &&
sudo chmod -R +x /opt/xlxd/*
sudo chmod -R 777 /var/log/*

    # Lista de servicios a reiniciar
    servicios=("xlxd" "rebooter-xlxd.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
 ;;
3)
wdp=/opt/wdp10
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
sudo systemctl disable rebooter-xlxd.service
sudo systemctl stop rebooter-xlxd.service
if [ -f "/etc/init.d/xlxd" ]; then
  systemctl stop xlxd
fi
if [ -f "/etc/init.d/xlxd" ]; then
  rm /etc/init.d/xlxd
fi
sudo update-rc.d -f xlxd remove
 ;;
4)
wdp=/opt/wdp10
xok0=$(grep "XLX-XRFNUM:" $wdp | grep -oP 'XLX-XRFNUM:\s*\K.*')
if [ -z "$xok0" ]; then
  whiptail --title "Easy-XLX" --msgbox "Primero seleciona opcion Easy config xlx / First select option Easy config xlx" 0 50
  exit 0
fi
 network=$(whiptail --title "Easy setup XLXD" --menu "Select Dashboard:" 10 60 2 \
    "1" "XLX Dashboard 1 Clasic" \
    "2" "XLX Dashboard 2 Dark" 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: /opt/xlxd/dashboard|g" $wdp
        ;;
    "2")
        sed -i "s|Web-Dashboar-dir:.*|Web-Dashboar-dir: /opt/xlxd/dashboard2|g" $wdp
        ;;
    *)
        echo "Invalid selection"
        ;;
    esac

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
sudo systemctl disable rebooter-xlxd.service
sudo systemctl stop rebooter-xlxd.service
if [ -f "/etc/init.d/xlxd" ]; then
  systemctl stop xlxd
fi
if [ -f "/etc/init.d/xlxd" ]; then
  rm /etc/init.d/xlxd
fi
sudo update-rc.d -f xlxd remove
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.*')
 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
XRFNUM=$(grep "XLX-XRFNUM:" $wdp | grep -oP 'XLX-XRFNUM:\s*\K.*')
AMBIP=$(grep "XLX-AMBIP:" $wdp | grep -oP 'XLX-AMBIP:\s*\K.*')
export LOCAL_IP=$(hostname -I | awk '{print $1}') && exec "$@"
cp /opt/xlxd/scripts/xlxd /etc/init.d/xlxd
sed -i "s|ARGUMENTS=.*|ARGUMENTS=\"$XRFNUM $LOCAL_IP $AMBIP\"|g" /etc/init.d/xlxd
sudo update-rc.d xlxd defaults &&
sudo systemctl daemon-reload &&
sudo chmod +x /etc/init.d/xlxd
sudo chmod -R +x /opt/xlxd/*
sudo chmod -R 777 /var/log/*

    # Lista de servicios a reiniciar
    servicios=("xlxd" "rebooter-xlxd.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
 ;;
5)
# Capturar la selección del menú
selection=$(whiptail --title "Easy setup XLXD" --menu "ACTIVE XLXD REFLECTOR PUBLIC:" 10 60 2 \
    "1" "No" \
    "2" "Yes" 3>&1 1>&2 2>&3)

# Verificar si el usuario canceló el menú
if [ $? -ne 0 ]; then
    echo "You chose cancel. Returning to the main menu."
    continue  # Regresa al inicio del bucle si estás dentro de uno
fi

# Procesar la selección del usuario
case "$selection" in
    "1")
        echo "Setting 'Active' to false..."
        sed -i "s|'Active.*|'Active']                               = false;|g" /opt/xlxd/dashboard/pgs/config.inc.php
        sed -i "s|'Active.*|'Active']                               = false;|g" /opt/xlxd/dashboard2/pgs/config.inc.php
        ;;
    "2")
        echo "Setting 'Active' to true..."
        sed -i "s|'Active.*|'Active']                               = true;|g" /opt/xlxd/dashboard/pgs/config.inc.php
        sed -i "s|'Active.*|'Active']                               = true;|g" /opt/xlxd/dashboard2/pgs/config.inc.php
        ;;
    *)
        echo "Invalid selection. Returning to the main menu."
        ;;
esac

wdp=/opt/wdp10
xok0=$(grep "XLX-XRFNUM:" $wdp | grep -oP 'XLX-XRFNUM:\s*\K.*')
if [ -z "$xok0" ]; then
  whiptail --title "Easy-XLX" --msgbox "Primero seleciona opcion Easy config xlx / First select option Easy config xlx" 0 50
  exit 0
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
if [ -f "/etc/init.d/xlxd" ]; then
  systemctl stop xlxd
fi
if [ -f "/etc/init.d/xlxd" ]; then
  rm /etc/init.d/xlxd
fi
sudo update-rc.d -f xlxd remove
#
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.*')
 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
XRFNUM=$(grep "XLX-XRFNUM:" $wdp | grep -oP 'XLX-XRFNUM:\s*\K.*')
AMBIP=$(grep "XLX-AMBIP:" $wdp | grep -oP 'XLX-AMBIP:\s*\K.*')
export LOCAL_IP=$(hostname -I | awk '{print $1}') && exec "$@"
cp /opt/xlxd/scripts/xlxd /etc/init.d/xlxd
sed -i "s|ARGUMENTS=.*|ARGUMENTS=\"$XRFNUM $LOCAL_IP $AMBIP\"|g" /etc/init.d/xlxd
sudo systemctl daemon-reload &&
sudo chmod +x /etc/init.d/xlxd
sudo update-rc.d xlxd defaults &&
sudo chmod -R +x /opt/xlxd/*
sudo chmod -R 777 /var/log/*

    # Lista de servicios a reiniciar
    servicios=("xlxd" "rebooter-xlxd.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
 ;;
6)
break
 ;;
esac
done
exit 0
EOF
############
chmod +x /bin/menu*
ln -sf /bin/menu-xlx /bin/MENU-XLX
chmod +x /bin/MENU*
