#!/usr/bin/env bash
set -euo pipefail

STATE_DIR="/var/lib/google-oslogin-niceos"
log(){ echo "google-oslogin(niceos): $*" >&2; }

if command -v systemctl >/dev/null 2>&1; then
  systemctl disable --now google-oslogin-cache.timer || true
fi

# Restore authselect user-nsswitch if backup exists
if [ -f "${STATE_DIR}/user-nsswitch.conf.bak" ]; then
  cp -a "${STATE_DIR}/user-nsswitch.conf.bak" /etc/authselect/user-nsswitch.conf || true
  authselect apply-changes || true
  log "user-nsswitch restored"
fi

# Restore PAM sshd if backup exists
if [ -f "${STATE_DIR}/pam.sshd.bak" ]; then
  cp -a "${STATE_DIR}/pam.sshd.bak" /etc/pam.d/sshd || true
  chmod 0644 /etc/pam.d/sshd || true
  log "PAM sshd restored"
fi

log "done"
