#!/bin/bash
SCRIPT_NAME="allmon3.sh"

# Registra el inicio en /opt/curl.txt
echo "Inicio: $SCRIPT_NAME" >> /opt/curl.txt
VERSION=$(sed 's/\..*//' /etc/debian_version)
if systemctl is-active "apache2" >/dev/null 2>&1; then
    sapache=1
 else
    sapache=0
fi
if [ $VERSION = 10 ]; then
  if [ ! -d "/etc/allmon3" ]; then
    samllm3=1
gpg --keyserver keyserver.ubuntu.com --recv-keys 04EE7237B7D453EC
gpg --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
gpg --export 04EE7237B7D453EC | sudo apt-key add -
gpg --export 648ACFD622F3D138 | sudo apt-key add -
echo "deb https://deb.debian.org/debian buster-backports main" > /etc/apt/sources.list.d/buster-backports.list
apt-get update -y
   else
   samllm3=0
  fi
fi
if [ $VERSION = 11 ]; then
  if [ ! -d "/etc/allmon3" ]; then
    samllm3=1
gpg --keyserver keyserver.ubuntu.com --recv-keys 0E98404D386FA1D9
gpg --keyserver keyserver.ubuntu.com --recv-keys 6ED0E7B82643E131
gpg --export 0E98404D386FA1D9 | sudo apt-key add -
gpg --export 6ED0E7B82643E131 | sudo apt-key add -
echo "deb https://deb.debian.org/debian bullseye-backports main" > /etc/apt/sources.list.d/bullseye-backports.list
apt-get update
   else
   samllm3=0
  fi
fi
if [ $VERSION = 12 ]; then
  if [ ! -d "/etc/allmon3" ]; then
    samllm3=1
cd /tmp
wget https://repo.allstarlink.org/public/asl-apt-repos.deb12_all.deb
sudo dpkg -i asl-apt-repos.deb12_all.deb
sudo apt-get update
   else
   samllm3=0
  fi
fi
 #   Install the dependencies
if [ $VERSION = 10 ]; then
apt-get remove python3-aiohttp python3-websockets -y
apt-get install -y python3-argon2 
apt-get install -y -t buster-backports python3-async-timeout python3-attr python3-multidict python3-yarl 
apt-get --fix-broken install -y
pip3 install aiohttp
pip3 install aiohttp_session
pip3 install websockets
apt-get install ca-certificates -y
update-ca-certificates --fresh
fi
if [ $VERSION = 11 ]; then
apt-get install -y python3-argon2 
apt-get install -y -t bullseye-backports python3-websockets python3-aiohttp python3-aiohttp-session
fi
####   Install Allmon3
if [ $VERSION = 10 ]; then
    # Descarga el paquete de Allmon3 para Debian 10 (Buster)
    wget https://github.com/AllStarLink/Allmon3/releases/download/t_rel_1_2_0/allmon3_1.2.0-1.buster_all.deb
    # Instala el paquete
    dpkg -i allmon3_1.2.0-1.buster_all.deb
fi
if [ $VERSION = 11 ]; then
    # Descarga el paquete de Allmon3 para Debian 11 (Bullseye)
    wget https://github.com/AllStarLink/Allmon3/releases/download/t_rel_1_2_0/allmon3_1.2.0-1.bullseye_all.deb
    # Instala el paquete
    dpkg -i allmon3_1.2.0-1.bullseye_all.deb
fi
if [ $VERSION = 12 ]; then
    apt-get install -y allmon3
fi
if [ -f "/usr/share/allmon3/index.html" ]; then
variable2=$(date +'%Y' | tail -c 5)
allmv=$(grep -oP '<span style="color:red">\K[^<]+' /usr/share/allmon3/index.html)
#echo $allmv
sed -i "s/AllStarLink<br>/AllStarLink<br>Dashboard <a title=\"Release: $allmv\" href=\"https:\/\/github.com\/AllStarLink\/Allmon3\"target=\"_blank\">AllMon3<\/a> Script project: <a title=\"Script Proyect by HP3ICC © 2018-$variable2\" href=\"https:\/\/gitlab.com\/hp3icc\/easy-asl\/\"target=\"_blank\">Easy-ASL<\/a><br>/g" /usr/share/allmon3/index.html
fi
if [ -d "/etc/allmon3" ]; then
    cat > "/etc/allmon3/allmon3.ini" <<- "EOFX"
;
; This is the master configuration file for all ASL/Asterisks node-related
; information. It is used for all server daemon processes
; (e.g. asl-statmon, asl-cmdlink) and the web interface/API.
;
; Each configuration item below is marked (R) or (O) for required or optional
; repsectively along with usage notes. The value in the table below is the
; default value for optiional items or examples for required items.
;
; [1999]
; host = 192.0.2.0     # (R) DNS name or IP address of the Asterisk/ASL node
;
; port = 5038          # (O) port of the Asterisk manager
;
; user = admin         # (R) username of the Asterisk monitor, most 
;                      #     commonly 'admin'
;
; pass = password      # (R) password of the monitor user
; 
; multinodes =         # (O) this node is a server hosting multiple nodes
;                      #     and this is the "primary" record for the host.
; 
; voters = n[,n,...]   # (O) List voters on this server, comma
;                      #     separate. No value disabled voters
;
; pollinterval = 1     # (O) polling interval to asterisk in seconds 
;                      #     default is 1. this value can be expressed as
;                      #     a decimal fraction of a second - e.g., .5
;                      #     is 500ms, .375 is 375ms, etc.
;
; vpollinterval = 1    # (O) broadcast interval of voter data in seconds
;                      #      default is 1. this value can be expressed as
;                      #      a decimal fraction of a second - e.g., .5
;                      #      is 500ms, .375 is 375ms, etc.
;
; retryinterval = 15   # (O) seconds between retries if initial connection to
;                      #     asterisk is lost (optional, default 15)
;
; retrycount =         # (O) number of times to retry a lost asterisk
;                      #     connection before ending (default infinite)

; For a basic installation, you chould be able to uncomment the below,
; change 1999 to your node number and set the pass= as
; set in asterisk/manager.conf
;[1999]
;host=127.0.0.1
;user=admin
;pass=password

[a5a4a3a2a1]
host=127.0.0.1
user=admin
pass=a2b2c2d2e2


EOFX
fi
if [ $samllm3 = 1 ]; then
    systemctl disable allmon3
    systemctl stop allmon3
fi
if [ $sapache = 1 ]; then
    systemctl stop apache2
    systemctl start apache2
    systemctl enable apache2
 else
    systemctl stop apache2
    systemctl disable apache2   
fi
#######################
if [ -d "/etc/nginx/sites-available" ]; then
cat > /etc/nginx/sites-available/allmon3 <<- "EOFX1" 
server {
    listen wd0p;



    location / {
        root /usr/share/allmon3;  # Ajusta la ruta según tu configuración
        index index.html;
        autoindex off;
    }

    location /master/ {
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header X-Request-Start $msec;
        proxy_pass http://127.0.0.1:16080/;
    }

    location ~ ^/ws/([0-9]+)$ {
        set $port $1;
        proxy_pass  http://127.0.0.1:$port/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header X-Request-Start $msec;
    }

    # Otras configuraciones según sea necesario...
}


EOFX1
fi
echo "Finalizado: $SCRIPT_NAME" >> /opt/curl.txt
#########################################################

