#!/usr/bin/with-contenv bash
set -eu
# The companion removes its previous owned container before reusing this
# dedicated volume. Chrome's old hostname/PID locks cannot survive that restart.
# Keep the browser profile, cookies and preferences; remove only transient locks.
profile=/config/second-look-chromium
if [ -L "$profile" ]; then
  echo 'The dedicated browser profile must not be a symbolic link.' >&2
  exit 1
fi
rm -f -- "$profile/SingletonLock" "$profile/SingletonSocket" "$profile/SingletonCookie"
# Only this dedicated directory is exposed by the explicit download action.
if [ -L /config/Downloads ]; then
  echo 'The dedicated downloads directory must not be a symbolic link.' >&2
  exit 1
fi
install -d -o abc -g abc -m 0700 /config/Downloads
mkdir -p /etc/chromium/policies/managed
cat > /etc/chromium/policies/managed/second-look-downloads.json <<'JSON'
{"DownloadDirectory":"/config/Downloads","PromptForDownloadLocation":false}
JSON
