#!/bin/bash

set -xeuo pipefail

# This script fixes up and removes old references to the update-via-foxnet mechanism in /etc/hosts and /root/.netrc --
# both of which may have post-factory-image changes causing bad settings to persist.
#
# This is a kludge to ensure dev units that were on pre-202110x images don't inadvertently break their connectivity to
# the update server by retaining bad config changes due to local edits.

# While the other kludge of requiring http auth tokens in /root/.netrc exists, require it is in sync with the factory
# file.
cp -av /usr/share/factory/root/.netrc /root/.netrc

# Check for old /etc/hosts lines.  If /etc/hosts has been edited, these would persist and not revert back to the factory
# file on next update.
if grep -Eq '^10\.77\.77\.1 (atomupd|images)\.steamos\.cloud' /etc/hosts; then
  sed -ri '/^10\.77\.77\.1 (atomupd|images)\.steamos\.cloud/ d' /etc/hosts
fi

# Always purge manifest entries in the /etc/ overlay such that local edits cannot brick updates (foxnet also touched
# this)
overlay_manifest=/var/lib/overlays/etc/upper/steamos-atomupd/client.conf
if [[ -f $overlay_manifest ]]; then
  rm -f $overlay_manifest
  mount -o remount /etc # So overlay resyncs
fi
