#!/bin/bash -e

function warning_blob() {
    cat <<MR_BLOBBY
This script uses "ostree admin unlock" to create a writable /usr overlay, and
makes some other basic tweaks so that apt works in this setup. This allows
Endless OS developers to test changes to the OS, but is not a supported
configuration for regular use.

By default, the changes made to the overlay will be lost on reboot. Passing
--hotfix will make the changes persist across reboot, keeping the current
deployment as the rollback deployment, but the changes will be lost on the next
OS update. You may wish to mask eos-updater.service if you want to make
long-lived changes.

MR_BLOBBY
}

if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
    echo "Usage:"
    echo "   $0 [--hotfix]"
    echo ""
    warning_blob
    exit 0
fi

warning_blob
read -p "Are you sure you want to proceed? [y/N] "
response="${REPLY,,}" # to lower
if [[ ! "$response" =~ ^(yes|y)$ ]]; then
    exit 1
fi

# Check that script was run with superuser privileges.
if [[ $EUID != 0 ]]; then
    echo "$0 requires superuser privileges."
    exit 1
fi

systemctl stop eos-autoupdater.timer
systemctl stop eos-autoupdater.service
systemctl stop eos-updater.service

ostree admin unlock "$@"
