#!/bin/bash -x

# This script should be ran via /boot/firmware/cmdline.txt (or /boot/cmdline.txt if no firmware directory) by adding " quiet init=/usr/sbin/reconfig-clusterctrl X"
# where X is replaced by either cbridge, cnat, p1, p2, p3, p4 depending on which image you want to setup.
# 
# Once added reboot the Pi and on first boot the script will reconfigure the Pi and then reboot again
#

reboot_pi () {
  umount /boot
  umount /boot/firmware
  sync
  echo b > /proc/sysrq-trigger
  sleep 5
  exit 0
}

mount -t proc proc /proc
mount -t sysfs sys /sys

# Configure for ClusterCTRL
# Parameter can be either p1 to p252 for nodes and cbridge or cnat for controller
#

CONFIGDIR="/usr/share/clusterctrl"

if [ -d /boot/firmware ];then
 BOOT="/boot/firmware"
else
 BOOT="/boot"
fi
mount $BOOT

if [ -f /etc/os-release ]; then
 source /etc/os-release
fi

if [ ! -z $1 ];then

 mount -o remount,rw /

 NUMBER='^[0-9]+$'
 if [ $1 = "cnat" ] || [ $1 = "cbridge" ] || { [ ${#1} -gt 1 ] && [ ${1:0:1} = "p" ] && [[ ${1:1} =~ $NUMBER  ]] && [ ${1:1} -gt 0 ] && [ ${1:1} -lt 253 ]; }; then

  # Backup kernel cmdline.txt file
  rm -f /$BOOT/cmdline.old
  cp -f /$BOOT/cmdline.txt /$BOOT/cmdline.old

  # Remove config file entries and add them back in later if needed
  sed -i "s# console=ttyGS0##" /$BOOT/cmdline.txt
  sed -i "s# init=/usr/sbin/reconfig-clusterctrl cbridge##" /$BOOT/cmdline.txt
  sed -i "s# init=/usr/sbin/reconfig-clusterctrl cnat##" /$BOOT/cmdline.txt
  sed -i "s# init=/usr/sbin/reconfig-clusterctrl p[0-9]\+##" /$BOOT/cmdline.txt
  sed -i "s# init=/sbin/reconfig-clusterctrl cbridge##" /$BOOT/cmdline.txt
  sed -i "s# init=/sbin/reconfig-clusterctrl c##" /$BOOT/cmdline.txt
  sed -i "s# init=/sbin/reconfig-clusterctrl p[0-9]\+##" /$BOOT/cmdline.txt
  if [ -f /etc/dhcpcd.conf ]; then
   sed -i "s/^\(denyinterfaces.*\) eth0\(.*\)/\1\2/" /etc/dhcpcd.conf
  fi
  if [ -f /etc/default/clusterctrl ];then
   sed -i '/TYPE=.*/d' /etc/default/clusterctrl
   sed -i '/ID=.*/d' /etc/default/clusterctrl
   RESIZE=0
  else
   # Only run resize/firstboot on the first boot
   RESIZE=1
   cp -f "$CONFIGDIR/default-clusterctrl" /etc/default/clusterctrl
  fi

  # Setup the interfaces/issue files
  if [ "$1" = "cnat" ];then
   rm -f /etc/network/interfaces.d/clusterctrl
   if [ $VERSION_CODENAME = "bookworm" ];then
    cp -f "$CONFIGDIR/interfaces.bookworm.cnat" /etc/network/interfaces.d/clusterctrl
   else
    cp -f "$CONFIGDIR/interfaces.cnat" /etc/network/interfaces.d/clusterctrl
   fi
   cp -f "$CONFIGDIR/issue.c" /etc/issue
   sed -i "s/^dtoverlay=dwc2.*$/dtoverlay=dwc2,dr_mode=host/" /$BOOT/config.txt
   sed -i "s/^#otg_mode=1 # Controller only$/otg_mode=1 # Controller only/" /$BOOT/config.txt
   sed -i "s#^127.0.1.1.*#127.0.1.1\tcnat#g" /etc/hosts
   echo "cnat" > /etc/hostname
   if [ -f /etc/dhcpcd.conf ]; then
    sed -i 's~^static ip_address=172.19.181.*/24 #Cluster.*~static ip_address=172.19.181.253/24 #ClusterCTRL~' /etc/dhcpcd.conf
   fi
   sed -i "s/^#net.ipv4.ip_forward=1 # Cluster.*/net.ipv4.ip_forward=1 # ClusterCTRL/" /etc/sysctl.conf
   systemctl enable clusterctrl-init
   systemctl disable clusterctrl-composite
   systemctl disable serial-getty@ttyGS0.service
   echo "TYPE=cnat" >> /etc/default/clusterctrl
  elif [ "$1" = "cbridge" ];then
   rm -f /etc/network/interfaces.d/clusterctrl
   if [ $VERSION_CODENAME = "bookworm" ];then
    cp -f "$CONFIGDIR/interfaces.bookworm.cbridge" /etc/network/interfaces.d/clusterctrl
   else
    cp -f "$CONFIGDIR/interfaces.cbridge" /etc/network/interfaces.d/clusterctrl
   fi
   cp -f "$CONFIGDIR/issue.c" /etc/issue
   sed -i "s/^dtoverlay=dwc2.*$/dtoverlay=dwc2,dr_mode=host/" /$BOOT/config.txt
   sed -i "s/^#otg_mode=1 # Controller only$/otg_mode=1 # Controller only/" /$BOOT/config.txt
   sed -i "s#^127.0.1.1.*#127.0.1.1\tcbridge#g" /etc/hosts
   echo "cbridge" > /etc/hostname
   if [ -f /etc/dhcpcd.conf ]; then
    sed -i 's~^static ip_address=172.19.181.*/24 #Cluster.*~static ip_address=172.19.181.253/24 #ClusterCTRL~' /etc/dhcpcd.conf
    sed -i 's#^\(denyinterfaces.*\)#\1 eth0#' /etc/dhcpcd.conf
   fi
   sed -i "s/^net.ipv4.ip_forward=1 # ClusterCTRL/#net.ipv4.ip_forward=1 # ClusterCTRL/" /etc/sysctl.conf
   systemctl enable clusterctrl-init
   systemctl disable clusterctrl-composite
   systemctl disable serial-getty@ttyGS0.service
   echo "TYPE=c" >> /etc/default/clusterctrl
  else
   P=${1:1}
   rm -f /etc/network/interfaces.d/clusterctrl
   if [ $VERSION_CODENAME = "bookworm" ];then
    cp -f "$CONFIGDIR/interfaces.bookworm.p" /etc/network/interfaces.d/clusterctrl
   fi
   cp -f "$CONFIGDIR/issue.p" /etc/issue
   sed -i "s#^127.0.1.1.*#127.0.1.1\t$1#g" /etc/hosts
   sed -i "s/^dtoverlay=dwc2.*$/dtoverlay=dwc2,dr_mode=peripheral/" /$BOOT/config.txt
   sed -i "s/^otg_mode=1 # Controller only$/#otg_mode=1 # Controller only/" /$BOOT/config.txt
   echo "$1" > /etc/hostname
   if [ -f /etc/dhcpcd.conf ]; then
    sed -i "s~^static ip_address=172.19.181.*/24 #Cluster.*~static ip_address=172.19.181.$P/24 #ClusterCTRL~" /etc/dhcpcd.conf
   fi
   if [ -f /etc/dhcp/dhclient.conf ];then
    sed -i "s~^  fixed-address 172\.19\.181\..* # ClusterCTRL Px~  fixed-address 172.19.181.$P; # ClusterCTRL Px~" /etc/dhcp/dhclient.conf
   fi
   systemctl enable serial-getty@ttyGS0.service
   systemctl disable clusterctrl-init
   sed -i "s/^net.ipv4.ip_forward=1 # ClusterCTRL/#net.ipv4.ip_forward=1 # ClusterCTRL/" /etc/sysctl.conf
   echo "TYPE=node" >> /etc/default/clusterctrl
   echo "ID=$P" >> /etc/default/clusterctrl
   systemctl enable clusterctrl-composite
  fi

 else
  # Unknown argument passed - do nothing
  echo "No reconfig"
 fi
 mount -o remount,ro /
fi

if [ $RESIZE -eq 1 ];then
 if [ -f /usr/lib/raspberrypi-sys-mods/firstboot ];then
  sed -i 's#$# init=/usr/lib/raspberrypi-sys-mods/firstboot#' /$BOOT/cmdline.txt
 elif [ -f /usr/lib/raspi-config/init_resize.sh ] && [ ! -f /$BOOT/noresize ] && [ ! -f /$BOOT/noresize.txt ];then
  sed -i 's#$# init=/usr/lib/raspi-config/init_resize.sh#' /$BOOT/cmdline.txt
 fi
fi

mount /$BOOT -o remount,ro
sync

# Reboot the Pi
reboot_pi
