#!/bin/bash

apt-get update
apt-get upgrade -y
type -p sudo >/dev/null || sudo apt install sudo

source /opt/scripts_local/shared_functions.sh

# vm tools
sudo dmesg | grep "Hypervisor detected" >/dev/null 2>&1 && sudo apt-get install qemu-guest-agent -y

# dbi user
#apt install sudo
export MYUSER_ID=2222
sudo useradd -u $MYUSER_ID -s /bin/bash -m -d /home/dbi dbi
sudo passwd dbi
sudo groupadd -g 2223 sysadmin
sudo usermod -aG sudo dbi
sudo usermod -aG sysadmin dbi
sudo usermod -aG sysadmin root
sudo chown root:sysadmin /opt/
sudo chmod g+w /opt
sudo chmod a+x /opt





# 
# type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
# curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg && sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null && sudo apt update && sudo apt install gh -y
# download_template "/mnt/unionfs/shared/root/github_cred.txt" "/root"
# sudo gh auth login --git-protocol https --scopes 'repo', 'read:org' --with-token < /root/github_cred.txt
# rm /root/github_cred.txt
# sudo apt install -y git
# sudo rm -rf /opt/.my_git_repos >/dev/null
# sudo mkdir -p /opt/.my_git_repos
# sudo chown root:sysadmin /opt/.my_git_repos
# sudo chmod go+w /opt/.my_git_repos
# cd /opt/.my_git_repos
# git clone https://github.com/Cartache/denis_mbp.git
# echo 'source /opt/.my_git_repos/denis_mbp/.bash_aliases' >> ~/.bashrc
# touch ~/.bash_aliases_local
# source /opt/.my_git_repos/denis_mbp/.bash_aliases
# echo "source /opt/.my_git_repos/denis_mbp/.bash_aliases" >> /home/dbi/.bashrc
# sudo chown dbi:dbi /home/dbi/.bashrc
# sudo bash -c 'echo source /opt/.my_git_repos/denis_mbp/.bash_aliases >> /etc/profile'
# sudo bash -c 'echo source /opt/.my_git_repos/denis_mbp/.bash_aliases >> /root/.bashrc'


# bash_aliases
#GIT_STORE=/home/dbi/.git-credentials
sudo apt install -y git
sudo rm -rf /opt/.my_git_repos
sudo mkdir -p /opt/.my_git_repos
sudo chown root:sysadmin /opt/.my_git_repos
sudo chmod go+w /opt/.my_git_repos
cd /opt/.my_git_repos
git config --global credential.helper store
git clone https://github.com/Cartache/denis_mbp.git
echo 'source /opt/.my_git_repos/denis_mbp/.bash_aliases' >> ~/.bashrc
touch ~/.bash_aliases_local
source /opt/.my_git_repos/denis_mbp/.bash_aliases
echo "source /opt/.my_git_repos/denis_mbp/.bash_aliases" >> /home/dbi/.bashrc
sudo chown dbi:dbi /home/dbi/.bashrc
sudo bash -c 'echo source /opt/.my_git_repos/denis_mbp/.bash_aliases >> /etc/profile'
sudo bash -c 'echo source /opt/.my_git_repos/denis_mbp/.bash_aliases >> /root/.bashrc'

# Files & Dirs
sudo mkdir -p \
/data \
/mnt/rclone_mount/rclone_dbc_media \
/mnt/rclone_mount/rclone_vmx-load \
/mnt/scripts \
/mnt/union \
/opt/appdata/.docker-compose \
/opt/rclone \
/opt/scripts_local \
/opt/secured \
/opt/shared \
/rclone_cache \
/rclone_cache/dbc_media \
/rclone_cache/vmx-load \
/var/log/rclone \
/var/log/docker

sudo touch \
/var/log/rclone/rclone_vmx-load.log \
/opt/appdata/.docker-compose/docker-compose.yml \
/opt/appdata/.docker-compose/.env


sudo chown root:sysadmin \
/opt \
/mnt 
sudo chmod 771 \
/opt \
/mnt 

sudo chown root:sysadmin -R \
/data \
/mnt/rclone_mount \
/mnt/scripts \
/mnt/union \
/opt/rclone \
/opt/scripts_local \
/opt/secured \
/opt/shared \
/rclone_cache \
/var/log/rclone \
/var/log/docker

sudo chmod 771 -R \
/data \
/mnt/rclone_mount \
/mnt/scripts \
/mnt/union \
/opt/rclone \
/opt/scripts_local \
/opt/secured \
/opt/shared \
/rclone_cache \
/var/log/rclone \
/var/log/docker

# Additionnal Software
sudo apt install -y \
acl \
bindfs \
btrfs-progs \
cloud-guest-utils \
chrony \
cronic \
curl \
dnsutils \
ethtool \
fzf \
git \
gzip \
htop \
inotify-tools \
iptables \
jq \
lbzip2 \
lshw \
mailutils \
mc \
moreutils \
mosh \
multitail \
ncdu \
netselect-apt \
net-tools \
p7zip-full \
parted \
psmisc \
pydf \
python3 \
python3-pip \
python3-venv \
rsync \
screen \
sshfs \
traceroute \
unzip \
util-linux \
wget

sudo apt install -y \
fuse3 \
nfs-common \
smbclient \
cifs-utils \
ldap-utils

# iterm2 config
curl -L https://iterm2.com/shell_integration/bash -o /tmp/.iterm2_shell_integration.bash
cp /tmp/.iterm2_shell_integration.bash ~/
sudo cp /tmp/.iterm2_shell_integration.bash /root/
echo "source ~/.iterm2_shell_integration.bash" >> ~/.bashrc
sudo bash -c 'echo "source /root/.iterm2_shell_integration.bash" >> /root/.bashrc'


# Tailscale


sudo apt install ufw &&
sudo ufw allow in on tailscale0 &&
sudo ufw allow 41641/udp &&
sudo ufw enable &&
sudo ufw default deny incoming &&
sudo ufw default allow outgoing

network=192.168.10.0/24
sudo crontab -l | { cat; echo "@reboot sleep 20 && ip rule add to $network priority 2500 lookup main"; } | crontab -

# Fail2ban
sudo apt install -y fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo cp /etc/fail2ban/fail2ban.conf /etc/fail2ban/fail2ban.local
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

# ssh
cd ~
mkdir -p .ssh
cd .ssh
ssh-keygen -b 4096 -t rsa -f id_rsa -C $USER@$HOSTNAME -q -N ""
ssh-keygen -t ed25519 -f id_ed25519 -C $USER@$HOSTNAME -q -N ""
touch ~/.ssh/authorized_keys ~/.ssh/known_hosts
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*
chmod 644 -f ~/.ssh/*.pub ~/.ssh/authorized_keys ~/.ssh/known_hosts

# Misc Config
export PATH="$HOME/Library/Python/3.9/bin:$HOME/.local/bin:$PATH"
sudo grep -i "s/deb cdrom/#deb cdrom/g" /etc/apt/sources.list
sudo timedatectl set-timezone Europe/Brussels
sudo journalctl --vacuum-size=100M
echo 'fs.inotify.max_user_watches=262144' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

# PBS
install_package install_pbs.sh

# Postfix
install_package install_postfix.sh

# Docker
install_package install_docker.sh

# Caddy
install_package install_caddy.sh

# Mergerfs
install_package install_mergerfs.sh

# Rclone
install_package install_rclone.sh

# Cronicle
install_package install_cronicle.sh

# Cloudcmd
install_package install_cloudcmd.sh

# Code-server
install_package install_codeserver.sh

# Cockpit
install_package install_cockpit.sh

# Glances
install_package install_glances.sh