#!/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 ddclient

# Configure icecast to run on port 80
sed -i 's/<port>8000<\/port>/<port>80<\/port>/g' /etc/icecast2/icecast.xml

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

systemctl daemon-reload
systemctl enable darkice
systemctl start darkice

# Configure ddclient for Namecheap Dynamic DNS
cat > /etc/ddclient.conf << EOF
# Namecheap Dynamic DNS configuration
daemon=300                              # Check every 5 minutes
syslog=yes                             # Log to syslog
pid=/var/run/ddclient.pid              # PID file location
ssl=yes                                # Use SSL

# How to get IP address
use=web, web=dynamicdns.park-your-domain.com/getip

# Namecheap settings
protocol=namecheap
server=dynamicdns.park-your-domain.com
login=blackportaldetroit.com
password=REPLACE_WITH_NAMECHEAP_DDNS_PASSWORD
@                                      # Update root domain
EOF

# Set proper permissions for ddclient config
chmod 600 /etc/ddclient.conf

# Enable and start ddclient (but don't start until password is configured)
systemctl enable ddclient

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 "------------------------------------------------------"