#!/bin/bash
set -e
set +o pipefail

# Always return root filesystem to RO on exit
cleanup() {
  exit_code=$?
  echo "Installer exiting with code $exit_code"
  echo "Returning root filesystem to read-only..."
  sudo mount -o remount,ro / || true
}
trap cleanup EXIT

sudo mount -o remount,rw /

# Ensure PHASON storage is writable
findmnt -no OPTIONS /media/pi/PHASON | grep -qw ro && sudo mount -o remount,rw /media/pi/PHASON || true

echo "enabled RW on /media/pi/PHASON/Logs..."

cd /tmp/os/update043Extract || {
  echo "Error: /tmp/os/update043Extract not found"
  exit 1
}

echo "/tmp/os/update043Extract path verified before installation..."

# Stop services before updating
sudo systemctl stop ph_alive_ping.timer || true
sudo systemctl stop ph_alive_ping.service || true

# Cleaning old files
# removing /etc/systemd/system/
sudo rm -f /etc/systemd/system/ph_alive_ping.service
sudo rm -f /etc/systemd/system/ph_alive_ping.timer
# removing /usr/local/bin/identitysyncservices/
sudo rm -f /usr/local/bin/identitysyncservices/ph_alive_ping.py
sudo rm -f /usr/local/bin/identitysyncservices/ph_common.py
sudo rm -f /usr/local/bin/identitysyncservices/ph_identity_decide.py
sudo rm -f /usr/local/bin/identitysyncservices/ph_identity.pyl
sudo rm -f /usr/local/bin/identitysyncservices/cloud-api-config.txt
# removing folder if exists
sudo rm -rf /usr/local/bin/identitysyncservices

sudo mkdir -p /media/pi/PHASON/Logs || true
sudo mkdir -p /usr/local/bin/identitysyncservices

echo "Cleaned up old files..."

sudo cp -a etc/. /etc/
sudo cp -a usr/. /usr/

# test whether files are copied successfully
test -f /etc/systemd/system/ph_alive_ping.service
test -f /etc/systemd/system/ph_alive_ping.timer
test -f /usr/local/bin/identitysyncservices/ph_alive_ping.py
test -f /usr/local/bin/identitysyncservices/ph_common.py
test -f /usr/local/bin/identitysyncservices/ph_identity_decide.py
test -f /usr/local/bin/identitysyncservices/ph_identity.py
test -f /usr/local/bin/identitysyncservices/cloud-api-config.txt

echo "Copied all package files..."

sudo chown -R pi:pi /usr/local/bin/identitysyncservices || true
sudo chmod -R 775 /usr/local/bin/identitysyncservices || true

echo "Permission setup completed..."

# sub services package installation flag added
sudo touch /home/pi/update043

# enable system daemon and start services
sudo systemctl daemon-reload
sudo systemctl enable ph_alive_ping.timer
sudo systemctl restart ph_alive_ping.timer
sudo systemctl start ph_alive_ping.service || true

echo "Checking service status..."

if ! sudo systemctl is-enabled --quiet ph_alive_ping.timer; then
  echo "Warning: ph_alive_ping.timer is not enabled."
  sudo systemctl status ph_alive_ping.timer --no-pager || true
fi

if ! sudo systemctl is-active --quiet ph_alive_ping.timer; then
  echo "ph_alive_ping.timer not active, retrying once..."
  sudo systemctl restart ph_alive_ping.timer || true

  if ! sudo systemctl is-active --quiet ph_alive_ping.timer; then
    echo "Warning: ph_alive_ping.timer still not active."
    sudo systemctl status ph_alive_ping.timer --no-pager || true
  fi
fi

if sudo systemctl is-failed --quiet ph_alive_ping.service; then
  echo "Warning: ph_alive_ping.service is in failed state."
  sudo systemctl status ph_alive_ping.service --no-pager || true
fi

echo "Ph alive ping service and ph_identity_decide service update043 package installed."