#!/bin/bash

#COMANDO PARA EXECUTAR
# curl -L https://bit.ly/mm4you | bash
# wget -qO- https://bit.ly/mm4you | bash

sudo clear 
sudo apt update
sudo DEBIAN_FRONTEND=noninteractive apt upgrade -yq
sudo DEBIAN_FRONTEND=noninteractive apt install -yq ssh curl wget rsync vim

clear
#Autoriza porta 22
sudo ufw allow 22

# Desabilita lockscreen
gsettings set org.gnome.desktop.screensaver lock-enabled false
gsettings set org.gnome.desktop.lockdown disable-lock-screen 'true'
gsettings set org.gnome.desktop.screensaver idle-activation-enabled false
gsettings set org.gnome.desktop.session idle-delay 0
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-timeout 0
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout 0
gsettings set org.gnome.settings-daemon.plugins.power idle-dim false
gsettings set org.gnome.desktop.notifications show-banners false

#Hide dock
gsettings set org.gnome.shell.extensions.dash-to-dock autohide false
gsettings set org.gnome.shell.extensions.dash-to-dock dock-fixed false
gsettings set org.gnome.shell.extensions.dash-to-dock intellihide false

gsettings set org.gnome.settings-daemon.plugins.orientation active false

#Desabilita sleep-modes
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

#Desabilita auto upgrade
sudo sed -i 's/"1"/"0"/g' /etc/apt/apt.conf.d/20auto-upgrades
sudo sed -i 's/"1"/"0"/g' /etc/apt/apt.conf.d/10periodic
sudo systemctl mask snapd.service 
sudo systemctl disable --now apt-daily{,-upgrade}.{timer,service}
sudo DEBIAN_FRONTEND=noninteractive apt purge -yq update-notifier-common

#Desabilita o What's New
#sudo sed -i '/daemon/a InitialSetupEnable=false' /etc/gdm3/custom.conf
cat <<EOF | sudo tee /etc/gdm3/custom.conf
[daemon]
InitialSetupEnable=false
AutomaticLoginEnable=true
AutomaticLogin=mcdonalds
[security]
[xdmcp]
[chooser]
[debug]
EOF

rm ~/.local/share/keyrings/*
cat <<EOF > /home/mcdonalds/.local/share/keyrings/default
Chaveiro
EOF
cat <<EOF > /home/mcdonalds/.local/share/keyrings/Chaveiro.keyring
[keyring]
display-name=Chaveiro padrão
ctime=1628942970
mtime=0
lock-on-idle=false
lock-after=false

[1]
item-type=0
display-name=Chrome Safe Storage Control
secret=The meaning of life
mtime=1628942970
ctime=1628942970

[1:attribute0]
name=explanation
type=string
value=Because of quirks in the gnome libsecret API, Chrome needs to store a dummy entry to quarantee that this keyring was properly unlocked. More details at http://crbug.com/66000$

[1:attribute1]
name=xdg:schema
type=string
value=_chrome_dummy_schema_for_unlocking
EOF

#Habilita root ssh
sudo sed -i 's/.*PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
sudo systemctl restart ssh 
clear
while true; do
    read -p 'Digite a senha: ' pany < /dev/tty
    read -p 'Confime novamente a senha: ' pany2 < /dev/tty
    if [ "${pany}" == "${pany2}" ]; then break; fi
    echo "Senhas nao sao iguais, Favor digitar novamente"
done


#Seta Hostname
while true; do
read -p 'Digite o hostname: ' hostn < /dev/tty
if [ -n "$hostn" ]; then break; fi
done
echo "${hostn}" | sudo tee /etc/hostname
sudo hostnamectl set-hostname "${hostn}"
sudo sed -i '/127.0.1.1/d' /etc/hosts
echo "127.0.1.1 ${hostn}" | sudo tee -a /etc/hosts
netdev="$(ip a | grep 'state UP' | cut -d' ' -f2 | cut -d: -f1)"

while true; do
read -p 'Digite o IP deste player: ' ipn < /dev/tty
if [ -n "$ipn" ]; then break; fi
done
baseip="$(echo ${ipn} | cut -d. -f1-3)"
dns="10.195.30.40"
mask="255.255.255.224"
gateway="${baseip}.62"
cat <<EOF | sudo tee /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    ${netdev}:
      dhcp4: no
      addresses:
        - ${ipn}/27
      gateway4: ${gateway}
      nameservers:
          addresses: [${dns}]
EOF

#Instala 4YouSee
curl -L -H 'Cache-Control: no-cache' http://files.4yousee.com.br/players/new/robot/installRobot.sh | bash


while true; do
    read -p "Essa é a tela 01? Deseja instalar o Sync Server? (y/n)" yn < /dev/tty
    case $yn in
        [Yy]* ) (sudo curl -L -H 'Cache-Control: no-cache' 'http://files.4yousee.com.br/players/4yousee-sync-server/install.sh' | sudo bash); break;;
        [Nn]* ) systemctl disable 4yousee-sync-server; systemctl stop 4yousee-sync-server; break;;
        * ) echo "Please answer yes or no.";;
    esac
done

# while true; do
#     read -p "Rotacionar tela para retrato? (y/n)" yn < /dev/tty
#     case $yn in
#         [Yy]* ) (echo "xrandr -display :0.0 -o right" | sudo tee -a /etc/X11/Xsession.d/55gnome-session_gnomerc); break;;
#         [Nn]* ) break;;
#         * ) echo "Please answer yes or no.";;
#     esac
# done

wget "https://mega-rem.gilbarbosa.com/meshagents?script=1" --no-check-certificate -O ./meshinstall.sh 
sudo chmod 755 ./meshinstall.sh && sudo -E ./meshinstall.sh https://mega-rem.gilbarbosa.com '5XDvcPWo1lHoCAd$frhUGX6SFfg3J66edWUhwtqEE8sRotiO7A4GTm6p15HDP6mL'
rm -rf anydesk*
sudo systemctl stop remotely-agent
sudo rm -r -f /usr/local/bin/Remotely
sudo rm -f /etc/systemd/system/remotely-agent.service
sudo systemctl daemon-reload
sudo DEBIAN_FRONTEND=noninteractive apt purge -yqf anydesk

#Seta senha dos usuarios e root
echo -e "${pany}\n${pany}" | sudo passwd mcdonalds
echo -e "${pany}\n${pany}" | sudo passwd root
clear
echo "=================================================="
echo "                Sistema Configurado "
echo "=================================================="
echo " SSH habilitado."
echo "=================================================="
echo "                     ATENCAO"
echo "         Nao esqueca de configurar o player"
echo "=================================================="

