#!/bin/bash

# authorized keys
mkdir -p /home/$USER/.ssh
echo ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC21HBjt4LN2p7+iYXSUpJjWtxzvkbI5l+pNhbvZbMoJxkQRq76OyLX1+nv2kiQEz2WDtYgIWJGZiHml6BwVGUYPoXdasY4myCvTXDHh29OD9uWDINHr5Ojx09m+Bcwd2QDDYfihM4Fw7dVFZ1vESW84FZ/PYc5GPcJpQSE8fHutzYPA8RZYi8YID0PN/ihEQKgqstpLhmE9UZnowYouwg/Di03TkNP8I/duFzTrYk2wfpUoC0+/gwGL037OwfFhNozxhHoXzFtKuzPquiOdHbzZe6Cm1aLUBKs9//TCbHQJ80txV67i2xSp3uTFiD+k5jVekoHkH3PZjuRbP38MKI3 Peter@PSNB >> /home/$USER/.ssh/authorized_keys
chmod 0600 /home/$USER/.ssh/authorized_keys

# rpi-update
sudo rpi-update

# enable i2c
sudo raspi-config nonint do_i2c 0

# overlay
sudo cp overlays/mcp23017_ps20.dtbo /boot/firmware/overlays/
sudo cp overlays/mcp23017_ps21.dtbo /boot/firmware/overlays/

# boot config
sudo sed -i '/dtparam=i2c_arm=on/s/^#//g' /boot/firmware/config.txt
sudo bash -c 'cat << EOF >> /boot/firmware/config.txt
[all]
dtoverlay=i2c1-pi5,pins_2_3
dtoverlay=i2c2-pi5,pins_12_13
dtoverlay=i2c3-pi5,pins_22_23

dtoverlay=mcp23017_ps20
dtoverlay=mcp23017_ps21
dtoverlay=mcp23017,addr=0x22,gpiopin=19,i2c3
dtoverlay=mcp23017,addr=0x23,gpiopin=26,i2c3

dtoverlay=disable-wifi
dtoverlay=disable-bt

dtparam=rtc_bbat_vchg=3000000
EOF'

# apt key
sudo bash -c 'cat << EOF >> /etc/apt/keyrings/psnt.asc
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQGNBGcnt9EBDACyltSn7tLkRL25IEFrXWPtQFaC3ZtUBc7aeIsWryfrlPhehE+Z
KDa+gmI1Kd4oH4BuF2YQ+/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QR+p1ktU315cscjHkn5lE0wlzW0yg6sY2wf/681c
aVsFVfHmT8DBWuOWFq0syfiR50lecVU8gZVw13x0QenKm/Of5LK+HDve5q+Pa7Oo
jg2TJLHKFQ0LzjSACInELGI=
=7DJ4
-----END PGP PUBLIC KEY BLOCK-----
EOF'

# apt source
sudo bash -c 'cat << EOF >> /etc/apt/sources.list.d/psnt.list
deb [arch=arm64 signed-by=/etc/apt/keyrings/psnt.asc] https://apt.psnt.de/debian   unstable main
EOF'

# apt
sudo apt update
sudo apt upgrade -y
sudo apt install -y \
    wireguard \
    vim \
    ca-certificates curl \
    htop \
    jq

# docker
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo   "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" |   sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
sudo chmod g+rwx "$HOME/.docker" -R

export CR_PAT=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
echo $CR_PAT | docker login ghcr.io -u peschuster --password-stdin

# companion prep
sudo cp 50-companion.rules /etc/udev/rules.d/50-companion.rules
sudo udevadm control --reload-rules

docker volume create companion-config

# tallyarbiter prep
docker volume create tallyarbiter-config

# node
curl -sL https://deb.nodesource.com/setup_18.x | sudo bash -
sudo apt update
sudo apt install -y nodejs

# nmcli
sudo nmcli c modify "Wired connection 1" connection.id eth0

# ipv6
sudo cp 10-disable-ipv6.conf /etc/sysctl.d/
sudo nmcli c modify eth0 ipv6.method ignore

# git firmware
mkdir -p /home/$USER/git
DIR=$(pwd)
cd /home/$USER/git
git init --bare psBroadcatIo-firmware.git
cat << EOF > psBroadcastIo-firmware.git/hooks/post-receive
#!/bin/sh

WORKTREE=/home/$USER/psBroadcastIo

# Check out the files
git --work-tree=\$WORKTREE --git-dir=/home/$USER/git/psBroadcastIo-firmware.git checkout -f

yarn --cwd=\$WORKTREE/rpi-server
yarn --cwd=\$WORKTREE/rpi-server run build

yarn --cwd=\$WORKTREE/webapp
yarn --cwd=\$WORKTREE/webapp run build

sudo systemctl restart psBroadcastController.service
EOF
chmod a+x psBroadcastIo-firmware.git/hooks/post-receive
cd $DIR

cp_set_exec () {
    cp "$1" "$2"
    chmod a+x "$2/$1"
}

# pull companion docker and start
cp .companion-version /home/$USER/
cp_set_exec pull_companion_ghcr.sh /home/$USER
cp_set_exec start_companion.sh /home/$USER
cp_set_exec upgrade_companion.sh /home/$USER

COMPANION_VERSION=$(cat /home/$USER/.companion-version)
bash /home/$USER/pull_companion_ghcr.sh $COMPANION_VERSION
bash /home/$USER/start_companion.sh

# pull tallyarbiter docker and start
cp_set_exec start_tallyarbiter.sh /home/$USER
docker pull josephdadams/tallyarbiter:latest
bash /home/$USER/start_tallyarbiter.sh

# install firmware
sudo apt update
sudp apt install -y psbroadcastio