cat << 'EOF' > instalar.sh
#!/bin/bash
# --- CONFIGURAÇÕES DE ACESSO ---
DB_USER="admin"
DB_PASS="102030#"
DB_GERAL="geral"
DB_USER_DATA="user"
DB_ACESSOS="acessos"

URL_PANEL="https://tvsbr.top/arquivos/painelcs.hospegagem/painel.tar.gz"
URL_SQL_GERAL="http://tvsbr.top/arquivos/painecs.hospedagem/BancoDados/geral.sql"
URL_SQL_USER="http://tvsbr.top/arquivos/painecs.hospedagem/BancoDados/user.sql"
URL_SQL_ACESSOS="http://tvsbr.top/arquivos/painecs.hospedagem/BancoDados/acessos.sql"

IP_SERVER=$(hostname -I | awk '{print $1}')

echo "====================================================="
echo "   INSTALADOR IPTV V2 - HARD RESET & PHPMYADMIN      "
echo "   UBUNTU 24.04 LTS (PHP 8.3)                        "
echo "====================================================="

export DEBIAN_FRONTEND=noninteractive
apt update && apt upgrade -y
apt install -y nginx mariadb-server unzip curl wget git

# Limpeza e Bancos
mysql -e "DROP DATABASE IF EXISTS $DB_GERAL;"
mysql -e "DROP DATABASE IF EXISTS $DB_USER_DATA;"
mysql -e "DROP DATABASE IF EXISTS $DB_ACESSOS;"
mysql -e "CREATE DATABASE $DB_GERAL;"
mysql -e "CREATE DATABASE $DB_USER_DATA;"
mysql -e "CREATE DATABASE $DB_ACESSOS;"
mysql -e "CREATE USER IF NOT EXISTS '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASS';"
mysql -e "GRANT ALL PRIVILEGES ON *.* TO '$DB_USER'@'localhost' WITH GRANT OPTION;"
mysql -e "FLUSH PRIVILEGES;"

# PHP 8.3
apt install -y php8.3-fpm php8.3-mysql php8.3-curl php8.3-xml php8.3-zip php8.3-mbstring php8.3-gd php8.3-intl php8.3-bcmath

# SQLs
wget -q $URL_SQL_GERAL -O geral.sql && mysql $DB_GERAL < geral.sql
wget -q $URL_SQL_USER -O user.sql && mysql $DB_USER_DATA < user.sql
wget -q $URL_SQL_ACESSOS -O acessos.sql && mysql $DB_ACESSOS < acessos.sql
rm *.sql

# Painel
rm -rf /var/www/html/*
wget -q $URL_PANEL -O painel.tar.gz
tar -xzf painel.tar.gz -C /var/www/html/
rm painel.tar.gz

# Permissões e Nginx
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html

cat <<EON > /etc/nginx/sites-available/default
server {
    listen 80;
    server_name _;
    root /var/www/html;
    index index.php index.html;
    location / { try_files \$uri \$uri/ /index.php?\$args; }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
    }
}
EON

systemctl restart nginx php8.3-fpm
echo "CONCLUÍDO! Acesse: http://$IP_SERVER"
EOF
chmod +x instalar.sh && ./instalar.sh