#!/bin/bash -x

# This script should be ran via /boot/cmdline.txt 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
  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"

mount /boot

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=.*##" /boot/cmdline.txt
  sed -i "s/^\(denyinterfaces.*\) eth0\(.*\)/\1\2/" /etc/dhcpcd.conf
  if [ -f /etc/default/clusterctrl ];then
   sed -i '/TYPE=.*/d' /etc/default/clusterctrl
   sed -i '/ID=.*/d' /etc/default/clusterctrl
   RESIZE=0
  else
   # Only do resize 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
   cp -f "$CONFIGDIR/interfaces.cnat" /etc/network/interfaces.d/clusterctrl
   cp -f "$CONFIGDIR/issue.c" /etc/issue
   sed -i "s/^dtoverlay=dwc2.*$/#dtoverlay=dwc2,dr_mode=peripheral/" /boot/config.txt
   sed -i "s#^127.0.1.1.*#127.0.1.1\tcnat#g" /etc/hosts
   echo "cnat" > /etc/hostname
   sed -i 's~^static ip_address=172.19.181.*/24 #Cluster.*~static ip_address=172.19.181.253/24 #ClusterCTRL~' /etc/dhcpcd.conf
   rm -f /etc/systemd/system/getty.target.wants/getty@ttyGS0.service
   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
   echo "TYPE=cnat" >> /etc/default/clusterctrl
  elif [ "$1" = "cbridge" ];then
   rm -f /etc/network/interfaces.d/clusterctrl
   cp -f "$CONFIGDIR/interfaces.cbridge" /etc/network/interfaces.d/clusterctrl
   cp -f "$CONFIGDIR/issue.c" /etc/issue
   sed -i "s/^dtoverlay=dwc2.*$/#dtoverlay=dwc2,dr_mode=peripheral/" /boot/config.txt
   sed -i "s#^127.0.1.1.*#127.0.1.1\tcbridge#g" /etc/hosts
   echo "cbridge" > /etc/hostname
   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
   rm -f /etc/systemd/system/getty.target.wants/getty@ttyGS0.service
   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
   echo "TYPE=c" >> /etc/default/clusterctrl
  else
   P=${1:1}
   rm -f /etc/network/interfaces.d/clusterctrl
   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
   echo "$1" > /etc/hostname
   sed -i "s~^static ip_address=172.19.181.*/24 #Cluster.*~static ip_address=172.19.181.$P/24 #ClusterCTRL~" /etc/dhcpcd.conf
   sed -i 's#^\(denyinterfaces.*\)#\1 eth0#' /etc/dhcpcd.conf
   ln -fs /lib/systemd/system/getty@.service \
    /etc/systemd/system/getty.target.wants/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 /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
