#!/bin/bash

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root (use sudo)" 1>&2
   exit 1
fi

is_Raspberry=$(cat /proc/device-tree/model | awk  '{print $1}')
if [ "x${is_Raspberry}" != "xRaspberry" ] ; then
  echo "Sorry, this only works on raspberry pi"
  exit 1
fi

ver="1.0"


# we create a dir with this version to ensure that 'dkms remove' won't delete
# the sources during kernel updates
marker="0.0.0"

apt update
apt -y install git vim zsh darkice icecast2 nginx

# Keep icecast on port 8000 (can't bind to port 80 without root)
# Nginx will proxy port 80 to icecast port 8000

cp ./darkice.cfg /etc/
cp ./darkice.service /lib/systemd/system/

systemctl daemon-reload
systemctl enable darkice
systemctl start darkice

# Configure nginx to proxy port 80 to icecast port 8000
cat > /etc/nginx/sites-available/blackportaldetroit.com << EOF
server {
    listen 80;
    server_name blackportaldetroit.com;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_buffering off;
    }
}
EOF

# Enable the nginx site and disable default
ln -sf /etc/nginx/sites-available/blackportaldetroit.com /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
systemctl restart nginx

echo ""
echo "🔗 Setting up Cloudflare Tunnel..."
chmod +x ./cloudflare-tunnel-setup.sh
su grace
./cloudflare-tunnel-setup.sh

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

echo "------------------------------------------------------"
echo "The Black Portal broadcasting device has been provisioned."
echo "Reboot this raspberry pi now in order for things to work"
echo "------------------------------------------------------"