#!/bin/bash

# Check if this script is running for the first time
if [ ! -f /tmp/setup_script_ran ]; then
  # This is the first time the script is being executed
  echo "Starting setup script..."

  # Create a file to mark that the script has been executed
  touch /tmp/setup_script_ran

  # Add this script to the list of startup scripts
  sudo sed -i -e '$i /usr/local/bin/setup_script.sh &\n' /etc/rc.local

  # Execute some commands before the reboot
  echo "Executing some commands before the reboot..."

  # Reboot the system
  echo "Rebooting the system..."
  sudo reboot
else
  # This is the second time the script is being executed (after the reboot)
  echo "Resuming setup script after reboot..."

  # Remove the file that marks that the script has been executed
  sudo rm /tmp/setup_script_ran

  # Remove this script from the list of startup scripts
  sudo sed -i '/setup_script.sh/d' /etc/rc.local

  # Execute some commands after the reboot
  echo "Executing some commands after the reboot..."

  # Clean up before exiting
  echo "Cleaning up..."
  sudo rm /usr/local/bin/setup_script.sh

  # Exit the script
  echo "Setup script completed successfully."
  exit 0
fi
