post_install() {
  echo "Configuring stellarmate scripts..."
  if [ -f /etc/stellarmate/model ]; then
    # Set executable permissions for scripts
    find /usr/share/smoscore -type f \( -name "*.sh" -o -name "*.py" \) -exec chmod +x {} \;
    
    model="$(head -1 /etc/stellarmate/model)"
    echo "Detected model: $model"

    # Ensure /etc/stellarmate exists
    mkdir -p /etc/stellarmate

    # Sync common scripts
    rsync --delete -az "/usr/share/smoscore/scripts/common/" "/etc/stellarmate"
    echo "Common files copied"

    # Sync model-specific scripts
    if [ -d "/usr/share/smoscore/scripts/$model/" ]; then
      rsync -az "/usr/share/smoscore/scripts/$model/" "/etc/stellarmate"
      echo "Model ($model) files copied"
    else
      echo "Warning: Model-specific scripts for '$model' not found in package."
    fi

    chown -R stellarmate:stellarmate /etc/stellarmate || true
    echo "Files permission modified"
  else
    echo "Warning: /etc/stellarmate/model not found. Skipping model-specific script configuration."
  fi

  chown -R stellarmate:stellarmate /opt/ekoslive || true

  # Set correction permissions
  chmod 600 /etc/NetworkManager/system-connections/*
}

post_upgrade() {
  post_install # Re-run configuration on upgrade
}

post_remove() {
  rm -rf /usr/share/smoscore/scripts
  echo "Stellarmate scripts package removed. Configuration files in /etc/stellarmate may remain."
}
