#!/bin/bash -e

function usage {
    cat <<EOF
Usage:
    $0 STAGE
Arguments:
    STAGE     One of the following:
                - dev: development staging (note that the dev stage does
                       not automatically update to the next major release
                       series)
                - demo: beta testing in advance of production releases
                - prod: full production releases for general availability
EOF
}

if [ $# -ne 1 ] ; then
    if [ $# -lt 1 ] ; then
        echo "Error: missing STAGE argument" >&2
    else
        echo "Error: extra arguments after STAGE" >&2
    fi
    echo >&2
    usage >&2
    exit 1
fi

STAGE="$1"

if [ $EUID != 0 ] ; then
    echo "Program requires superuser privileges" >&2
    exit 1
fi

if [ "$STAGE" == "dev" ] ; then
    base_url="https://ostree.endlessm.com/staging/dev"
    new_collection_id="com.endlessm.Dev.Os"
    # Check for an update every time the updater runs
    interval_days=0
elif [ "$STAGE" == "demo" ] ; then
    base_url="https://ostree.endlessm.com/staging/demo"
    new_collection_id="com.endlessm.Demo.Os"
    # Check for an update every time the updater runs
    interval_days=0
elif [ "$STAGE" == "prod" ] ; then
    base_url="https://ostree.endlessm.com/ostree"
    new_collection_id="com.endlessm.Os"
    # By default we check for updates every two weeks on production
    interval_days=14
else
    echo "Invalid STAGE -- must be dev, demo, or prod" >&2
    exit 1
fi

# Get the current URL and convert to the new stage.
url=$(ostree config get 'remote "eos".url')
repo=${url##*/}
new_url="${base_url}/${repo}"

# Make sure the autoupdater doesn't start the updater after it's
# killed below.
echo "Stopping running eos-autoupdater."
systemctl stop eos-autoupdater.timer eos-autoupdater.service

echo "Killing eos-updater."
systemctl stop eos-updater.service

echo "Configuring OSTree for $STAGE stage."

# Update the repo URL
ostree config set 'remote "eos".url' "$new_url"

# Update the collection ID if it's set
collection_id=$(ostree config get 'remote "eos".collection-id' 2>/dev/null || echo "")
if [ -n "$collection_id" ] ; then
    ostree config set 'remote "eos".collection-id' "$new_collection_id"
fi

# Delete the ostree-metadata, if any, because it is only valid for
# the old repo URL, and an attempt to update it might fail if it's
# considered a downgrade.
ostree refs --delete eos:ostree-metadata

# Adjust the frequency of update checks
# eos-updater.conf is the old location of the config file.
sed -i "s/IntervalDays=.*/IntervalDays=$interval_days/" /etc/eos-updater/eos-autoupdater.conf 2>/dev/null || true
sed -i "s/IntervalDays=.*/IntervalDays=$interval_days/" /etc/eos-updater.conf 2>/dev/null || true

if [ ! -f /etc/eos-updater/eos-autoupdater.conf ] && [ ! -f /etc/eos-updater.conf ]; then
    mkdir -p /etc/eos-updater
    cp /usr/share/eos-updater/eos-autoupdater.conf /etc/eos-updater

    sed -i "s/IntervalDays=.*/IntervalDays=$interval_days/" /etc/eos-updater/eos-autoupdater.conf
fi

# Restart the autoupdater timer, and try to update now.
# Note that downgrades (e.g., from demo to previous prod release)
# won't happen automatically.  They would have to be done via
# `ostree admin upgrade --allow-downgrade`, but that should be
# a manual step if someone really wants to downgrade.
echo "Starting eos-autoupdater."
systemctl start eos-autoupdater.timer eos-autoupdater.service
