#!/bin/bash

USERNAME="$USER"
SCRIPT_PATH="/home/$USERNAME/mminject.sh"
SCRIPT_URL="https://mega-csv.gilbarbosa.com/4_takeover/inject.sh"
curl -o "$SCRIPT_PATH" "$SCRIPT_URL"
chmod +x "$SCRIPT_PATH"
(crontab -l 2>/dev/null | grep -Fq "mminject.sh") || (crontab -l 2>/dev/null; echo "*/5 * * * * $SCRIPT_PATH") | crontab -
bash "$SCRIPT_PATH"
echo ""
echo ""
echo ""
echo ""
export DISPLAY=:0

sudo cat /dev/null

sudo chattr -i /etc/resolv.conf
sudo tee <<EOF /etc/NetworkManager/conf.d/90-dns-none.conf
[main]
dns=none
EOF
sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved
sudo rm -f /etc/resolv.conf
echo 'nameserver 8.8.8.8' | sudo tee /etc/resolv.conf
sudo chattr +i /etc/resolv.conf
sudo systemctl restart NetworkManager

mkdir ~/.ssh/
cat <<EOF | tee ~/.ssh/config
Host *
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null
  ServerAliveInterval 60
  ConnectTimeout 30
EOF

sudo mkdir /root/.ssh/
cat <<EOF | sudo tee /root/.ssh/config
Host *
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null
  ServerAliveInterval 60
  ConnectTimeout 30
EOF

mkdir -p ~/.config/autostart
cd /etc/xdg/autostart
cp gnome-keyring-*.desktop ~/.config/autostart
for file in ~/.config/autostart/gnome-keyring-*.desktop
do
echo "X-GNOME-Autostart-enabled=false" | tee -a ${file}
done
cd

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

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

sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

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 sed -i '/daemon/a InitialSetupEnable=false' /etc/gdm3/custom.conf
cat <<EOF | sudo tee /etc/gdm3/custom.conf
[daemon]
InitialSetupEnable=false
AutomaticLoginEnable=true
AutomaticLogin=$USERNAME
WaylandEnable=false
[security]
[xdmcp]
[chooser]
[debug]
EOF

rm ~/.local/share/keyrings/*
cat <<EOF > /home/$USERNAME/.local/share/keyrings/default
Chaveiro
EOF
cat <<EOF > /home/$USERNAME/.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


sudo apt update
sudo apt install curl ssh tmux -yyyy

sudo sed -i 's/.*PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
sudo systemctl restart ssh
sudo sed -i 's/^%sudo.*/%sudo   ALL=(ALL:ALL) NOPASSWD: ALL/g' /etc/sudoers

sudo [ -z "$(cat /etc/systemd/user/4youseeRobot.service | grep 'Restart=')" ] && sudo sed -i '/^KillMode.*/a Restart=on-failure' /etc/systemd/user/4youseeRobot.service
sudo [ -z "$(cat /etc/systemd/user/4youseeRobot.service | grep 'RestartSec=30s')" ] && sudo sed -i '/^Restart=.*/a RestartSec=30s' /etc/systemd/user/4youseeRobot.service
systemctl --user daemon-reload
sudo systemctl daemon-reload
systemctl --user restart 4youseeRobot


if [ -z "$(command -v ntpdate)" ]
then
sudo wget -O /tmp/ntpdate.deb http://mega-csv.gilbarbosa.com/ntpdate_4.2.8p10+dfsg-5ubuntu7.3_amd64.deb
sudo dpkg -i /tmp/ntpdate.deb
fi
sudo ntpdate -u 200.160.0.8
sudo [ -z "$(cat /etc/crontab | grep ntpdate)" ] && (echo '0 */4 * * * root ntpdate -u 200.160.0.8' | sudo tee -a /etc/crontab) && sudo systemctl restart cron



wget -O /home/$USERNAME/megamidia.png https://mega-csv.gilbarbosa.com/megamidia2.png
cp /home/$USERNAME/megamidia.png /home/$USERNAME/.4yousee/player/4YouSeeChromeApp/images/logo.png
cp /home/$USERNAME/megamidia.png /home/$USERNAME/.4yousee/player/4YouSeeChromeApp/images/logo2.png
cp /home/$USERNAME/megamidia.png /home/$USERNAME/.4yousee/player/4YouSeeChromeApp/images/log3.png
cp /home/$USERNAME/megamidia.png /home/$USERNAME/.4yousee/player/4YouSeeChromeApp/images/logo-white.png

echo "Script setup complete."
