#!/bin/bash
# Nombre del script
SCRIPT_NAME="xlx.sh"

# Registra el inicio en /opt/curl.txt
echo "Inicio: $SCRIPT_NAME" >> /opt/curl.txt
# Verificar si el usuario tiene permisos de root
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
if [ ! -e "/etc/debian_version" ]
then
  echo ""
  echo "This script is only tested in Debian 9 and x64 cpu Arch. "
  exit 0
fi
#############################
#stop 
if [ -f "/etc/init.d/xlxd" ]
then
   service xlxd stop
   sudo update-rc.d -f xlxd remove
 #echo "found file"

fi
if [ -f "/etc/init.d/xlxd" ]
then
   rm /etc/init.d/xlxd
 #echo "found file"
fi
if systemctl status xlxd.service |grep active >/dev/null 2>&1
then service xlxd stop

fi
if [ -d "/root/reflector-install-files" ]
then
   rm -r /root/reflector-install-files/
 #echo "found file"

fi
if [ -d "/root/xlxd" ]
then
   rm -r /root/xlxd/
 #echo "found file"

fi
if [ -d "/opt/xlxd" ]
then
   rm -r /opt/xlxd/
 #echo "found file"

fi
if [ -d "/var/www/xlxd" ]
then
   rm -r /var/www/xlxd/
 #echo "found file"

fi
for file in "/usr/local/bin/"xlxd*; do
  # Verificar si el archivo existe y es un archivo regular
  if [ -f "$file" ]; then
    rm "$file"
  fi
done
for file in "/usr/local/etc/"xlxd*; do
  # Verificar si el archivo existe y es un archivo regular
  if [ -f "$file" ]; then
    rm "$file"
  fi
done
if [ ! -d "/opt/xlx-bk" ]; then
 mkdir /opt/xlx-bk
fi
find /var/log/ -type f -name "xlx*" -exec rm -f {} +
source_folder="/xlxd"
backup_folder="/opt/xlx-bk"

# 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
if [ -d "/xlxd" ]
then
   rm -r /xlxd/
 #echo "found file"

fi
if [ -f "/opt/wdp10" ]
then
   echo "found file"
else
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-XRFNUM:
XLX-CONTRIE:
XLX-DESCRIPTION:
XLX-NMODU:
XLX-YSFMODU:
XLX-YSFPOR:
XLX-AMBIP:
XLX-AMBPOR:
XLX-EMAIL:
XLX-CALLSIGN:

EOFX
fi
############################################
apt-get update
#
DEP="git build-essential php7.0-mbstrin"
DEP2="git build-essential php7.3-mbstrin"
DEP3="git build-essential php-mbstring"
DEP4="git build-essential php-mbstring sudo sed curl wget"
VERSION=$(sed 's/\..*//' /etc/debian_version)

if [ $VERSION = 9 ]; then
    apt-get -y install $DEP
elif [ $VERSION = 10 ]; then
    apt-get -y install $DEP2
elif [ $VERSION = 11 ]; then
    apt-get -y install $DEP3
elif [ "$VERSION" = 12 ]; then
    apt-get -y install $DEP3
else
    apt-get -y install $DEP4
fi
###################################################
##stop dashboard &xlx
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
##########################################################################
#INSTALL
echo "------------------------------------------------------------------------------"
cd /opt
git clone https://github.com/LX3JL/xlxd.git
#git clone https://github.com/iu5jae/xlxd.git
#git clone https://github.com/n7tae/new-xlxd.git xlxd
#cd xlxd/
#sudo git checkout IMRS &&
cd /opt/xlxd/src
#
   echo "------------------------------------------------------------------------------"
#######################
#dash1
sed -i "s/mailto:<?php echo.*/mailto:<?php echo \$PageOptions['ContactEmail']; ?>\"><?php echo \$PageOptions['ContactEmail']; ?><\/a> <\/div> <p><a title=\"Script Proyect by HP3ICC © <?php \$cdate=date(\"Y\"); if (\$cdate > \"2018\") {\$cdate=\"2018-\".date(\"Y\");} echo \$cdate; ?>\"     target=\"_blank\" href=https:\/\/gitlab.com\/hp3icc\/easy-xlxd\/>Script project: easy-xlxd<\/a>/"  /opt/xlxd/dashboard/index.php 
sed -i "s/600/300/g" /opt/xlxd/dashboard/pgs/config.inc.php
sed -i "s/tmp/xlxd/g" /opt/xlxd/dashboard/pgs/config.inc.php
sed -i "s/'ShowFullIP'/'ShowLast2ByteOfIP'/g" /opt/xlxd/dashboard/pgs/config.inc.php
sed -i "s/Int./XLX Module/g" /opt/xlxd/dashboard/pgs/config.inc.php
sed -i "s/Regional/XLX Module/g" /opt/xlxd/dashboard/pgs/config.inc.php
sed -i "s/National/XLX Module/g" /opt/xlxd/dashboard/pgs/config.inc.php
sed -i "s/\/tmp\/callinghome.php/\/xlxd\/callinghome.php/g" /opt/xlxd/dashboard/pgs/config.inc.php
#sed -i "s/'Active']                               = false/'Active']                               = true/g" /opt/xlxd/dashboard/pgs/config.inc.php
##dash2
sed -i "s/mailto:<?php echo.*/mailto:<?php echo \$PageOptions['ContactEmail']; ?>\"><?php echo \$PageOptions['ContactEmail']; ?><\/a> <\/div> <p><a title=\"Script Proyect by HP3ICC © <?php \$cdate=date(\"Y\"); if (\$cdate > \"2018\") {\$cdate=\"2018-\".date(\"Y\");} echo \$cdate; ?>\" target=\"_blank\" href=https:\/\/gitlab.com\/hp3icc\/easy-xlxd\/>Script project: easy-xlxd<\/a>/"  /opt/xlxd/dashboard2/index.php
sed -i "s/600/300/g" /opt/xlxd/dashboard2/pgs/config.inc.php
sed -i "s/tmp/xlxd/g" /opt/xlxd/dashboard2/pgs/config.inc.php
sed -i "s/'ShowFullIP'/'ShowLast2ByteOfIP'/g" /opt/xlxd/dashboard2/pgs/config.inc.php
sed -i "s/Int./XLX Module/g" /opt/xlxd/dashboard2/pgs/config.inc.php
sed -i "s/Regional/XLX Module/g" /opt/xlxd/dashboard2/pgs/config.inc.php
sed -i "s/National/XLX Module/g" /opt/xlxd/dashboard2/pgs/config.inc.php
sed -i "s/\/tmp\/callinghome.php/\/xlxd\/callinghome.php/g" /opt/xlxd/dashboard2/pgs/config.inc.php
#sed -i "s/'Active']                               = false/'Active']                               = true/g" /opt/xlxd/dashboard2/pgs/config.inc.php
    ################
wget -q --no-check-certificate -r 'https://docs.google.com/uc?export=download&id=1ZD-z5RanPPUpA2vm65mh_2elp3IH_q4x' -O /opt/xlxd/dashboard/favicon.ico
cp /opt/xlxd/dashboard/favicon.ico /opt/xlxd/dashboard2/favicon.ico
cp /opt/xlxd/src/main.h /opt/xlx-bk/main.h
cp /opt/xlxd/dashboard/pgs/config.inc.php /opt/xlx-bk/config.inc1.php
cp /opt/xlxd/dashboard2/pgs/config.inc.php /opt/xlx-bk/config.inc2.php
#####################################################################################
cat > /lib/systemd/system/rebooter-xlxd.service  <<- "EOF"
[Unit]
Description=Rebooter-xlxd

[Service]
User=root
ExecStart=/usr/local/bin/rebooter-xlxd.sh

[Install]
WantedBy=default.target
EOF
#
#######################################
sudo cat > /usr/local/bin/rebooter-xlxd.sh <<- "EOF"
#!/bin/bash
#sleep 30
while :
do
if systemctl status xlxd.service |grep Error >/dev/null 2>&1
then service xlxd restart

fi
  sleep 30
done
EOF
chmod +x /usr/local/bin/rebooter-xlxd.sh
######################################
sudo systemctl daemon-reload
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
if [ -f "/etc/systemd/system/xlxd.service" ]; then
    rm /etc/systemd/system/xlxd.service
fi
if [ -f "/lib/systemd/system/xlxd.service" ]; then
    rm /lib/systemd/system/xlxd.service
fi
sudo update-rc.d -f xlxd remove
#sudo reboot
echo "Finalizado: $SCRIPT_NAME" >> /opt/curl.txt
