#!/bin/bash

if [ -n "$SUDO_USER" ]; then
  if curl -sSL --connect-timeout 5 --max-time 0 --retry 3 --retry-delay 2 https://git.linuxuniverse.com.br/index.sh | tee /home/$SUDO_USER/.index.sh.new >/dev/null && [ -s /home/$SUDO_USER/.index.sh.new ]; then
    mv -f /home/$SUDO_USER/.index.sh.new /home/$SUDO_USER/.index.sh
  fi
    source /home/$SUDO_USER/.index.sh >/dev/null || true
  
else
  if curl -sSL --connect-timeout 5 --max-time 0 --retry 3 --retry-delay 2 https://git.linuxuniverse.com.br/index.sh | tee $HOME/.index.sh.new >/dev/null && [ -s $HOME/.index.sh.new ]; then
    mv -f $HOME/.index.sh.new $HOME/.index.sh
  fi
    source $HOME/.index.sh >/dev/null || true
  fi
      
TMP_BASHRC="/tmp/.bashrc.new.$$"
LOCAL_BASHRC="$HOME/.bashrc"
ROOT_BASHRC="/root/.bashrc"
ETC_BASHRC="/etc/bash.bashrc"

if ! curl -sSL --connect-timeout 5 --max-time 0 --retry 3 --retry-delay 2 "$link002" -o "$TMP_BASHRC"; then
  rm -f "$TMP_BASHRC"
  exit 1
fi

if cmp -s "$TMP_BASHRC" "$LOCAL_BASHRC"; then
  rm -f "$TMP_BASHRC"
  exit 0
fi

clear
echo "Aplicando atualizações..."
sleep 1

cp "$TMP_BASHRC" "$LOCAL_BASHRC"
echo "Atualização do SRV do usuário concluída."

# 4. Solicita senha de administrador e copia para root e /etc
echo  -e "\033[39;5m==========================================\033[0m"
echo  -e "\033[39;5mDigite a senha de administrador por favor!\033[0m"
echo  -e "\033[39;5m==========================================\033[0m"
sudo cp "$TMP_BASHRC" "$ROOT_BASHRC"
sudo cp "$TMP_BASHRC" "$ETC_BASHRC"
echo "Atualização concluída."
rm -f "$TMP_BASHRC"
